Skip to contentSkip to conversation

atoi.3: Explain disadvantages of atoi()

PATCH In review Arkadiusz Drabczyk 2 participants 2 messages Jun 18, 2020 – Jun 19, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 atoi.3: Explain disadvantages of atoi()

I believe new users should be discouraged from using atoi() and that its disadvantages should be explained.

I added the information that 0 is returned on error - although C standard and POSIX say that "If the value of the result cannot be represented, the behavior is undefined." there are some interpretations that 0 has to be returned
https://stackoverflow.com/questions/38393162/what-can-i-assume-about-the-behaviour-of-atoi-on-error
and this is also what happens in practice with glibc, musl and uClibc.

---
 man3/atoi.3 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
man3/atoi.3 +12 -1
index c5894d3..3c6527a 100644
@@ -81,7 +81,18 @@ functions behave the same as
 except that they convert the initial portion of the
 string to their return type of \fIlong\fP or \fIlong long\fP.
 .SH RETURN VALUE
-The converted value.
+The converted value or 0 on error.
+.SH BUGS
+.I errno
+is not set on error so there is no way to distinguish between 0 as an
+error and as the converted value.
+No checks for overflow or underflow are done.
+Only input in decimal base can be converted.
+It's recommended to use
+.BR strtol ()
+and
+.BR strtoul ()
+family of functions in new programs.
 .SH ATTRIBUTES
 For an explanation of the terms used in this section, see
 .BR attributes (7).
Signature · 1 line
2.9.0
#1 lore raw

Hello Arkadiusz,

Quoted 11 lines · Arkadiusz Drabczyk

I believe new users should be discouraged from using atoi() and that its disadvantages should be explained.

I added the information that 0 is returned on error - although C standard and POSIX say that "If the value of the result cannot be represented, the behavior is undefined." there are some interpretations that 0 has to be returned
https://stackoverflow.com/questions/38393162/what-can-i-assume-about-the-behaviour-of-atoi-on-error
and this is also what happens in practice with glibc, musl and uClibc.

Thanks. Patch applied. I also made a few small tweaks. The results have been pushed to master.

Thanks,

Michael

Quoted 29 lines
---
 man3/atoi.3 | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
man3/atoi.3 +12 -1
index c5894d3..3c6527a 100644
@@ -81,7 +81,18 @@ functions behave the same as
 except that they convert the initial portion of the
 string to their return type of \fIlong\fP or \fIlong long\fP.
 .SH RETURN VALUE
-The converted value.
+The converted value or 0 on error.
+.SH BUGS
+.I errno
+is not set on error so there is no way to distinguish between 0 as an
+error and as the converted value.
+No checks for overflow or underflow are done.
+Only input in decimal base can be converted.
+It's recommended to use
+.BR strtol ()
+and
+.BR strtoul ()
+family of functions in new programs.
 .SH ATTRIBUTES
 For an explanation of the terms used in this section, see
 .BR attributes (7).
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/