[Zodb-checkins] CVS: Zope3/src/zodb/btrees - _fsBTree.c:1.3
Tim Peters
tim_one@email.msn.com
Sat, 5 Apr 2003 01:43:20 -0500
[Jeremy Hylton]
> Modified Files:
> _fsBTree.c
> Log Message:
> Shouldn't user strcmp() to compare binary strings.
>
> XXX Not sure where this macro is used or what kind of failures this
> bug caused.
> ...
> -#define TEST_VALUE(K, T) strncmp(K,T,6)
> +#define TEST_VALUE(K, T) memcmp(K,T,6)
> ...
TEST_VALUE is used by primarily bucket conflict resolution, where "equal"
versus "not equal" is the only outcome checked. There's also the funky
byValue() method of mappings, which returns items sorted by value. That
method isn't used anywhere in Zope3, and probably won't ever be used on an
fsBTree. TEST_VALUE isn't used anywhere else.
strncmp(p, q, n) and memcmp(p, q, n) always return the same {less, equal,
greater} result, except possibly when strlen(p) < n or strlen(q) < n.
That's because strncmp considers an input to end when it sees the first NUL
byte, or when it's seen n chars, whichever comes first. So, e.g.,
strncmp("\0\0", "\0\377\", 2)
sez the strings are equal, but memcmp applied to the same inputs sez the
second argument is larger (all these guys treat the input bytes as unsigned,
BTW). So the primary danger is that strncmp would say that unequal binary
strings are equal. That could certainly cause conflict resolution to screw
up, probably by failing to detect a conflict when the same key is mapped to
distinct binary values containing NUL bytes, and strncmp erroneously
considers the values to be equal.