2024-05-10 20:24:42

by Sergey Shtylyov

[permalink] [raw]
Subject: [PATCH] nfs: fix undefined behavior in nfs_block_bits()

Shifting *signed int* typed constant 1 left by 31 bits causes undefined
behavior. Specify the correct *unsigned long* type by using 1UL instead.

Found by Linux Verification Center (linuxtesting.org) with the Svace static
analysis tool.

Cc: [email protected]
Signed-off-by: Sergey Shtylyov <[email protected]>

---
This patch is against the master branch of Trond Myklebust's linux-nfs.git repo.

fs/nfs/internal.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

Index: linux-nfs/fs/nfs/internal.h
===================================================================
--- linux-nfs.orig/fs/nfs/internal.h
+++ linux-nfs/fs/nfs/internal.h
@@ -710,9 +710,9 @@ unsigned long nfs_block_bits(unsigned lo
if ((bsize & (bsize - 1)) || nrbitsp) {
unsigned char nrbits;

- for (nrbits = 31; nrbits && !(bsize & (1 << nrbits)); nrbits--)
+ for (nrbits = 31; nrbits && !(bsize & (1UL << nrbits)); nrbits--)
;
- bsize = 1 << nrbits;
+ bsize = 1UL << nrbits;
if (nrbitsp)
*nrbitsp = nrbits;
}


2024-05-13 11:17:28

by Benjamin Coddington

[permalink] [raw]
Subject: Re: [PATCH] nfs: fix undefined behavior in nfs_block_bits()

On 10 May 2024, at 16:24, Sergey Shtylyov wrote:

> Shifting *signed int* typed constant 1 left by 31 bits causes undefined
> behavior. Specify the correct *unsigned long* type by using 1UL instead.
>
> Found by Linux Verification Center (linuxtesting.org) with the Svace static
> analysis tool.
>
> Cc: [email protected]
> Signed-off-by: Sergey Shtylyov <[email protected]>

Reviewed-by: Benjamin Coddington <[email protected]>

Ben