Skip to content

Commit ea5d05b

Browse files
jankaratorvalds
authored andcommitted
lib/bitmap.c: fix undefined shift in __bitmap_shift_{left|right}()
If __bitmap_shift_left() or __bitmap_shift_right() are asked to shift by a multiple of BITS_PER_LONG, they will try to shift a long value by BITS_PER_LONG bits which is undefined. Change the functions to avoid the undefined shift. Coverity id: 1192175 Coverity id: 1192174 Signed-off-by: Jan Kara <jack@suse.cz> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5a6e759 commit ea5d05b

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎lib/bitmap.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ void __bitmap_shift_right(unsigned long *dst,
131131
lower = src[off + k];
132132
if (left && off + k == lim - 1)
133133
lower &= mask;
134-
dst[k] = upper << (BITS_PER_LONG - rem) | lower >> rem;
134+
dst[k] = lower >> rem;
135+
if (rem)
136+
dst[k] |= upper << (BITS_PER_LONG - rem);
135137
if (left && k == lim - 1)
136138
dst[k] &= mask;
137139
}
@@ -172,7 +174,9 @@ void __bitmap_shift_left(unsigned long *dst,
172174
upper = src[k];
173175
if (left && k == lim - 1)
174176
upper &= (1UL << left) - 1;
175-
dst[k + off] = lower >> (BITS_PER_LONG - rem) | upper << rem;
177+
dst[k + off] = upper << rem;
178+
if (rem)
179+
dst[k + off] |= lower >> (BITS_PER_LONG - rem);
176180
if (left && k + off == lim - 1)
177181
dst[k + off] &= (1UL << left) - 1;
178182
}

0 commit comments

Comments
 (0)