Re: [PATCH] bitarray_clearsetn: Fix possible "overflow"

From: Olivier Brunel <jjk_at_jjacky.com>
Date: Sat, 2 Jan 2016 19:02:05 +0100

On Sat, 2 Jan 2016 17:45:42 +0100
Olivier Brunel <jjk_at_jjacky.com> wrote:

> Whenever setting (or clearing) (up to) the last bit in a char, it
> would "overflow" and set/clear all the bits up to it instead.
>
> Signed-off-by: Olivier Brunel <jjk_at_jjacky.com>
> ---
> src/libstddjb/bitarray_clearsetn.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/src/libstddjb/bitarray_clearsetn.c
> b/src/libstddjb/bitarray_clearsetn.c index b3f46f1..5fee42d 100644
> --- a/src/libstddjb/bitarray_clearsetn.c
> +++ b/src/libstddjb/bitarray_clearsetn.c
> _at_@ -8,7 +8,7 @@ void bitarray_clearsetn (register unsigned char *s,
> register unsigned int a, reg b += a ;
> if ((a >> 3) == ((b-1) >> 3))
> {
> - register unsigned char mask = ((1 << (a & 7)) - 1) ^ ((1 << (b &
> 7)) - 1) ;
> + register unsigned char mask = ((1 << (a & 7)) - 1) ^ ((1 << ((b
> & 7) ? b & 7 : 8)) - 1) ; if (h) s[a>>3] |= mask ; else s[a>>3] &=
> ~mask ; }
> else
> _at_@ -18,7 +18,7 @@ void bitarray_clearsetn (register unsigned char *s,
> register unsigned int a, reg if (h) s[a>>3] |= mask ; else s[a>>3] &=
> ~mask ; mask = h ? 0xff : 0x00 ;
> for (; i < b>>3 ; i++) s[i] = mask ;
> - mask = (1 << (b & 7)) - 1 ;
> + mask = (1 << ((b & 7) ? b & 7 : 8)) - 1 ;

On second thought, this one isn't needed actually. Since we're not
setting only one bit, but up to it, and to get to the last one we'll
have simply used the 0xff mask above, an empty mask (0) works fine
here.
So in this case it doesn't apply, actually. Only the first one, when
setting only the last bit in the char. Commit message should also be
updated to remove the "(up to)"

> if (h) s[b>>3] |= mask ; else s[b>>3] &= ~mask ;
> }
> }
Received on Sat Jan 02 2016 - 18:02:05 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:38:49 UTC