From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
David Howells <dhowells@redhat.com>,
Ricardo Martinez <ricardo.martinez@linux.intel.com>
Subject: [RFC PATCH 2/4] list.h: Fix parentheses around macro pointer parameter use
Date: Wed, 3 May 2023 21:29:12 -0400 [thread overview]
Message-ID: <20230504012914.1797355-2-mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <20230504012914.1797355-1-mathieu.desnoyers@efficios.com>
Add missing parentheses around use of macro argument "pos" in those
patterns to ensure operator precedence behaves as expected:
- typeof(*pos)
- pos->member
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.
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 */
//...
}
}
// 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.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Ricardo Martinez <ricardo.martinez@linux.intel.com>
---
include/linux/list.h | 44 ++++++++++++++++++++++----------------------
1 file changed, 22 insertions(+), 22 deletions(-)
diff --git a/include/linux/list.h b/include/linux/list.h
index f10344dbad4d..bb106238eaf6 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -603,7 +603,7 @@ static inline void list_splice_tail_init(struct list_head *list,
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
- for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
+ for (pos = (head)->next; !list_is_head(pos, head); pos = (pos)->next)
/**
* list_for_each_rcu - Iterate over a list in an RCU-safe fashion
@@ -612,8 +612,8 @@ static inline void list_splice_tail_init(struct list_head *list,
*/
#define list_for_each_rcu(pos, head) \
for (pos = rcu_dereference((head)->next); \
- !list_is_head(pos, (head)); \
- pos = rcu_dereference(pos->next))
+ !list_is_head(pos, head); \
+ pos = rcu_dereference((pos)->next))
/**
* list_for_each_continue - continue iteration over a list
@@ -623,7 +623,7 @@ static inline void list_splice_tail_init(struct list_head *list,
* Continue to iterate over a list, continuing after the current position.
*/
#define list_for_each_continue(pos, head) \
- for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
+ for (pos = (pos)->next; !list_is_head(pos, head); pos = (pos)->next)
/**
* list_for_each_prev - iterate over a list backwards
@@ -631,7 +631,7 @@ static inline void list_splice_tail_init(struct list_head *list,
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
- for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
+ for (pos = (head)->prev; !list_is_head(pos, head); pos = (pos)->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
@@ -640,9 +640,9 @@ static inline void list_splice_tail_init(struct list_head *list,
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
- for (pos = (head)->next, n = pos->next; \
- !list_is_head(pos, (head)); \
- pos = n, n = pos->next)
+ for (pos = (head)->next, n = (pos)->next; \
+ !list_is_head(pos, head); \
+ pos = n, n = (pos)->next)
/**
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
@@ -651,9 +651,9 @@ static inline void list_splice_tail_init(struct list_head *list,
* @head: the head for your list.
*/
#define list_for_each_prev_safe(pos, n, head) \
- for (pos = (head)->prev, n = pos->prev; \
- !list_is_head(pos, (head)); \
- pos = n, n = pos->prev)
+ for (pos = (head)->prev, n = (pos)->prev; \
+ !list_is_head(pos, head); \
+ pos = n, n = (pos)->prev)
/**
* list_count_nodes - count nodes in the list
@@ -677,7 +677,7 @@ static inline size_t list_count_nodes(struct list_head *head)
* @member: the name of the list_head within the struct.
*/
#define list_entry_is_head(pos, head, member) \
- (&pos->member == (head))
+ (&(pos)->member == (head))
/**
* list_for_each_entry - iterate over list of given type
@@ -686,7 +686,7 @@ static inline size_t list_count_nodes(struct list_head *head)
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
- for (pos = list_first_entry(head, typeof(*pos), member); \
+ for (pos = list_first_entry(head, typeof(*(pos)), member); \
!list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))
@@ -697,7 +697,7 @@ static inline size_t list_count_nodes(struct list_head *head)
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
- for (pos = list_last_entry(head, typeof(*pos), member); \
+ for (pos = list_last_entry(head, typeof(*(pos)), member); \
!list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
@@ -710,7 +710,7 @@ static inline size_t list_count_nodes(struct list_head *head)
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
*/
#define list_prepare_entry(pos, head, member) \
- ((pos) ? : list_entry(head, typeof(*pos), member))
+ ((pos) ? : list_entry(head, typeof(*(pos)), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
@@ -773,7 +773,7 @@ static inline size_t list_count_nodes(struct list_head *head)
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
- for (pos = list_first_entry(head, typeof(*pos), member), \
+ for (pos = list_first_entry(head, typeof(*(pos)), member), \
n = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
@@ -820,7 +820,7 @@ static inline size_t list_count_nodes(struct list_head *head)
* of list entry.
*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
- for (pos = list_last_entry(head, typeof(*pos), member), \
+ for (pos = list_last_entry(head, typeof(*(pos)), member), \
n = list_prev_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_prev_entry(n, member))
@@ -1033,10 +1033,10 @@ static inline void hlist_move_list(struct hlist_head *old,
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each(pos, head) \
- for (pos = (head)->first; pos ; pos = pos->next)
+ for (pos = (head)->first; pos ; pos = (pos)->next)
#define hlist_for_each_safe(pos, n, head) \
- for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
+ for (pos = (head)->first; pos && ({ n = (pos)->next; 1; }); \
pos = n)
#define hlist_entry_safe(ptr, type, member) \
@@ -1082,8 +1082,8 @@ static inline void hlist_move_list(struct hlist_head *old,
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(pos, n, head, member) \
- for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
- pos && ({ n = pos->member.next; 1; }); \
- pos = hlist_entry_safe(n, typeof(*pos), member))
+ for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
+ pos && ({ n = (pos)->member.next; 1; }); \
+ pos = hlist_entry_safe(n, typeof(*(pos)), member))
#endif
--
2.25.1
next prev parent reply other threads:[~2023-05-04 1:29 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-04 1:29 [RFC PATCH 1/4] rcu: rcupdate.h: Add missing parentheses around macro pointer dereference Mathieu Desnoyers
2023-05-04 1:29 ` Mathieu Desnoyers [this message]
2023-05-04 14:41 ` [RFC PATCH 2/4] list.h: Fix parentheses around macro pointer parameter use Andy Shevchenko
2023-05-04 14:45 ` Mathieu Desnoyers
2023-05-04 1:29 ` [RFC PATCH 3/4] rculist.h: " Mathieu Desnoyers
2023-05-04 16:19 ` Joel Fernandes
2023-05-05 14:06 ` Steven Rostedt
2023-05-05 14:35 ` Joel Fernandes
2023-05-05 15:02 ` Steven Rostedt
2023-05-04 1:29 ` [RFC PATCH 4/4] llist.h: " Mathieu Desnoyers
2023-05-04 5:54 ` Huang, Ying
2023-05-04 14:54 ` Mathieu Desnoyers
2023-05-04 17:16 ` Linus Torvalds
2023-05-05 1:38 ` Huang, Ying
2023-05-05 14:23 ` Mathieu Desnoyers
2023-05-05 18:08 ` Linus Torvalds
2023-05-06 1:12 ` Huang, Ying
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=20230504012914.1797355-2-mathieu.desnoyers@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=dhowells@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ricardo.martinez@linux.intel.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
all inboxes | Powered by JetHome®