[RFC PATCH 2/4] selftests/binderfs: Update close_prot_errno_disarm macro to do{...}while(false) structure for safety

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Enclosing the close_prot_errno_disarm macro in do {...} while (false)
structure could prevent potential bugs and undefined behavior.

Example code:

    #define BINDERFUNC(x) f(x); g(x);
    if (condition)
	BINDERFUNC(x);

When BINDERFUNC(x) expands, g(x) would be executed outside of if
block. Enclosing the macro under do{...}while(false) adds a scope to
the macro, making it safer.

Signed-off-by: Abhinav Saxena <xandfury@xxxxxxxxx>
---
 .../filesystems/binderfs/binderfs_test.c         | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c
index 447ec4296e69..4a149e3d4ba4 100644
--- a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c
+++ b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c
@@ -28,13 +28,15 @@
 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))

-#define close_prot_errno_disarm(fd) \
-	if (fd >= 0) {              \
-		int _e_ = errno;    \
-		close(fd);          \
-		errno = _e_;        \
-		fd = -EBADF;        \
-	}
+#define close_prot_errno_disarm(fd)      \
+	do {				 \
+		if (fd >= 0) {		 \
+			int _e_ = errno; \
+			close(fd);	 \
+			errno = _e_;	 \
+			fd = -EBADF;	 \
+		}			 \
+	} while (false)

 static void change_mountns(struct __test_metadata *_metadata)
 {
--
2.34.1





[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux