diff mbox series

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

Message ID 20240515010805.605511-3-xandfury@gmail.com (mailing list archive)
State New
Headers show
Series selftests/binderfs: Fixes to binderfs_test | expand

Commit Message

Abhinav Saxena May 15, 2024, 1:08 a.m. UTC
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@gmail.com>
---
 .../filesystems/binderfs/binderfs_test.c         | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

--
2.34.1
diff mbox series

Patch

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)
 {