5 ms·
This isn't the usual way this is coded: char *one = "one"; char *end; errno = 0; // remember errno? long i = strtol(one, &end, 10); if (errno != 0) {
by kazinator 5y ago
This isn't the usual way this is coded:
char *one = "one";
char *end;
errno = 0; // remember errno?
long i = strtol(one, &end, 10);
if (errno != 0) {
perror("Error parsing integer from string: ");
} else if (i == 0 && end == one) {
fprintf(stderr, "Error: invalid input: %s\n", one);
} else if (i == 0 && *end != '\0') {
f__kMeGently(with_a_chainsaw);
}
It's actually like this:
errno = 0;
long i = strtol(input, &end, 10);
if (end == input) {
// no digits were found
} else if (*end != 0 && no_ignore_trailing_junk) {
// unwanted trailing junk
} else if ((i == LONG_MIN || i == LONG_MAX)) && errno != 0) {
// overflow case
} else {
// good!
}
errno only needs to be checked in the LONG_MIN or LONG_MAX case. These cares are ambiguous: LONG_MIN and LONG_MAX are valid values of type long, and they are used for reporting an underflow or overflow. Therefore errno is reset to zero first. Otherwise what if errno contains a nonzero value, and LONG_MAX happens to be a valid, non-overflowing value out of the function?
Anyway, you cannot get away from handling these cases no matter how you implement integer scanning; they are inherent to the problem.
It's not strtol's fault that the string could be empty, or that it could have a valid number followed by junk.
Overflows stem from the use of a fixed-width integer. But even if you use bignums, and parse them from a stream (e.g. network), you may need to set a cutoff: what if a malicious user feeds you an endless stream of digits?
The bit with errno is a bit silly; given that the function's has enough parameters that it could have been dispensed with. We could write a function which is invoked exactly like strtoul, but which, in the overflow case, sets the *end pointer to NULL:
// no assignment to errno before strtol
int i = my_strtoul(input, &end, 10);
if (end == 0) {
// underflow or overflow, indicated by LONG_MIN or LONG_MAX value
} else if (end == input) {
// no digits were found
} else if (*end != 0 && no_ignore_trailing_junk) {
// unwanted trailing junk, but i is good
} else {
// no trailing junk, value in i
}
errno is a pig; under multiple threads, it has to access a thread local value. E.g
#define errno (*__thread_specific_errno_location())
The designer of strtoul didn't do this likely because of the overriding requirement that the end pointer is advanced past whatever the function was able to recognize as a number, no matter what. This is lets the programmer write a tokenizer which can diagnose the overflow error, and then keep going with the next token.
- xigoi 5y agoSure, you can't get away from handling the cases, but as the article clearly demonstrates, there can be a much better interface for it.
- kazinator 5y agoI'm not convinced. Can the Rust function which is shown (that function alone) tokenize a number out of a string such that the number is overflowing the target type? Yet indicate to the caller where that overflowing number ends, so that tokenization can continue with subsequent characters, if any? E.g. suppose we have a string with this kind of syntax: "12345 : 12345 , 12345" We can 1. use strtol to get the first integer and a pointer to just after it. 2. use ptr += strspn(ptr, " ") to skip spaces 3. check for the colon and if we find it, skip with ptr++ 4. use strtol to get the second integer (possibly preceded by space). 5. similarly to the colon handling, do the comma 6. strtol again to get the integer. This is efficient: no splitting of the string into pieces requiring memory allocation, and extra list processing. We can code this robustly: it can recognize valid syntax even if some of the numbers overflow. So for this kind of input: "1234523442345345345234534545454545 : 12345 12345" the code could diagnose the overflow, and the missing comma in one pass. If you don't care about the details, just "is this number in range, with no trailing junk, or else is it bad", then raw strtol isn't convenient. But takes only a little code to wrap it.
- kaba0 5y agoRust can return string slices, so a tuple of a return type with a potential number and a slice for the still unprocessed string would be an option, which is much safer than your proposed one and arguably more readable.
- iovrthoughtthis 5y agobut this is not a appropriate place for that functionality.
- kazinator 5y ago