]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
atoi.3: Explain disadvantages of atoi()
authorArkadiusz Drabczyk <arkadiusz@drabczyk.org>
Thu, 18 Jun 2020 20:42:41 +0000 (22:42 +0200)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Fri, 19 Jun 2020 12:13:57 +0000 (14:13 +0200)
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.

Signed-off-by: Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
man3/atoi.3

index c5894d325d83d1e8c3ffcb4f0cf0d8a95aaf004d..3c6527a5bf618a407e61d99a128a87398ee198cf 100644 (file)
@@ -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).