Skip to contentSkip to conversation

virtiofsd: Fix side-effect in assert()

PATCH In review Greg Kurz 7 participants 8 messages Apr 9, 2021 – May 6, 2021

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/1 virtiofsd: Fix side-effect in assert()

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

  2796          /* fchdir should not fail here */
=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
  2798          ret = getxattr(procname, name, value, size);
  2799          FCHDIR_NOFAIL(lo->root.fd);
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")
---
 tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
tools/virtiofsd/passthrough_ll.c +13 -8
index 1553d2ef454f..6592f96f685e 100644
@@ -2723,6 +2723,11 @@ static int xattr_map_server(const struct lo_data *lo, const char *server_name,
     return -ENODATA;
 }
 
+#define FCHDIR_NOFAIL(fd) do {                         \
+        int fchdir_res = fchdir(fd);                   \
+        assert(fchdir_res == 0);                       \
+    } while (0)
+
 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
                         size_t size)
 {
@@ -2789,9 +2794,9 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
         ret = fgetxattr(fd, name, value, size);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = getxattr(procname, name, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     if (ret == -1) {
@@ -2864,9 +2869,9 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
         ret = flistxattr(fd, value, size);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = listxattr(procname, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     if (ret == -1) {
@@ -3000,9 +3005,9 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
         ret = fsetxattr(fd, name, value, size, flags);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = setxattr(procname, name, value, size, flags);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     saverr = ret == -1 ? errno : 0;
@@ -3066,9 +3071,9 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *in_name)
         ret = fremovexattr(fd, name);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = removexattr(procname, name);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     saverr = ret == -1 ? errno : 0;
Signature · 1 line
2.26.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Greg Kurz

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

  2796          /* fchdir should not fail here */
=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
  2798          ret = getxattr(procname, name, value, size);
  2799          FCHDIR_NOFAIL(lo->root.fd);
Quoted 6 lines
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")
---
 tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Greg Kurz
Quoted 11 lines · Greg Kurz

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

Quoted 8 lines
  2796          /* fchdir should not fail here */
=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
  2798          ret = getxattr(procname, name, value, size);
  2799          FCHDIR_NOFAIL(lo->root.fd);
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")

Looks good to me.

Vivek

Quoted 75 lines
---
 tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
tools/virtiofsd/passthrough_ll.c +13 -8
index 1553d2ef454f..6592f96f685e 100644
@@ -2723,6 +2723,11 @@ static int xattr_map_server(const struct lo_data *lo, const char *server_name,
     return -ENODATA;
 }
 
+#define FCHDIR_NOFAIL(fd) do {                         \
+        int fchdir_res = fchdir(fd);                   \
+        assert(fchdir_res == 0);                       \
+    } while (0)
+
 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
                         size_t size)
 {
@@ -2789,9 +2794,9 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
         ret = fgetxattr(fd, name, value, size);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = getxattr(procname, name, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     if (ret == -1) {
@@ -2864,9 +2869,9 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
         ret = flistxattr(fd, value, size);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = listxattr(procname, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     if (ret == -1) {
@@ -3000,9 +3005,9 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
         ret = fsetxattr(fd, name, value, size, flags);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = setxattr(procname, name, value, size, flags);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     saverr = ret == -1 ? errno : 0;
@@ -3066,9 +3071,9 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *in_name)
         ret = fremovexattr(fd, name);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = removexattr(procname, name);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     saverr = ret == -1 ? errno : 0;
Signature · 6 lines
2.26.3

_______________________________________________
Virtio-fs mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/virtio-fs
Reviewed-by on v1 1/1
#3 lore raw
In reply to Greg Kurz
Quoted 16 lines · Greg Kurz

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

2796 /* fchdir should not fail here */

=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
   2798          ret = getxattr(procname, name, value, size);
   2799          FCHDIR_NOFAIL(lo->root.fd);
Quoted 6 lines
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")
---
  tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------
  1 file changed, 13 insertions(+), 8 deletions(-)
Quoted 65 lines
tools/virtiofsd/passthrough_ll.c +13 -8
index 1553d2ef454f..6592f96f685e 100644
@@ -2723,6 +2723,11 @@ static int xattr_map_server(const struct lo_data *lo, const char *server_name,
      return -ENODATA;
  }
 
+#define FCHDIR_NOFAIL(fd) do {                         \
+        int fchdir_res = fchdir(fd);                   \
+        assert(fchdir_res == 0);                       \
+    } while (0)
+
  static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
                          size_t size)
  {
@@ -2789,9 +2794,9 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
          ret = fgetxattr(fd, name, value, size);
      } else {
          /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
          ret = getxattr(procname, name, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
      }
 
      if (ret == -1) {
@@ -2864,9 +2869,9 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
          ret = flistxattr(fd, value, size);
      } else {
          /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
          ret = listxattr(procname, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
      }
 
      if (ret == -1) {
@@ -3000,9 +3005,9 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
          ret = fsetxattr(fd, name, value, size, flags);
      } else {
          /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
          ret = setxattr(procname, name, value, size, flags);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
      }
 
      saverr = ret == -1 ? errno : 0;
@@ -3066,9 +3071,9 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *in_name)
          ret = fremovexattr(fd, name);
      } else {
          /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
          ret = removexattr(procname, name);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
      }
 
      saverr = ret == -1 ? errno : 0;
Reviewed-by on v1 1/1
#4 lore raw
In reply to Greg Kurz
Quoted 11 lines · Greg Kurz

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

Quoted 8 lines
  2796          /* fchdir should not fail here */
=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
  2798          ret = getxattr(procname, name, value, size);
  2799          FCHDIR_NOFAIL(lo->root.fd);
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")
Signature · 1 line
Alex Bennée
#5 lore raw
In reply to Greg Kurz

Am 09.04.21 um 12:06 schrieb Greg Kurz:

tools/virtiofsd/passthrough_ll.c static int xattr_map_server(const struct lo_data

Quoted 28 lines

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

2796 /* fchdir should not fail here */

=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
   2798          ret = getxattr(procname, name, value, size);
   2799          FCHDIR_NOFAIL(lo->root.fd);
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")
---
  tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------
  1 file changed, 13 insertions(+), 8 deletions(-)
tools/virtiofsd/passthrough_ll.c +0 -0
index 1553d2ef454f..6592f96f685e 100644
@@ -2723,6 +2723,11 @@ static int xattr_map_server(const struct lo_data 

*lo, const char *server_name,

tools/virtiofsd/passthrough_ll.c static int xattr_map_server(const struct lo_data *lo, const char *server_name,

Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +5 -0
      return -ENODATA;
  }
 
+#define FCHDIR_NOFAIL(fd) do {                         \
+        int fchdir_res = fchdir(fd);                   \
+        assert(fchdir_res == 0);                       \
+    } while (0)
+
  static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
 

I am afraid that this will raise a compiler warning (or error with -Werror) when NDEBUG is defined because the variable is unused in that case ([-Wunused-variable]).

I suggest to use different implementations of the macro depending on NDEBUG.

Stefan

#6 lore raw

Hi Stefan,

Quoted 44 lines · Stefan Weil

Am 09.04.21 um 12:06 schrieb Greg Kurz:

tools/virtiofsd/passthrough_ll.c static int xattr_map_server(const struct lo_data

Quoted 29 lines

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
  Assertion `fchdir_res == 0' failed.
Aborted

   2796          /* fchdir should not fail here */ =>2797          FCHDIR_NOFAIL(lo->proc_self_fd); 2798          ret = getxattr(procname, name, value, size); 2799          FCHDIR_NOFAIL(lo->root.fd);

Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")

---
  tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------

  1 file changed, 13 insertions(+), 8 deletions(-)
tools/virtiofsd/passthrough_ll.c +0 -0
index 1553d2ef454f..6592f96f685e 100644
@@ -2723,6 +2723,11 @@ static int xattr_map_server(const struct lo_data 

*lo, const char *server_name,

Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +4 -0
       return -ENODATA;
   }
   +#define FCHDIR_NOFAIL(fd) do {                         \
+        int fchdir_res = fchdir(fd);                   \
+        assert(fchdir_res == 0);                       \
+    } while (0)
+

  static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,

Quoted 6 lines

I am afraid that this will raise a compiler warning (or error with -Werror) when NDEBUG is defined because the variable is unused in that case ([-Wunused-variable]).

I suggest to use different implementations of the macro depending on NDEBUG.

QEMU doesn't build with NDEBUG, see commit 262a69f4282 ("osdep.h: Prohibit disabling assert() in supported builds").

Regards,

Phil.

#7 lore raw
In reply to Greg Kurz
Quoted 11 lines · * Greg Kurz ([email protected]) wrote:

It is bad practice to put an expression with a side-effect in assert() because the side-effect won't happen if the code is compiled with -DNDEBUG.

Use an intermediate variable. Consolidate this in an macro to have proper line numbers when the assertion is hit.

virtiofsd: ../../tools/virtiofsd/passthrough_ll.c:2797: lo_getxattr:
Assertion `fchdir_res == 0' failed.
Aborted

Quoted 8 lines
  2796          /* fchdir should not fail here */
=>2797          FCHDIR_NOFAIL(lo->proc_self_fd);
  2798          ret = getxattr(procname, name, value, size);
  2799          FCHDIR_NOFAIL(lo->root.fd);
Fixes bdfd66788349 ("virtiofsd: Fix xattr operations")

Queued

Quoted 72 lines
---
 tools/virtiofsd/passthrough_ll.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
tools/virtiofsd/passthrough_ll.c +13 -8
index 1553d2ef454f..6592f96f685e 100644
@@ -2723,6 +2723,11 @@ static int xattr_map_server(const struct lo_data *lo, const char *server_name,
     return -ENODATA;
 }
 
+#define FCHDIR_NOFAIL(fd) do {                         \
+        int fchdir_res = fchdir(fd);                   \
+        assert(fchdir_res == 0);                       \
+    } while (0)
+
 static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
                         size_t size)
 {
@@ -2789,9 +2794,9 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
         ret = fgetxattr(fd, name, value, size);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = getxattr(procname, name, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     if (ret == -1) {
@@ -2864,9 +2869,9 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
         ret = flistxattr(fd, value, size);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = listxattr(procname, value, size);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     if (ret == -1) {
@@ -3000,9 +3005,9 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *in_name,
         ret = fsetxattr(fd, name, value, size, flags);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = setxattr(procname, name, value, size, flags);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     saverr = ret == -1 ? errno : 0;
@@ -3066,9 +3071,9 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *in_name)
         ret = fremovexattr(fd, name);
     } else {
         /* fchdir should not fail here */
-        assert(fchdir(lo->proc_self_fd) == 0);
+        FCHDIR_NOFAIL(lo->proc_self_fd);
         ret = removexattr(procname, name);
-        assert(fchdir(lo->root.fd) == 0);
+        FCHDIR_NOFAIL(lo->root.fd);
     }
 
     saverr = ret == -1 ? errno : 0;
Signature · 1 line
2.26.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK