Mentioning strtonum(3) in style(9)

Andre Nathan andre at digirati.com.br
Fri Sep 3 16:37:23 PDT 2004


Hello

There was a recent style(9) commit regarding the use of safe functions, so
I guess strtonum(3) should be mentioned too. Below is a patch, though
someone who speaks english better than me will probably come up with
better wording...

--- style.9.orig        2004-09-03 19:38:00.000000000 -0300
+++ style.9     2004-09-03 20:38:34.000000000 -0300
@@ -353,10 +353,10 @@
                        bflag = 1;
                        break;
                case 'n':
-                       num = strtol(optarg, &ep, 10);
-                       if (num <= 0 || *ep != '\e0') {
-                               warnx("illegal number, -n argument -- %s",
-                                   optarg);
+                       num = strtonum(optarg, 0, INT_MAX, &errstr);
+                       if (errstr) {
+                               warnx("-n argument is %s: %s", errstr,
+                                     optarg);
                                usage();
                        }
                        break;
@@ -652,6 +652,14 @@
 should be used instead of
 .Xr sprintf 3 .
 .Pp
+When doing string-to-number conversions,
+.Xr strtonum 3
+should be used instead of functions which are either difficult or impossible
+to use safely, such as
+.Xr atoi 3 ,
+.Xr strtol 3
+and similars.
+.Pp
 Varargs procedures should be formatted as follows:
 .Bd -literal
 #include <stdarg.h>

I'd like to point to a previous thread[1] where I sent a patch to mention
strtonum on the manual pages of the other string-to-number functions (as
well as patches that substitute calls to these functions).

Regards,
Andre

[1]http://leaf.dragonflybsd.org/mailarchive/submit/2004-08/msg00078.html





More information about the Submit mailing list