Repository navigation
Use memcmp() instead of strncmp() to compare cache buckets. - #46
Merged
Merged
Conversation
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!)
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)). |
johnezang
added a commit
that referenced
this pull request
Sep 23, 2011
Use memcmp() instead of strncmp() to compare cache buckets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: