diff options
author | Eric Blake <eblake@redhat.com> | 2016-11-07 14:38:13 -0600 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2016-11-10 16:01:30 +0100 |
commit | a5068244b4f0c994791303b6186b6f732adab6c2 (patch) | |
tree | ddd9d91c164f50d07796dfc1b66beb76aeceb6c1 | |
parent | 175cad36a599bb24ab2a5cd195c96b1f123e25a9 (diff) | |
download | qemu-a5068244b4f0c994791303b6186b6f732adab6c2.tar.gz qemu-a5068244b4f0c994791303b6186b6f732adab6c2.tar.bz2 qemu-a5068244b4f0c994791303b6186b6f732adab6c2.zip |
nbd: Don't inf-loop on early EOF
Commit 7d3123e converted a single read_sync() into a while loop
that assumed that read_sync() would either make progress or give
an error. But when the server hangs up early, the client sees
EOF (a read_sync() of 0) and never makes progress, which in turn
caused qemu-iotest './check -nbd 83' to go into an infinite loop.
Rework the loop to accomodate reads cut short by EOF.
Reported-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1478551093-32757-1-git-send-email-eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r-- | nbd/client.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/nbd/client.c b/nbd/client.c index 7db4301d29..ffb0743bce 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -90,20 +90,21 @@ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports); * the amount of bytes consumed. */ static ssize_t drop_sync(QIOChannel *ioc, size_t size) { - ssize_t ret, dropped = size; + ssize_t ret = 0; char small[1024]; char *buffer; buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size)); while (size > 0) { - ret = read_sync(ioc, buffer, MIN(65536, size)); - if (ret < 0) { + ssize_t count = read_sync(ioc, buffer, MIN(65536, size)); + + if (count <= 0) { goto cleanup; } - assert(ret <= size); - size -= ret; + assert(count <= size); + size -= count; + ret += count; } - ret = dropped; cleanup: if (buffer != small) { |