News for hackers. Stuff that matters.
Re:“People who implement new and improved interfaces always ... by thomasahle on Friday September 11, 2015 @11:48AM (#10204335)

          if (isdigit(*str)) {
  -               io_tlb_nslabs = simple_strtoul(str, &str, 0);
  +               str += parse_integer(str, 0, &io_tlb_nslabs);

  and obviously nobody spent even a *second* asking themselves
  "what if parse_integer returns an error".
Can anybody explain why parse_integer would return an error, when isdigit(*str)?

Also, why is the result (presumably integer) added back to the string?

EDIT: Regarding to "adding to the string" I realize that's just C pointer arithmetic.

Re:“People who implement new and improved interfaces always ... by victorNicollet on Friday September 11, 2015 @11:56AM (#10204388)
An overflow may happen if there is an overflow for the type (e.g. reading '300' into an unsigned char).

The function returns either a positive number indicating the number of characters successfully read from the string (if it encountered a non-digit character) or a negative number indicating an error code.

https://lwn.net/Articles/643159/

Re:“People who implement new and improved interfaces always ... by thomasahle on Friday September 11, 2015 @12:59PM (#10204788)
I see. And the previous code didn't have this issue?
Re:“People who implement new and improved interfaces always ... by metric10 on Friday September 11, 2015 @04:06PM (#10205872)
I don't know about Linux's simple_strtoul(), but the C library's strtoul(3) will return ULONG_MAX and set the string pointer to a valid location. So the old code COULD cause an issue if "io_tlb_nslabs" being equal to ULONG_MAX isn't accounted for later on, but adding -1 (0xffffffffffffffff) to a pointer (as the new code does) is far worse.
Re:“People who implement new and improved interfaces always ... by sanderjd on Friday September 11, 2015 @02:39PM (#10205412)
Haven't read any of the relevant code, but if the new code returns a negative number representing an error, it will decrement the `str` pointer, whereas I'm guessing `simple_strtoul` leaves its `str` pointer alone on error.
Re:“People who implement new and improved interfaces always ... by detaro on Friday September 11, 2015 @12:02PM (#10204440)
EDIT: I'm wrong, victorNicollet spotted my mistake. I thought that:

    str == "09"
fails. Leading zero indicates base 8 -> 9 is an invalid digit.

But that is parsed as "0" and the 9 is left in the string.

Re:“People who implement new and improved interfaces always ... by jandrese on Friday September 11, 2015 @12:13PM (#10204517)
IMHO, "leading zero indicates octal" is as old as it is wrong. There are a handful of cases where this is helpful, and an ocean of cases where it breaks things unexpectedly because someone tried inputting prettyprinted data.

At least with Hex you can't mistake the 0x prefix for anything else.

One of the best features of inet_pton over inet_addr is that the former doesn't do automatic conversions and instead assumes every field is a decimal number. The latter has a hidden trap where 192.168.0.11 is different than 192.168.000.011.

Re:“People who implement new and improved interfaces always ... by acveilleux on Friday September 11, 2015 @12:59PM (#10204784)
Yeah, so it's a wrong thing in 2015 where the only significant use of octal is in unix permissions which themselves are only octal because of PDP-7/8.

The 9/18/36-bits architectures have lost but octal conventions of C was made on the PDP-7 (18 bits) and until the early 1980s, 36-bit machines were commercially available (PDP-10.)

Re:“People who implement new and improved interfaces always ... by SixSigma on Friday September 11, 2015 @06:36PM (#10206515)
I use octal with tr
Re:“People who implement new and improved interfaces always ... by victorNicollet on Friday September 11, 2015 @12:20PM (#10204561)
I agree. Still, that's the behaviour of parse_integer (specifically, base determination in _parse_integer_fixup_radix).
Re:“People who implement new and improved interfaces always ... by detaro on Friday September 11, 2015 @12:27PM (#10204596)
It is the specified behavior of the discussed code when not passing in a specified base to parse_integer. Now if that is a needed feature in the example context (or a fixed base of 10 would work as well) I don't know. It is an odd convention though, I agree. Hex I somewhat get, but octal?
Re:“People who implement new and improved interfaces always ... by victorNicollet on Friday September 11, 2015 @12:27PM (#10204592)
Unless I'm mistaken, this would treat character '9' as not part of the number string, return zero and advance the string pointer by one.
Re:“People who implement new and improved interfaces always ... by detaro on Friday September 11, 2015 @12:31PM (#10204614)
Yeah, you're right. Missed that 0 didn't advance it, only 0x.
Re:“People who implement new and improved interfaces always ... by tedunangst on Friday September 11, 2015 @01:12PM (#10204852)
Percentage chance that's what the user desires?
Re:“People who implement new and improved interfaces always ... by saurik on Friday September 11, 2015 @12:00PM (#10204434)
> And yes, parse_integer() really can return an error, even despite that "isdigit(*str)" check. Think about it. Or just read the source code.

Actually taking the advice of the article and looking at the source code makes it clear that parse_integer is doing what we would hope such an API would: it guarantees that the resulting number fits into the provided integer.

https://lwn.net/Articles/643159/

Re:“People who implement new and improved interfaces always ... by mikeash on Friday September 11, 2015 @12:41PM (#10204681)
Returning an error is fine, but the code that calls the function doesn't check for errors, and just blindly applies the return value as an offset to a pointer. When the function returns -ERANGE to indicate that the value was too large for the requested data type, entertaining things will occur.
Re:“People who implement new and improved interfaces always ... by raverbashing on Friday September 11, 2015 @12:05PM (#10204459)
Because isdigit is checking only the first digit, and parse_integer is parsing not only that one, but the following digits as well
Re:“People who implement new and improved interfaces always ... by victorNicollet on Friday September 11, 2015 @12:30PM (#10204608)
parse_integer stops reading when it encounters a non-digit character (in order to support reading multiple integers from the same string).
Re:“People who implement new and improved interfaces always ... by bdarnell on Friday September 11, 2015 @11:59AM (#10204427)
I haven't looked at the patch, but the error is probably overflow: too many digits to fit in the integer variable.

parse_integer() presumably returns the number of bytes consumed, so the str pointer is advanced by that many bytes to point to whatever is after the integer. But on error, it returns a negative number, so str is moved backwards into some nonsensical position.

Re:“People who implement new and improved interfaces always ... by jonathlee on Friday September 11, 2015 @12:06PM (#10204463)
If isdigit() is the standard library function, that takes/checks a single character. However, parse_integer() takes a full string and parses until it reaches a null or newline. The first character could definitely be a valid digit but the rest of the string could cause a problem. However, I am not a C programmer, so my internet search-guided intuition may be wrong.
Re:“People who implement new and improved interfaces always ... by gpvos on Friday September 11, 2015 @11:56AM (#10204387)
I haven't seen the code of parse_integer, but it could be on overflow, i.e., a too large number.
Re:“People who implement new and improved interfaces always ... by missblit on Friday September 11, 2015 @11:58AM (#10204415)
I didn't look too closely, but I think it will fail when the string is "0x".
Re:“People who implement new and improved interfaces always ... by victorNicollet on Friday September 11, 2015 @12:28PM (#10204597)
It only switches to base 16 if there's a digit after the "x". For "0x", it would just parse 0.
Re:“People who implement new and improved interfaces always ... by missblit on Friday September 11, 2015 @12:43PM (#10204691)
Ah thanks, I missed the isxdigit(s[2])
Re:“People who implement new and improved interfaces always ... by ajross on Friday September 11, 2015 @11:54AM (#10204381)
Not sure because I didn't read the parse_integer patches. But the converse is a problem. isdigit("-2") returns false but the value will parse correctly.
Re:“People who implement new and improved interfaces always ... by interdrift on Friday September 11, 2015 @11:58AM (#10204422)
parse_integer takes a string not a char, didn't look at the implementation either but it makes sense to check for the whole string doesnt it)
Re:“People who implement new and improved interfaces always ... by monochromatic on Friday September 11, 2015 @11:56AM (#10204391)
How is that a problem though?
Re:“People who implement new and improved interfaces always ... by username3 on Friday September 11, 2015 @11:54AM (#10204378)
Maybe under some race conditions.