Harden numeric value parsing#177
Conversation
|
This library is for command line argument parsing. Are you aware of a system that can actually supply a null byte to arguments? |
|
You're right that an embedded NUL can't come from the OS command line itself — The reason I proposed the change is that the parsing API can also be used with caller-provided int i;
reader("x", std::string("12\0garbage", 10), i); // accepted as 12
double d;
reader("x", std::string("12\0garbage", 10), d); // rejectedWhat motivated the patch was mainly this inconsistency rather than the embedded-NUL case itself. The integer path validates using the C-string terminator, while the floating-point path already rejects trailing data beyond the NUL. The change makes the integer reader validate against the actual string length and behave consistently with the floating-point reader. That said, if inputs outside normal |
|
That is reasonable enough. It's really an edge case, but it doesn't bloat the code, and it's a moderate improvement, so I'm happy with it. Thanks for the PR. |
Fix integer parsing to correctly reject values containing embedded NUL (
'\0') characters.The integer parsing path uses
std::strtoull/std::strtoll, which operate on C strings and stop parsing at the first NUL byte. The existing validation checked for trailing data using*end == '\0', which allowed inputs such as:to be accepted as the value
12while silently ignoring the remaining contents of the string.This change validates parsing against the actual length of the input string rather than the first NUL terminator, ensuring that all characters in the provided
std::stringare accounted for.Changes
stop) derived fromvalue.size().Behavior
Before:
After:
Valid numeric forms continue to parse correctly, including:
12)-7)0x10)010)Testing
Added regression coverage for:
Verified that valid numeric inputs continue to parse successfully.