Blame SOURCES/kvm-nbd-client-Drop-pointless-buf-variable.patch

7711c0
From 4f4e329628b6d530cf74d792f0297f651e18e6ba Mon Sep 17 00:00:00 2001
7711c0
From: John Snow <jsnow@redhat.com>
7711c0
Date: Wed, 27 Mar 2019 17:22:24 +0100
7711c0
Subject: [PATCH 085/163] nbd/client: Drop pointless buf variable
7711c0
7711c0
RH-Author: John Snow <jsnow@redhat.com>
7711c0
Message-id: <20190327172308.31077-12-jsnow@redhat.com>
7711c0
Patchwork-id: 85182
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 11/55] nbd/client: Drop pointless buf variable
7711c0
Bugzilla: 1691009
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
7711c0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
7711c0
From: Eric Blake <eblake@redhat.com>
7711c0
7711c0
There's no need to read into a temporary buffer (oversized
7711c0
since commit 7d3123e1) followed by a byteswap into a uint64_t
7711c0
to check for a magic number via memcmp(), when the code
7711c0
immediately below demonstrates reading into the uint64_t then
7711c0
byteswapping in place and checking for a magic number via
7711c0
integer math.  What's more, having a different error message
7711c0
when the server's first reply byte is 0 is unusual - it's no
7711c0
different from any other wrong magic number, and we already
7711c0
detected short reads. That whole strlen() issue has been
7711c0
present and useless since commit 1d45f8b5 in 2010; perhaps it
7711c0
was leftover debugging (since the correct magic number happens
7711c0
to be ASCII)?  Make the error messages more consistent and
7711c0
detailed while touching things.
7711c0
7711c0
Signed-off-by: Eric Blake <eblake@redhat.com>
7711c0
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
7711c0
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
7711c0
Message-Id: <20181215135324.152629-9-eblake@redhat.com>
7711c0
(cherry picked from commit ef2e35fcc8e14bcc9366df5fdf53f65d679f8dca)
7711c0
Signed-off-by: John Snow <jsnow@redhat.com>
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 nbd/client.c       | 22 +++++++---------------
7711c0
 nbd/nbd-internal.h |  3 ++-
7711c0
 2 files changed, 9 insertions(+), 16 deletions(-)
7711c0
7711c0
diff --git a/nbd/client.c b/nbd/client.c
7711c0
index 5a03a84..f625c20 100644
7711c0
--- a/nbd/client.c
7711c0
+++ b/nbd/client.c
7711c0
@@ -733,7 +733,6 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
                           QIOChannel **outioc, NBDExportInfo *info,
7711c0
                           Error **errp)
7711c0
 {
7711c0
-    char buf[256];
7711c0
     uint64_t magic;
7711c0
     int rc;
7711c0
     bool zeroes = true;
7711c0
@@ -754,27 +753,20 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
         goto fail;
7711c0
     }
7711c0
 
7711c0
-    if (nbd_read(ioc, buf, 8, errp) < 0) {
7711c0
-        error_prepend(errp, "Failed to read data: ");
7711c0
-        goto fail;
7711c0
-    }
7711c0
-
7711c0
-    buf[8] = '\0';
7711c0
-    if (strlen(buf) == 0) {
7711c0
-        error_setg(errp, "Server connection closed unexpectedly");
7711c0
+    if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
7711c0
+        error_prepend(errp, "Failed to read initial magic: ");
7711c0
         goto fail;
7711c0
     }
7711c0
-
7711c0
-    magic = ldq_be_p(buf);
7711c0
+    magic = be64_to_cpu(magic);
7711c0
     trace_nbd_receive_negotiate_magic(magic);
7711c0
 
7711c0
-    if (memcmp(buf, "NBDMAGIC", 8) != 0) {
7711c0
-        error_setg(errp, "Invalid magic received");
7711c0
+    if (magic != NBD_INIT_MAGIC) {
7711c0
+        error_setg(errp, "Bad initial magic received: 0x%" PRIx64, magic);
7711c0
         goto fail;
7711c0
     }
7711c0
 
7711c0
     if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
7711c0
-        error_prepend(errp, "Failed to read magic: ");
7711c0
+        error_prepend(errp, "Failed to read server magic: ");
7711c0
         goto fail;
7711c0
     }
7711c0
     magic = be64_to_cpu(magic);
7711c0
@@ -913,7 +905,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
7711c0
         }
7711c0
         info->flags = oldflags;
7711c0
     } else {
7711c0
-        error_setg(errp, "Bad magic received");
7711c0
+        error_setg(errp, "Bad server magic received: 0x%" PRIx64, magic);
7711c0
         goto fail;
7711c0
     }
7711c0
 
7711c0
diff --git a/nbd/nbd-internal.h b/nbd/nbd-internal.h
7711c0
index f38be9e..82aa221 100644
7711c0
--- a/nbd/nbd-internal.h
7711c0
+++ b/nbd/nbd-internal.h
7711c0
@@ -46,8 +46,9 @@
7711c0
 /* Size of oldstyle negotiation */
7711c0
 #define NBD_OLDSTYLE_NEGOTIATE_SIZE (8 + 8 + 8 + 4 + 124)
7711c0
 
7711c0
+#define NBD_INIT_MAGIC              0x4e42444d41474943LL /* ASCII "NBDMAGIC" */
7711c0
 #define NBD_REQUEST_MAGIC           0x25609513
7711c0
-#define NBD_OPTS_MAGIC              0x49484156454F5054LL
7711c0
+#define NBD_OPTS_MAGIC              0x49484156454F5054LL /* ASCII "IHAVEOPT" */
7711c0
 #define NBD_CLIENT_MAGIC            0x0000420281861253LL
7711c0
 #define NBD_REP_MAGIC               0x0003e889045565a9LL
7711c0
 
7711c0
-- 
7711c0
1.8.3.1
7711c0