From: Bill Wendling <morbo@google.com>
To: Abhinav Saxena <xandfury@gmail.com>
Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
llvm@lists.linux.dev, Shuah Khan <shuah@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Justin Stitt <justinstitt@google.com>
Subject: Re: [RFC PATCH 3/4] selftests/binderfs: Macro argument 'fd' may be better as '(fd)' to avoid precedence issues
Date: Wed, 15 May 2024 11:32:48 -0700 [thread overview]
Message-ID: <CAGG=3QWyeWuLmJg1R7X8T4CsSgM+EiDToxjd_-8MD94D=DCLtw@mail.gmail.com> (raw)
In-Reply-To: <20240515010805.605511-4-xandfury@gmail.com>
On Tue, May 14, 2024 at 6:08 PM Abhinav Saxena <xandfury@gmail.com> wrote:
>
> Change the macro argument 'fd' to '(fd)' to avoid potential precedence
> issues. Without parentheses, the macro expansion could lead to
> unexpected behavior when used with an expression having different
> precedence levels.
>
> Example Code:
>
> #define CALC_SQR_BAD(x) (x*x)
> #define CALC_SQR_GOOD(x) ((x)*(x))
>
> CALC_SQR_BAD(2+3) expands to 2+(3*3)+2, giving 11 as the incorrect
> answer. Enclosing with parathesis CALC_SQR_GOOD(2+3) sets the correct
s/parathesis/parentheses/
> order of precedence and expands to (2+3)*(2+3), giving 25 as the
> correct answer.
>
> Signed-off-by: Abhinav Saxena <xandfury@gmail.com>
> ---
> .../testing/selftests/filesystems/binderfs/binderfs_test.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c
> index 4a149e3d4ba4..d5dba6c1e07f 100644
> --- a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c
> +++ b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c
> @@ -30,11 +30,11 @@
>
> #define close_prot_errno_disarm(fd) \
> do { \
> - if (fd >= 0) { \
> + if ((fd) >= 0) { \
> int _e_ = errno; \
> - close(fd); \
> + close((fd)); \
> errno = _e_; \
> - fd = -EBADF; \
> + (fd) = -EBADF; \
While I agree that it's generally good to add parentheses in macros,
this line ensures that 'fd' must be an lvalue and not an arithmetic
expression.
-bw
> } \
> } while (false)
>
> --
> 2.34.1
>
next prev parent reply other threads:[~2024-05-15 18:33 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 ` [RFC PATCH 2/4] selftests/binderfs: Update close_prot_errno_disarm macro to do{...}while(false) structure for safety Abhinav Saxena
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 [this message]
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='CAGG=3QWyeWuLmJg1R7X8T4CsSgM+EiDToxjd_-8MD94D=DCLtw@mail.gmail.com' \
--to=morbo@google.com \
--cc=justinstitt@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=shuah@kernel.org \
--cc=xandfury@gmail.com \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).