-
Type: Bug
-
Resolution: Unresolved
-
Priority: Minor - P4
-
None
-
Affects Version/s: bson-4.7.2, bson-5.5.0, bson-6.1.0
-
Component/s: BSON
What problem are you facing?
Long.fromString doesn't check if the string is a valid decimal string. This behavior leads to bizarre and hard-to-track bugs when a string is invalid.
This function splits the string up into 8-digit chunks and parses each one individually. However, it uses parseInt to do so, which ignores characters after an invalid character is found, i.e. parseInt("123abc") == 123.
While this behavior is predictable on a single int, the interaction of these two mechanisms, as well as needing to "pad" the last chunk if it is shorter than 8 digits produces some bizarre behavior when invalid characters are in the string:
- Long.fromString("1234xxx5") == 1234
- Long.fromString("1234xxxx5") == 12345
- Long.fromString("1234xxxxx5") == 123400
You can see why this behavior happens when you look at the chunks each string is split into:
- "1234xxx5" → ["1234xxx5"]
- "1234xxxx5" → ["1234xxxx", "5"]
- "1234xxxxx5" → ["1234xxxx", "x5"]
However, this behavior is still incredibly unpredictable to users, especially since I don't believe it is documented anywhere what happens when the string is not a valid decimal string.
I noticed that Long.fromExtendedJSON performs a regex check to make sure the string is a valid decimal integer. Why don't we move this check into Long.fromString instead?
I don't know if it's intentional that the strings aren't checked (i.e. for performance reasons), but if that's the case, it should at the very least be documented.
What driver and relevant dependency versions are you using?
Relevant on versions >=4.0.0 of js-bson.
Steps to reproduce?
Described above
- is related to
-
NODE-5648 Add a Long.fromStringStrict method
- Closed