All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] seq_file: fix NULL pointer arithmetic warning
@ 2022-01-31 18:22 Maíra Canal
  2022-02-01 16:26 ` Nathan Chancellor
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Maíra Canal @ 2022-01-31 18:22 UTC (permalink / raw)
  To: gregkh, tj, viro, nathan, ndesaulniers, willy
  Cc: linux-kernel, linux-fsdevel, llvm

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);

Signed-off-by: Maíra Canal <maira.canal@usp.br>
---
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(-)

diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 9414a7a60a9f..7aefaca876a0 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -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)
diff --git a/fs/seq_file.c b/fs/seq_file.c
index f8e1f4ee87ff..7ab8a58c29b6 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -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)
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 88cc16444b43..60820ab511d2 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -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 *);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] seq_file: fix NULL pointer arithmetic warning
  2022-01-31 18:22 [PATCH v3] seq_file: fix NULL pointer arithmetic warning Maíra Canal
@ 2022-02-01 16:26 ` Nathan Chancellor
  2022-02-01 16:30 ` Matthew Wilcox
  2022-02-01 16:32 ` Al Viro
  2 siblings, 0 replies; 4+ messages in thread
From: Nathan Chancellor @ 2022-02-01 16:26 UTC (permalink / raw)
  To: Maíra Canal
  Cc: gregkh, tj, viro, ndesaulniers, willy, linux-kernel, linux-fsdevel, llvm

Hi Maíra,

On Mon, Jan 31, 2022 at 03:22:42PM -0300, Maíra Canal wrote:
> 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);
> 
> Signed-off-by: Maíra Canal <maira.canal@usp.br>

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/20201028151202.3074398-1-arnd@kernel.org/

This is a cleaner solution as it uses existing defines and functions to
deduplicate the code.

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

> ---
> 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(-)
> 
> diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
> index 9414a7a60a9f..7aefaca876a0 100644
> --- a/fs/kernfs/file.c
> +++ b/fs/kernfs/file.c
> @@ -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)
> diff --git a/fs/seq_file.c b/fs/seq_file.c
> index f8e1f4ee87ff..7ab8a58c29b6 100644
> --- a/fs/seq_file.c
> +++ b/fs/seq_file.c
> @@ -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)
> diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
> index 88cc16444b43..60820ab511d2 100644
> --- a/include/linux/seq_file.h
> +++ b/include/linux/seq_file.h
> @@ -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 *);
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] seq_file: fix NULL pointer arithmetic warning
  2022-01-31 18:22 [PATCH v3] seq_file: fix NULL pointer arithmetic warning Maíra Canal
  2022-02-01 16:26 ` Nathan Chancellor
@ 2022-02-01 16:30 ` Matthew Wilcox
  2022-02-01 16:32 ` Al Viro
  2 siblings, 0 replies; 4+ messages in thread
From: Matthew Wilcox @ 2022-02-01 16:30 UTC (permalink / raw)
  To: Maíra Canal
  Cc: gregkh, tj, viro, nathan, ndesaulniers, linux-kernel,
	linux-fsdevel, llvm

On Mon, Jan 31, 2022 at 03:22:42PM -0300, Maíra Canal wrote:
> 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);
> 
> Signed-off-by: Maíra Canal <maira.canal@usp.br>

Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] seq_file: fix NULL pointer arithmetic warning
  2022-01-31 18:22 [PATCH v3] seq_file: fix NULL pointer arithmetic warning Maíra Canal
  2022-02-01 16:26 ` Nathan Chancellor
  2022-02-01 16:30 ` Matthew Wilcox
@ 2022-02-01 16:32 ` Al Viro
  2 siblings, 0 replies; 4+ messages in thread
From: Al Viro @ 2022-02-01 16:32 UTC (permalink / raw)
  To: Maíra Canal
  Cc: gregkh, tj, nathan, ndesaulniers, willy, linux-kernel,
	linux-fsdevel, llvm

On Mon, Jan 31, 2022 at 03:22:42PM -0300, Maíra Canal wrote:
> 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);
> 
> Signed-off-by: Maíra Canal <maira.canal@usp.br>
> ---
> 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

Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-01 16:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-31 18:22 [PATCH v3] seq_file: fix NULL pointer arithmetic warning Maíra Canal
2022-02-01 16:26 ` Nathan Chancellor
2022-02-01 16:30 ` Matthew Wilcox
2022-02-01 16:32 ` Al Viro

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.