* [PATCH] afs: use BIT_ULL for shifting to fix integer overflow
@ 2019-08-30 10:49 Colin King
2019-08-30 14:01 ` David Howells
0 siblings, 1 reply; 3+ messages in thread
From: Colin King @ 2019-08-30 10:49 UTC (permalink / raw)
To: David Howells, linux-afs; +Cc: kernel-janitors, linux-kernel
From: Colin Ian King <colin.king@canonical.com>
The expression 1 << nr_slots is evaluated with 32 bit integer arithmetic
and can overflow before it is widened. Instead, use BIT_ULL to avoid
overflow.
Addresses-Coverity: ("Unintentional integer overflow")
Fixes: 63a4681ff39c ("afs: Locally edit directory data for mkdir/create/unlink/...")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
fs/afs/dir_edit.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/fs/afs/dir_edit.c b/fs/afs/dir_edit.c
index d4fbe5f85f1b..f360119923aa 100644
--- a/fs/afs/dir_edit.c
+++ b/fs/afs/dir_edit.c
@@ -36,7 +36,7 @@ static int afs_find_contig_bits(union afs_xdr_dir_block *block, unsigned int nr_
bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
bitmap >>= 1; /* The first entry is metadata */
bit = 1;
- mask = (1 << nr_slots) - 1;
+ mask = BIT_ULL(nr_slots) - 1;
do {
if (sizeof(unsigned long) == 8)
@@ -70,7 +70,7 @@ static void afs_set_contig_bits(union afs_xdr_dir_block *block,
{
u64 mask, before, after;
- mask = (1 << nr_slots) - 1;
+ mask = BIT_ULL(nr_slots) - 1;
mask <<= bit;
before = *(u64 *)block->hdr.bitmap;
@@ -95,7 +95,7 @@ static void afs_clear_contig_bits(union afs_xdr_dir_block *block,
{
u64 mask, before, after;
- mask = (1 << nr_slots) - 1;
+ mask = BIT_ULL(nr_slots) - 1;
mask <<= bit;
before = *(u64 *)block->hdr.bitmap;
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] afs: use BIT_ULL for shifting to fix integer overflow
2019-08-30 10:49 [PATCH] afs: use BIT_ULL for shifting to fix integer overflow Colin King
@ 2019-08-30 14:01 ` David Howells
2019-08-30 14:06 ` Colin Ian King
0 siblings, 1 reply; 3+ messages in thread
From: David Howells @ 2019-08-30 14:01 UTC (permalink / raw)
To: Colin King; +Cc: dhowells, linux-afs, kernel-janitors, linux-kernel
Colin King <colin.king@canonical.com> wrote:
> The expression 1 << nr_slots is evaluated with 32 bit integer arithmetic
> and can overflow before it is widened.
If it does, it's an error on the part of the caller. See the banner comment:
1 <= nr_slots <= 9.
And, in any case, if nr_slots >= 64, using BIT_ULL wouldn't help...
David
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] afs: use BIT_ULL for shifting to fix integer overflow
2019-08-30 14:01 ` David Howells
@ 2019-08-30 14:06 ` Colin Ian King
0 siblings, 0 replies; 3+ messages in thread
From: Colin Ian King @ 2019-08-30 14:06 UTC (permalink / raw)
To: David Howells; +Cc: linux-afs, kernel-janitors, linux-kernel
On 30/08/2019 15:01, David Howells wrote:
> Colin King <colin.king@canonical.com> wrote:
>
>> The expression 1 << nr_slots is evaluated with 32 bit integer arithmetic
>> and can overflow before it is widened.
>
> If it does, it's an error on the part of the caller. See the banner comment:
> 1 <= nr_slots <= 9.
Doh, totally missed that. My bad.
>
> And, in any case, if nr_slots >= 64, using BIT_ULL wouldn't help...
>
> David
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-08-30 14:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-30 10:49 [PATCH] afs: use BIT_ULL for shifting to fix integer overflow Colin King
2019-08-30 14:01 ` David Howells
2019-08-30 14:06 ` Colin Ian King
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).