#10909 closed bug (invalid)
[easy] strtol doesn't handle overflow properly
Reported by: | pulkomandy | Owned by: | nobody |
---|---|---|---|
Priority: | normal | Milestone: | |
Component: | System/POSIX | Version: | R1/Development |
Keywords: | Cc: | ||
Blocked By: | Blocking: | ||
Platform: | All |
Description
When trying to parse a number too big to fit a long, strtol returns 0 instead of LONG_MAX. If I read the POSIX spec correctly, it should:
- Set end to the character after the last digit in the string
- Set errno to ERANGE
- Return LONG_MAX.
See attached test program.
http://pubs.opengroup.org/onlinepubs/007904875/functions/strtol.html
Attachments (1)
Change History (12)
by , 10 years ago
comment:1 by , 10 years ago
Milestone: | R1 → Unscheduled |
---|
Move POSIX compatibility related tickets out of R1 milestone (FutureHaiku/Features).
comment:5 by , 6 years ago
Milestone: | Unscheduled → R1 |
---|
Probably not (you can check the attached test program), but rather than fixing the existing code, we can try to get the implementation from https://www.musl-libc.org/ and see if it can be used to replace ours. It's likely that the implementation in musl is not affected by this bug.
We could also try to get an implementation from FreeBSD or other places where this function is implemented.
comment:7 by , 6 years ago
I think this is not a bug, but a problem with the test program. Because this function is not declared in stdio.h, its function declaration will default to int (...), so you can compile it. When linking, it will link to the standard library by default, so the link will pass. I didn't check the standard, but I can imagine that this is an undefined behavior or an unspecified behavior, and the compiler also gives the correct warning.
comment:8 by , 6 years ago
int and long int are the same on 32-bit Haiku. No undefined behavior here. You can add the include if you want.
The function should not return 0, but LONG_MAX. Does it?
comment:9 by , 6 years ago
Of course yes.
#include "stdio.h" #include "stdlib.h" int main(void){ char *end[50]; long u = strtol("99999999999999999999999", end, 10); printf("u=%ld\n", u); printf("LONG_MAX=%ld\n", LONG_MAX); return 0; }
Return LONG_MAX correctly.
comment:10 by , 6 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Uh, indeed. The problem is not the return type, but the missing arguments. Of course it works better if you call it with all 3 arguments. Sorry!
Thanks for investigating!
comment:11 by , 5 years ago
Milestone: | R1 |
---|
Remove milestone for tickets with status = closed and resolution != fixed
Test program that shows strtol returning 0.