Skip to contentSkip to conversation

Fix scan-build warnings

PATCH Superseded Dominique Martinet 2 participants 13 messages Apr 27, 2023 – May 3, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dominique Martinet

author maintainer
#0 lore raw

cover Fix scan-build warnings

I ran scan-build very crudly on our source files, and there was at least one real bug so we might as well run it once in a while, in which case we probably ought to also fix the less important things hence this series.
In here the first patch is a real fix and the rest is low priority, the last one is arguably not an improvement and can be discussed (happy to just move the 0-initializations around to variable declaration which would also silence scan-build afaict)

Anyway, it can probably all wait until after this merge, sorry for the timing.

---
Dominique Martinet (5):
9p: fix ignored return value in v9fs_dir_release 9p: virtio: fix unlikely null pointer deref in handle_rerror 9p: virtio: make sure 'offs' is initialized in zc_request 9p: virtio: skip incrementing unused variable
9p: remove dead stores (variable set again without being read)

 fs/9p/vfs_dir.c        |  5 +++--
 fs/9p/vfs_inode.c      |  6 ------
 fs/9p/vfs_inode_dotl.c |  1 -
 net/9p/client.c        | 46 ++++++++++++----------------------------------
 net/9p/trans_virtio.c  |  8 ++++----
 5 files changed, 19 insertions(+), 47 deletions(-)

---
base-commit: 4eb3117888a923f6b9b1ad2dd093641c49a63ae5
change-id: 20230427-scan-build-d894c16fc945

Best regards,

Signature · 1 line
Dominique Martinet | Asmadeus
author maintainer
#1 lore raw

PATCH 1/5 9p: fix ignored return value in v9fs_dir_release

retval from filemap_fdatawrite was immediately overwritten by the following p9_fid_put: preserve any error in fdatawrite if there was any first.

This fixes the following scan-build warning:
fs/9p/vfs_dir.c:220:4: warning: Value stored to 'retval' is never read [deadcode.DeadStores]

                        retval = filemap_fdatawrite(inode->i_mapping);
                        ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 fs/9p/vfs_dir.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/9p/vfs_dir.c +3 -2
index 289b58cb896e..54bb99f12390 100644
@@ -209,7 +209,7 @@ int v9fs_dir_release(struct inode *inode, struct file *filp)
 	struct p9_fid *fid;
 	__le32 version;
 	loff_t i_size;
-	int retval = 0;
+	int retval = 0, put_err;
 
 	fid = filp->private_data;
 	p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
@@ -222,7 +222,8 @@ int v9fs_dir_release(struct inode *inode, struct file *filp)
 		spin_lock(&inode->i_lock);
 		hlist_del(&fid->ilist);
 		spin_unlock(&inode->i_lock);
-		retval = p9_fid_put(fid);
+		put_err = p9_fid_put(fid);
+		retval = retval < 0 ? retval : put_err;
 	}
 
 	if ((filp->f_mode & FMODE_WRITE)) {
Signature · 1 line
2.39.2
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] 9p: fix ignored return value in v9fs_dir_release

Quoted 8 lines · Dominique Martinet

retval from filemap_fdatawrite was immediately overwritten by the following p9_fid_put: preserve any error in fdatawrite if there was any first.

This fixes the following scan-build warning:
fs/9p/vfs_dir.c:220:4: warning: Value stored to 'retval' is never read [deadcode.DeadStores]

                        retval = filemap_fdatawrite(inode->i_mapping);
                        ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Perhaps:

Fixes 89c58cb395ec ("fs/9p: fix error reporting in v9fs_dir_release")
Quoted 1 line
author maintainer
#3 lore raw

Re: [PATCH 1/5] 9p: fix ignored return value in v9fs_dir_release

Thanks for all the reviews!

Simon Horman wrote on Tue, May 02, 2023 at 04:46:17PM +0200:

Quoted 5 lines · Dominique Martinet
Quoted 4 lines

retval from filemap_fdatawrite was immediately overwritten by the following p9_fid_put: preserve any error in fdatawrite if there was any first.

Quoted 8 lines
Quoted 4 lines

This fixes the following scan-build warning:
fs/9p/vfs_dir.c:220:4: warning: Value stored to 'retval' is never read [deadcode.DeadStores]

                        retval = filemap_fdatawrite(inode->i_mapping);
                        ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Perhaps:

Fixes 89c58cb395ec ("fs/9p: fix error reporting in v9fs_dir_release")

Right, this one warrants a fix tag as it's the only real bug in this series.
I'll add the Fixes and fix the typo in patch 5 and send a v2 later today.

Signature · 1 line
Dominique Martinet | Asmadeus
#4 lore raw

Re: [PATCH 1/5] 9p: fix ignored return value in v9fs_dir_release

Quoted 13 lines · Dominique Martinet

Thanks for all the reviews!

Simon Horman wrote on Tue, May 02, 2023 at 04:46:17PM +0200:

Quoted 10 lines · Dominique Martinet
Quoted 8 lines

retval from filemap_fdatawrite was immediately overwritten by the following p9_fid_put: preserve any error in fdatawrite if there was any first.

This fixes the following scan-build warning:
fs/9p/vfs_dir.c:220:4: warning: Value stored to 'retval' is never read [deadcode.DeadStores]

                        retval = filemap_fdatawrite(inode->i_mapping);
                        ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 6 lines
Quoted 3 lines

Perhaps:

Fixes 89c58cb395ec ("fs/9p: fix error reporting in v9fs_dir_release")

Right, this one warrants a fix tag as it's the only real bug in this series.

Hi Dominique,

Yes, I agree that this seems to be the only patch in this series that warrants a fixes tag.

author maintainer
#5 lore raw

PATCH 2/5 9p: virtio: fix unlikely null pointer deref in handle_rerror

In reply to Dominique Martinet

handle_rerror can dereference the pages pointer, but it is not necessarily set for small payloads.
In practice these should be filtered out by the size check, but might as well double-check explicitly.

This fixes the following scan-build warnings:
net/9p/trans_virtio.c:401:24: warning: Dereference of null pointer [core.NullDereference]

                memcpy_from_page(to, *pages++, offs, n);
                                     ^~~~~~~~
net/9p/trans_virtio.c:406:23: warning: Dereference of null pointer (loaded from variable 'pages') [core.NullDereference]
        memcpy_from_page(to, *pages, offs, size);
                             ^~~~~~
---
 net/9p/trans_virtio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/9p/trans_virtio.c +1 -1
index 3c27ffb781e3..2c9495ccda6b 100644
@@ -384,7 +384,7 @@ static void handle_rerror(struct p9_req_t *req, int in_hdr_len,
 	void *to = req->rc.sdata + in_hdr_len;
 
 	// Fits entirely into the static data?  Nothing to do.
-	if (req->rc.size < in_hdr_len)
+	if (req->rc.size < in_hdr_len || !pages)
 		return;
 
 	// Really long error message?  Tough, truncate the reply.  Might get
Signature · 1 line
2.39.2
Reviewed-by on v1 2/5
#6 lore raw

Re: [PATCH 2/5] 9p: virtio: fix unlikely null pointer deref in handle_rerror

Quoted 7 lines · Dominique Martinet

handle_rerror can dereference the pages pointer, but it is not necessarily set for small payloads.
In practice these should be filtered out by the size check, but might as well double-check explicitly.

This fixes the following scan-build warnings:
net/9p/trans_virtio.c:401:24: warning: Dereference of null pointer [core.NullDereference]

Quoted 7 lines
                memcpy_from_page(to, *pages++, offs, n);
                                     ^~~~~~~~
net/9p/trans_virtio.c:406:23: warning: Dereference of null pointer (loaded from variable 'pages') [core.NullDereference]
        memcpy_from_page(to, *pages, offs, size);
                             ^~~~~~
author maintainer
#7 lore raw

PATCH 3/5 9p: virtio: make sure 'offs' is initialized in zc_request

In reply to Dominique Martinet

Similarly to the previous patch: offs can be used in handle_rerrors without initializing on small payloads; in this case handle_rerrors will not use it because of the size check, but it doesn't hurt to make sure it is zero to please scan-build.

This fixes the following warning:
net/9p/trans_virtio.c:539:3: warning: 3rd function call argument is an uninitialized value [core.CallAndMessage]

                handle_rerror(req, in_hdr_len, offs, in_pages);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 net/9p/trans_virtio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/9p/trans_virtio.c +1 -1
index 2c9495ccda6b..f3f678289423 100644
@@ -428,7 +428,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
 	struct page **in_pages = NULL, **out_pages = NULL;
 	struct virtio_chan *chan = client->trans;
 	struct scatterlist *sgs[4];
-	size_t offs;
+	size_t offs = 0;
 	int need_drop = 0;
 	int kicked = 0;
 
Signature · 1 line
2.39.2
Reviewed-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] 9p: virtio: make sure 'offs' is initialized in zc_request

Quoted 11 lines · Dominique Martinet

Similarly to the previous patch: offs can be used in handle_rerrors without initializing on small payloads; in this case handle_rerrors will not use it because of the size check, but it doesn't hurt to make sure it is zero to please scan-build.

This fixes the following warning:
net/9p/trans_virtio.c:539:3: warning: 3rd function call argument is an uninitialized value [core.CallAndMessage]

                handle_rerror(req, in_hdr_len, offs, in_pages);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
author maintainer
#9 lore raw

PATCH 4/5 9p: virtio: skip incrementing unused variable

In reply to Dominique Martinet

Fix the following scan-build warning:
net/9p/trans_virtio.c:504:3: warning: Value stored to 'in' is never read [deadcode.DeadStores]

                in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
                ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm honestly not 100% sure about this one; I'm tempted to think we could (should?) just check the return value of pack_sg_list_p to skip the in_sgs++ and setting sgs[] if it didn't process anything, but I'm not sure it should ever happen so this is probably fine as is.

Just removing the assignment at least makes it clear the return value isn't used, so it's an improvement in terms of readability.

---
 net/9p/trans_virtio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/trans_virtio.c +2 -2
index f3f678289423..e305071eb7b8 100644
@@ -501,8 +501,8 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
 
 	if (in_pages) {
 		sgs[out_sgs + in_sgs++] = chan->sg + out + in;
-		in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
-				     in_pages, in_nr_pages, offs, inlen);
+		pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
+			       in_pages, in_nr_pages, offs, inlen);
 	}
 
 	BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
Signature · 1 line
2.39.2
Reviewed-by on v1 4/5
#10 lore raw

Re: [PATCH 4/5] 9p: virtio: skip incrementing unused variable

Quoted 10 lines · Dominique Martinet

Fix the following scan-build warning:
net/9p/trans_virtio.c:504:3: warning: Value stored to 'in' is never read [deadcode.DeadStores]

                in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
                ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm honestly not 100% sure about this one; I'm tempted to think we could (should?) just check the return value of pack_sg_list_p to skip the in_sgs++ and setting sgs[] if it didn't process anything, but I'm not sure it should ever happen so this is probably fine as is.

Quoted 4 lines

Just removing the assignment at least makes it clear the return value isn't used, so it's an improvement in terms of readability.

author maintainer
#11 lore raw

PATCH 5/5 9p: remove dead stores (variable set again without being read)

In reply to Dominique Martinet

The 9p code for some reason used to initialize variables outside of the declaration, e.g. instead of just initializing the variable like this:

int retval = 0

We would be doing this:

int retval;
retval = 0;

This is perfectly fine and the compiler will just optimize dead stores
anyway, but scan-build seems to think this is a problem and there are
many of these warnings making the output of scan-build full of such
warnings:
fs/9p/vfs_inode.c:916:2: warning: Value stored to 'retval' is never read [deadcode.DeadStores]

        retval = 0;
        ^        ~

I have no strong opinion here, but if we want to regularily run scan-build we should fix these just to silence the messages.

I've confirmed these all are indeed ok to remove.

---
 fs/9p/vfs_inode.c      |  6 ------
 fs/9p/vfs_inode_dotl.c |  1 -
 net/9p/client.c        | 46 ++++++++++++----------------------------------
 3 files changed, 12 insertions(+), 41 deletions(-)
fs/9p/vfs_inode.c +0 -6
index 54dfe4f10f43..82589d1d9bbe 100644
@@ -164,7 +164,6 @@ int v9fs_uflags2omode(int uflags, int extended)
 {
 	int ret;
 
-	ret = 0;
 	switch (uflags&3) {
 	default:
 	case O_RDONLY:
@@ -604,7 +603,6 @@ v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
 
 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
 
-	err = 0;
 	name = dentry->d_name.name;
 	dfid = v9fs_parent_fid(dentry);
 	if (IS_ERR(dfid)) {
@@ -816,8 +814,6 @@ v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
 	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
 		return finish_no_open(file, res);
 
-	err = 0;
-
 	v9ses = v9fs_inode2v9ses(dir);
 	perm = unixmode2p9mode(v9ses, mode);
 	p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
@@ -913,7 +909,6 @@ v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 		return -EINVAL;
 
 	p9_debug(P9_DEBUG_VFS, "\n");
-	retval = 0;
 	old_inode = d_inode(old_dentry);
 	new_inode = d_inode(new_dentry);
 	v9ses = v9fs_inode2v9ses(old_inode);
@@ -1067,7 +1062,6 @@ static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
 	if (retval)
 		return retval;
 
-	retval = -EPERM;
 	v9ses = v9fs_dentry2v9ses(dentry);
 	if (iattr->ia_valid & ATTR_FILE) {
 		fid = iattr->ia_file->private_data;
fs/9p/vfs_inode_dotl.c +0 -1
index f9371b5b70ea..df7023af17cf 100644
@@ -367,7 +367,6 @@ static int v9fs_vfs_mkdir_dotl(struct mnt_idmap *idmap,
 	struct posix_acl *dacl = NULL, *pacl = NULL;
 
 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
-	err = 0;
 	v9ses = v9fs_inode2v9ses(dir);
 
 	omode |= S_IFDIR;
net/9p/client.c +12 -34

235 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v1 5/5
#12 lore raw

Re: [PATCH 5/5] 9p: remove dead stores (variable set again without being read)

Quoted 15 lines · Dominique Martinet

The 9p code for some reason used to initialize variables outside of the declaration, e.g. instead of just initializing the variable like this:

int retval = 0

We would be doing this:

int retval;
retval = 0;

This is perfectly fine and the compiler will just optimize dead stores
anyway, but scan-build seems to think this is a problem and there are
many of these warnings making the output of scan-build full of such
warnings:
fs/9p/vfs_inode.c:916:2: warning: Value stored to 'retval' is never read [deadcode.DeadStores]

Quoted 4 lines
        retval = 0;
        ^        ~

I have no strong opinion here, but if we want to regularily run

s/regularily/regularly/

Quoted 3 lines

scan-build we should fix these just to silence the messages.

I've confirmed these all are indeed ok to remove.

Likewise, these look good to me.

Quoted 1 line