Skip to content

Use memcmp() instead of strncmp() to compare cache buckets. - #46

Merged
johnezang merged 1 commit into
johnezang:masterfrom
jparise:cache-collisions
Sep 23, 2011
Merged

johnezang merged 1 commit into
johnezang:masterfrom
jparise:cache-collisions

Conversation

@jparise

@jparise jparise commented Aug 31, 2011

Copy link
Copy Markdown
Contributor

In the rare case when a hash collision occurs between two values, the
object cache's bucket-probing code needs to go as far as comparing the
bytes content of the candidate bucket with the bytes content of the
current token.

For numeric values, 'bytes' refers to the raw bytes of the number.
These byte representations may contain leading '\0' values. strncmp()
is only designed for comparing strings, so characters appearing after
a '\0' are not compared.

Therefore, if a hash collision occurs between two numeric values that
only differ in their lower bytes, they will be assigned to the same
bucket, and the cache will return the first token's object for the
second token's value.

memcmp() is binary-safe and considers all of the numbers' bytes,
avoiding this problem.

This is a rare case indeed, but here is an example that reproduces the
problem:

JSON Input:             [ 1734.75, 417.75 ]
Identical Types:        double (8 bytes)
Identical DJB Hashes:   2510392872

JSONKit (strncmp):      [ 1734.75, 1734.75 ]    (boo!)
JSONKit (memcmp):       [ 1734.75, 417.75 ]     (yay!)

In the rare case when a hash collision occurs between two values, the
object cache's bucket-probing code needs to go as far as comparing the
bytes content of the candidate bucket with the bytes content of the
current token.

For numeric values, 'bytes' refers to the raw bytes of the number.
These byte representations may contain leading '\0' values.  strncmp()
is only designed for comparing strings, so characters appearing after
a '\0' are not compared.

Therefore, if a hash collision occurs between two numeric values that
only differ in their lower bytes, they will be assigned to the same
bucket, and the cache will return the first token's object for the
second token's value.

memcmp() is binary-safe and considers all of the numbers' bytes,
avoiding this problem.

This is a rare case indeed, but here is an example that reproduces the
problem:

    JSON Input:             [ 1734.75, 417.75 ]
    Identical Types:        double (8 bytes)
    Identical DJB Hashes:   2510392872

    JSONKit (strncmp):      [ 1734.75, 1734.75 ]    (boo!)
    JSONKit (memcmp):       [ 1734.75, 417.75 ]     (yay!)
@johnezang

Copy link
Copy Markdown
Owner

Ack, I forget about this one, sorry for letting it sit. My bad.

This is a fantastic catch. Sorry about this, no idea how it got through... either a blatant error on my part, or due to some reason lost to the mists of time (when I originally implemented the cache, it was just for strings, since they dominated both in frequency of occurrence and in instantiation cost... in which case, strcmp would have been valid since there is an explicit check to reject the (technically legal) U+0000 code point (see the README)).

@ghost ghost assigned johnezang Sep 23, 2011
johnezang added a commit that referenced this pull request Sep 23, 2011
Use memcmp() instead of strncmp() to compare cache buckets.
@johnezang
johnezang merged commit 660546f into johnezang:master Sep 23, 2011
@jparise
jparise deleted the cache-collisions branch February 16, 2018 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants