Skip to contentSkip to conversation

nbd/server: improve nbd_negotiate_send_rep_list

PATCH In review Vladimir Sementsov-Ogievskiy 2 participants 3 messages Dec 26, 2019 – Jan 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd/server: improve nbd_negotiate_send_rep_list

Don't try to write zero-lenght strings.

---
 nbd/server.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
nbd/server.c +10 -6
index 24ebc1a805..28a915f5a2 100644
@@ -392,14 +392,18 @@ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
         return -EINVAL;
     }
 
-    if (nbd_write(ioc, name, name_len, errp) < 0) {
-        error_prepend(errp, "write failed (name buffer): ");
-        return -EINVAL;
+    if (name_len > 0) {
+        if (nbd_write(ioc, name, name_len, errp) < 0) {
+            error_prepend(errp, "write failed (name buffer): ");
+            return -EINVAL;
+        }
     }
 
-    if (nbd_write(ioc, desc, desc_len, errp) < 0) {
-        error_prepend(errp, "write failed (description buffer): ");
-        return -EINVAL;
+    if (desc_len > 0) {
+        if (nbd_write(ioc, desc, desc_len, errp) < 0) {
+            error_prepend(errp, "write failed (description buffer): ");
+            return -EINVAL;
+        }
     }
 
     return 0;
Signature · 1 line
2.21.0
#1 lore raw
Quoted 1 line · Vladimir Sementsov-Ogievskiy

Don't try to write zero-lenght strings.

length

Quoted 11 lines
---
  nbd/server.c | 16 ++++++++++------
  1 file changed, 10 insertions(+), 6 deletions(-)
nbd/server.c +0 -0
index 24ebc1a805..28a915f5a2 100644

nbd/server.c static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, · line 396

Quoted 8 lines
nbd/server.c +2 -3
@@ -392,14 +392,18 @@ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
          return -EINVAL;
      }
 
-    if (nbd_write(ioc, name, name_len, errp) < 0) {
-        error_prepend(errp, "write failed (name buffer): ");
-        return -EINVAL;
+    if (name_len > 0) {
+        if (nbd_write(ioc, name, name_len, errp) < 0) {

What's the rationale for this change? nbd_write() should be a no-op for a zero length write, at which point this is a micro-optimization (fewer CPU cycles, but no semantic change).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 11 lines · Eric Blake
Quoted 1 line · Vladimir Sementsov-Ogievskiy

Don't try to write zero-lenght strings.

length

Quoted 6 lines

---
  nbd/server.c | 16 ++++++++++------

  1 file changed, 10 insertions(+), 6 deletions(-)
Quoted 14 lines

nbd/server.c static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp, · line 395

Quoted 12 lines
nbd/server.c +2 -3
index 24ebc1a805..28a915f5a2 100644
@@ -392,14 +392,18 @@ static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
           return -EINVAL;
       }
-    if (nbd_write(ioc, name, name_len, errp) < 0) {
-        error_prepend(errp, "write failed (name buffer): ");
-        return -EINVAL;
+    if (name_len > 0) {
+        if (nbd_write(ioc, name, name_len, errp) < 0) {

What's the rationale for this change?  nbd_write() should be a no-op

I looked through the code and it seems for me that nobody does this check, so it would not be a no-op, at least it would be extra syscall..

Quoted 2 lines

for a zero length write, at which point this is a micro-optimization (fewer CPU cycles, but no semantic change).

Signature · 2 lines
Best regards,
Vladimir