Skip to contentSkip to conversation

sparse: Use offsetof macro to silence null ptr subtraction warning

PATCH In review Richard Palethorpe 2 participants 2 messages Mar 21, 2022 – May 21, 2022 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparse: Use offsetof macro to silence null ptr subtraction warning

Subtracting (char *)0 is undefined behavior. Newer compilers warn about this unless it is done in system headers.

---
 token.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
token.h +1 -1
index bccac0e4..9000e0cb 100644
@@ -201,7 +201,7 @@ struct token {
 
 static inline struct token *containing_token(struct token **p)
 {
-	void *addr = (char *)p - ((char *)&((struct token *)0)->next - (char *)0);
+	void *addr = (char *)p - offsetof(struct token, next);
 	return addr;
 }
 
Signature · 1 line
2.35.1
#1 lore raw
Quoted 2 lines · Richard Palethorpe

Subtracting (char *)0 is undefined behavior. Newer compilers warn about this unless it is done in system headers.

Thanks you, pushed now.

-- Luc