bitstring.h -- Correct errors in bit_nsearch.
authorVenkatesh Srinivas <me@endeavour.zapto.org>
Thu, 5 May 2011 08:28:59 +0000 (01:28 -0700)
committerVenkatesh Srinivas <me@endeavour.zapto.org>
Thu, 5 May 2011 08:28:59 +0000 (01:28 -0700)
* bit_nsearch returned 0, not the correct -1, for a size 0 request.

* bit_nsearch returned 0, not the correct -1, for requests larger than the
  bitvector.

* bit_nsearch didn't find a zero cluster if it was the last one in a vector.

Submitted-by: Luis Useche <useche@gmail.com>
Closes bug: 2057

include/bitstring.h

index 975f8b6..6c47b4b 100644 (file)
@@ -172,34 +172,28 @@ typedef   unsigned char bitstr_t;
        int _nbits = (nbits); \
        int *_value = (value); \
        int _len = (len); \
-       int _bit, _bit0; \
+       int _bit, _bit0 = -1; \
        int _tmplen = _len; \
        int _match = 0; \
        *(_value) = -1; \
-       for (_bit = 0; _bit < _nbits; _bit++) { \
+       for (_bit = 0; _bit < _nbits && _tmplen > 0; _bit++) { \
                if (_match == 0) { \
                        if (bit_test((_name), _bit) == 0) { \
                                _match = 1; \
                                _tmplen--; \
                                _bit0 = _bit; \
-                       } else { \
-                               continue; \
                        } \
                } else { \
-                       if (_tmplen > 0) { \
-                               if (bit_test((_name), _bit) == 0) { \
-                                       _tmplen--; \
-                               } else { \
-                                       _match = 0; \
-                                       _tmplen = _len; \
-                                       continue; \
-                               } \
+                       if (bit_test((_name), _bit) == 0) { \
+                               _tmplen--; \
                        } else { \
-                               *(_value) = _bit0; \
-                                break; \
+                               _match = 0; \
+                               _tmplen = _len; \
                        } \
                } \
        } \
+       if (_tmplen == 0) \
+               *(_value) = _bit0; \
 } while (0)
 
 #endif /* !_BITSTRING_H_ */