From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39539) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c4tWx-0004yj-4q for qemu-devel@nongnu.org; Thu, 10 Nov 2016 12:52:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c4tWw-0003O2-HV for qemu-devel@nongnu.org; Thu, 10 Nov 2016 12:52:55 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:34518) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c4tWw-0003Np-Ao for qemu-devel@nongnu.org; Thu, 10 Nov 2016 12:52:54 -0500 Received: by mail-wm0-x241.google.com with SMTP id g23so4619456wme.1 for ; Thu, 10 Nov 2016 09:52:54 -0800 (PST) Sender: Paolo Bonzini From: Paolo Bonzini Date: Thu, 10 Nov 2016 18:52:42 +0100 Message-Id: <1478800362-18138-7-git-send-email-pbonzini@redhat.com> In-Reply-To: <1478800362-18138-1-git-send-email-pbonzini@redhat.com> References: <1478800362-18138-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PULL 6/6] nbd: Don't inf-loop on early EOF List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org From: Eric Blake 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 Signed-off-by: Eric Blake Message-Id: <1478551093-32757-1-git-send-email-eblake@redhat.com> Signed-off-by: Paolo Bonzini --- nbd/client.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 7db4301..ffb0743 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) { -- 1.8.3.1