Please sign in to comment.
Use memcmp() instead of strncmp() to compare cache buckets.
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!)
- Loading branch information...