seq_file: fix NULL pointer arithmetic warning
PATCH v3 1/1 seq_file: fix NULL pointer arithmetic warning
Implement conditional logic in order to replace NULL pointer arithmetic.
The use of NULL pointer arithmetic was pointed out by clang with the following warning:
fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
return NULL + !*ppos;
~~~~ ^
fs/seq_file.c:559:14: warning: performing pointer arithmetic on a
null pointer has undefined behavior [-Wnull-pointer-arithmetic]
return NULL + (*pos == 0);---
V1 -> V2:
- Use SEQ_START_TOKEN instead of open-coding it
- kernfs_seq_start call single_start instead of open-coding it V2 -> V3:
- Remove the EXPORT of the single_start symbol
--- fs/kernfs/file.c | 7 +------ fs/seq_file.c | 4 ++-- include/linux/seq_file.h | 1 + 3 files changed, 4 insertions(+), 8 deletions(-)
fs/kernfs/file.c +1 -6
@@ -120,13 +120,8 @@ static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)if (next ==ERR_PTR (-ENODEV ))kernfs_seq_stop_active (sf, next);return next; - }else { -/* -* The same behavior and code as single_open(). Returns -* !NULL if pos is at the beginning; otherwise, NULL. -*/ -return NULL + !*ppos; } +return single_start (sf, ppos); }static void *kernfs_seq_next (struct seq_file *sf,void *v,loff_t *ppos)
fs/seq_file.c +2 -2
@@ -554,9 +554,9 @@ int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) }EXPORT_SYMBOL (seq_dentry); -static void *single_start (struct seq_file *p,loff_t *pos) +void *single_start (struct seq_file *p,loff_t *pos) { -return NULL + (*pos ==0 ); +return *pos ?NULL :SEQ_START_TOKEN ; }static void *single_next (struct seq_file *p,void *v,loff_t *pos)
include/linux/seq_file.h +1 -0
@@ -162,6 +162,7 @@ int seq_dentry(struct seq_file *, struct dentry *, const char *);int seq_path_root (struct seq_file *m,const struct path *path,const struct path *root,const char *esc); +void *single_start (struct seq_file *,loff_t *);int single_open (struct file *,int (*)(struct seq_file *,void *),void *);int single_open_size (struct file *,int (*)(struct seq_file *,void *),void *,size_t );int single_release (struct inode *,struct file *);
Signature · 1 line
2.34.1
Hi Maíra,
Quoted 7 lines · Maíra Canal
Implement conditional logic in order to replace NULL pointer arithmetic.
The use of NULL pointer arithmetic was pointed out by clang with the following warning:
fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
Quoted 7 lines
return NULL + !*ppos; ~~~~ ^ fs/seq_file.c:559:14: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + (*pos == 0);Signed-off-by Maíra Canal <[email protected]>
Thanks a lot for the patch!
It looks like Arnd sent a similar patch but it was never picked up:
https://lore.kernel.org/r/[email protected]/
This is a cleaner solution as it uses existing defines and functions to deduplicate the code.
Quoted 62 lines
---
V1 -> V2:
- UseSEQ_START_TOKENinstead of open-coding it
-kernfs_seq_startcallsingle_startinstead of open-coding it V2 -> V3:
- Remove the EXPORT of thesingle_startsymbol--- fs/kernfs/file.c | 7 +------ fs/seq_file.c | 4 ++-- include/linux/seq_file.h | 1 + 3 files changed, 4 insertions(+), 8 deletions(-)fs/kernfs/file.c +1
-6@@ -120,13 +120,8 @@ static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)if (next ==ERR_PTR (-ENODEV ))kernfs_seq_stop_active (sf, next);return next; - }else { -/* -* The same behavior and code as single_open(). Returns -* !NULL if pos is at the beginning; otherwise, NULL. -*/ -return NULL + !*ppos; } +return single_start (sf, ppos); }static void *kernfs_seq_next (struct seq_file *sf,void *v,loff_t *ppos)fs/seq_file.c +2
-2@@ -554,9 +554,9 @@ int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) }EXPORT_SYMBOL (seq_dentry); -static void *single_start (struct seq_file *p,loff_t *pos) +void *single_start (struct seq_file *p,loff_t *pos) { -return NULL + (*pos ==0 ); +return *pos ?NULL :SEQ_START_TOKEN ; }static void *single_next (struct seq_file *p,void *v,loff_t *pos)include/linux/seq_file.h +1
-0@@ -162,6 +162,7 @@ int seq_dentry(struct seq_file *, struct dentry *, const char *);int seq_path_root (struct seq_file *m,const struct path *path,const struct path *root,const char *esc); +void *single_start (struct seq_file *,loff_t *);int single_open (struct file *,int (*)(struct seq_file *,void *),void *);int single_open_size (struct file *,int (*)(struct seq_file *,void *),void *,size_t );int single_release (struct inode *,struct file *);Signature · 1 line
2.34.1
Quoted 7 lines · Maíra Canal
Implement conditional logic in order to replace NULL pointer arithmetic.
The use of NULL pointer arithmetic was pointed out by clang with the following warning:
fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
Quoted 7 lines
return NULL + !*ppos; ~~~~ ^ fs/seq_file.c:559:14: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + (*pos == 0);Signed-off-by Maíra Canal <[email protected]>
Quoted 13 lines · Maíra Canal
Implement conditional logic in order to replace NULL pointer arithmetic.
The use of NULL pointer arithmetic was pointed out by clang with the following warning:
fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]return NULL + !*ppos; ~~~~ ^ fs/seq_file.c:559:14: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + (*pos == 0);
Quoted 7 lines
Signed-off-by Maíra Canal <[email protected]>---
V1 -> V2:
- UseSEQ_START_TOKENinstead of open-coding it
-kernfs_seq_startcallsingle_startinstead of open-coding it V2 -> V3:
- Remove the EXPORT of thesingle_startsymbol
Applied.