public inbox for llvm@lists.linux.dev
 help / color / mirror / Atom feed
From: Abhinav Saxena <xandfury@gmail.com>
To: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	llvm@lists.linux.dev
Cc: Shuah Khan <shuah@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	Abhinav Saxena <xandfury@gmail.com>
Subject: [RFC PATCH 2/4] selftests/binderfs: Update close_prot_errno_disarm macro to do{...}while(false) structure for safety
Date: Wed, 15 May 2024 01:08:03 +0000	[thread overview]
Message-ID: <20240515010805.605511-3-xandfury@gmail.com> (raw)
In-Reply-To: <20240515010805.605511-1-xandfury@gmail.com>

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

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


  parent reply	other threads:[~2024-05-15  1:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-15  1:08 [RFC PATCH 0/4] selftests/binderfs: Fixes to binderfs_test Abhinav Saxena
2024-05-15  1:08 ` [RFC PATCH 1/4] selftests/binderfs: Run clang-format on binderfs test Abhinav Saxena
2024-05-15  1:08 ` Abhinav Saxena [this message]
2024-05-15  1:08 ` [RFC PATCH 3/4] selftests/binderfs: Macro argument 'fd' may be better as '(fd)' to avoid precedence issues Abhinav Saxena
2024-05-15 18:32   ` Bill Wendling
2024-05-15  1:08 ` [RFC PATCH 4/4] selftests/binderfs: Add missing a blank line after declarations Abhinav Saxena
2024-05-15  4:27 ` [RFC PATCH 0/4] selftests/binderfs: Fixes to binderfs_test Abhinav Saxena

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240515010805.605511-3-xandfury@gmail.com \
    --to=xandfury@gmail.com \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox