linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Maíra Canal" <maira.canal@usp.br>
To: gregkh@linuxfoundation.org, tj@kernel.org,
	viro@zeniv.linux.org.uk, nathan@kernel.org,
	ndesaulniers@google.com
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	llvm@lists.linux.dev
Subject: [PATCH] seq_file: fix NULL pointer arithmetic warning
Date: Sun, 30 Jan 2022 12:27:00 -0300	[thread overview]
Message-ID: <YfauRJpYiAT3yEnK@fedora> (raw)

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


             reply	other threads:[~2022-01-30 15:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-30 15:27 Maíra Canal [this message]
2022-01-30 17:22 ` [PATCH] seq_file: fix NULL pointer arithmetic warning Al Viro

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=YfauRJpYiAT3yEnK@fedora \
    --to=maira.canal@usp.br \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).