Re: [RFC PATCH 05/13] list.h: Fix parentheses around macro pointer parameter use

From: Andy Shevchenko
Date: Mon May 08 2023 - 08:16:50 EST


On Thu, May 04, 2023 at 04:05:19PM -0400, Mathieu Desnoyers wrote:
> Add missing parentheses around use of macro argument "pos" in those
> patterns to ensure operator precedence behaves as expected:
>
> - typeof(*pos)
> - pos->member
> - "x = y" is changed for "x = (y)", because "y" can be an expression
> containing a comma if it is the result of the expansion of a macro such
> as #define eval(...) __VA_ARGS__, which would cause unexpected operator
> precedence. This use-case is far-fetched, but we have to choose one
> way or the other (with or without parentheses) for consistency,
> - x && y is changed for (x) && (y).
>
> Remove useless parentheses around use of macro parameter (head) in the
> following pattern:
>
> - list_is_head(pos, (head))
>
> Because comma is the lowest priority operator already, so the extra pair
> of parentheses is redundant.

But strictly speaking it might be something like

list_...(..., (a, b))

where (a, b) is the head. No?

> This corrects the following usage pattern where operator precedence is
> unexpected:
>
> LIST_HEAD(testlist);
>
> struct test {
> struct list_head node;
> int a;
> };
>
> // pos->member issue
> void f(void)
> {
> struct test *t1;
> struct test **t2 = &t1;
>
> list_for_each_entry((*t2), &testlist, node) { /* works */
> //...
> }
> list_for_each_entry(*t2, &testlist, node) { /* broken */
> //...

Me still in doubt. But it's up to maintainers.

> }
> }
>
> // typeof(*pos) issue
> void f2(void)
> {
> struct test *t1 = NULL, *t2;
>
> t2 = list_prepare_entry((0 + t1), &testlist, node); /* works */
> t2 = list_prepare_entry(0 + t1, &testlist, node); /* broken */
> }
>
> Note that the macros in which "pos" is also used as an lvalue probably
> don't suffer from the lack of parentheses around "pos" in typeof(*pos),
> but add those nevertheless to keep everything consistent.

--
With Best Regards,
Andy Shevchenko