All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] seq_file: fix NULL pointer arithmetic warning
@ 2022-01-30 15:27 Maíra Canal
  2022-01-30 17:22 ` Al Viro
  0 siblings, 1 reply; 2+ messages in thread
From: Maíra Canal @ 2022-01-30 15:27 UTC (permalink / raw)
  To: gregkh, tj, viro, nathan, ndesaulniers; +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>
---
 fs/kernfs/file.c | 6 ++++--
 fs/seq_file.c    | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 9414a7a60a9f..3a6990c7fe8e 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -120,12 +120,14 @@ 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 {
+	} else if (*ppos) {
 		/*
 		 * The same behavior and code as single_open().  Returns
 		 * !NULL if pos is at the beginning; otherwise, NULL.
 		 */
-		return NULL + !*ppos;
+		return NULL;
+	} else {
+		return (void *) 1;
 	}
 }
 
diff --git a/fs/seq_file.c b/fs/seq_file.c
index f8e1f4ee87ff..7b6165d5d829 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -556,7 +556,7 @@ EXPORT_SYMBOL(seq_dentry);
 
 static void *single_start(struct seq_file *p, loff_t *pos)
 {
-	return NULL + (*pos == 0);
+	return *pos ? NULL : (void *) 1;
 }
 
 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
-- 
2.34.1


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

* Re: [PATCH] seq_file: fix NULL pointer arithmetic warning
  2022-01-30 15:27 [PATCH] seq_file: fix NULL pointer arithmetic warning Maíra Canal
@ 2022-01-30 17:22 ` Al Viro
  0 siblings, 0 replies; 2+ messages in thread
From: Al Viro @ 2022-01-30 17:22 UTC (permalink / raw)
  To: Maíra Canal
  Cc: gregkh, tj, nathan, ndesaulniers, linux-kernel, linux-fsdevel, llvm

On Sun, Jan 30, 2022 at 12:27:00PM -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);

NAK.  _If_ you want to bother with that at all, at least make it
use SEQ_START_TOKEN, rather than open-coding it; what's more,
kernfs_seq_start() should simply call single_start() instead of
open-coding it.  I.e.
	if (ops->seq_start) {
		void *next = ops->seq_start(sf, ppos);
		/* see the comment above kernfs_seq_stop_active() */
		if (next == ERR_PTR(-ENODEV))
			kernfs_seq_stop_active(sf, next);
		return next;
	} else {
		return single_start(sf, ppos);
	}
and
	return *ppos ? NULL : SEQ_START_TOKEN;
in single_start() itself.

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

end of thread, other threads:[~2022-01-30 17:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-30 15:27 [PATCH] seq_file: fix NULL pointer arithmetic warning Maíra Canal
2022-01-30 17:22 ` 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.