News for hackers. Stuff that matters.
Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by PaulDavisThe1st on Wednesday May 21, 2025 @05:50PM (#44056642)
> The return value should have been an error code and the actual long should have been "returned" via a pointer.

Oh, you mean like:

    int ret = sscanf (str, "%d", &value);

?
Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by sltkr on Wednesday May 21, 2025 @06:22PM (#44056878)
Yes, that API is actually great, but the problem with (s)scanf() is that reading an invalid value is undefined behavior. So you can't use it if you don't already know the result fits in &value, which is exactly the situation where you'd use strtol() instead.
Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by CamperBob2 on Wednesday May 21, 2025 @07:21PM (#44057247)
Presumably 'undefined behavior' means you'll get an undefined int value back -- which you will (of course) range-check -- not that it will wipe out the next 600KB of memory starting at &value or do something similarly hazardous.
Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by roelschroeven on Wednesday May 21, 2025 @08:07PM (#44057493)
No, that's very much not what undefined behavior means. Undefined Behavior (the man page on my system actually capitalizes both words) means that there are no guarantees at all about the behavior of the whole program. It can very much wipe out whole chunks of memory, or crash (not necessarily in or around the sscanf call), or get stuck in an infinite loop, or whatever.
Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by PaulDavisThe1st on Wednesday May 21, 2025 @08:47PM (#44057689)
From the man page on my system:

> Use of the numeric conversion specifiers produces Undefined Behavior for invalid input. See C11 7.21.6.2/10 ⟨https://port70.net/%7Ensz/c/c11/n1570.html#7.21.6.2p10⟩. This is a bug in the ISO C standard, and not an inherent design issue with the API. However, current implementations are not safe from that bug, so it is not recommended to use them. Instead, programs should use functions such as strtol(3) to parse numeric input. This manual page deprecates use of the numeric conversion specifiers until they are fixed by ISO C.

Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by CamperBob2 on Wednesday May 21, 2025 @08:59PM (#44057751)
If the CRTL maintainers don't care, why should I? Such behavior is broken, not "undefined."

As the other poster points out, the bug is in the spec, and I'd be astonished if the library function itself actually misbehaves with any given input.

Re:Ratatoi is a C libary that wraps stdlib's strtol (as atoi... by roelschroeven on Thursday May 22, 2025 @06:44PM (#44067967)
The bug is in the spec, but as the other pointer also points out:

> However, current implementations are not safe from that bug, so it is not recommended to use them. Instead, programs should use functions such as strtol(3) to parse numeric input. This manual page deprecates use of the numeric conversion specifiers until they are fixed by ISO C.

In other words, sscanf itself is not safe to use for numeric conversions.

Why should you care? Depends. Do you care about writing correct programs? If not, there's no need to care.