[PATCH] bitarray_clearsetn: Fix possible "overflow"

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

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 ;
     if (h) s[b>>3] |= mask ; else s[b>>3] &= ~mask ;
   }
 }
-- 
2.6.4
Received on Sat Jan 02 2016 - 16:45:42 UTC

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