Slighty different version, so withdrawal of casts and strtol => strtonum.
I still maintain (SHRT_MAX - 1) as PID_MAX is not exposed to the userland
(@tedu mentioned he planned to increase it,
would it be good to expose it via, for example, sysctl which makes it also
settable ?)
Index: bin/ps/ps.c
===================================================================
RCS file: /cvs/src/bin/ps/ps.c,v
retrieving revision 1.62
diff -u -p -r1.62 ps.c
--- bin/ps/ps.c 8 Jul 2014 23:31:22 -0000 1.62
+++ bin/ps/ps.c 9 Dec 2014 16:51:51 -0000
@@ -97,6 +97,7 @@ main(int argc, char *argv[])
uid_t uid;
int all, ch, flag, i, fmt, lineno, nentries;
int prtheader, showthreads, wflag, kflag, what, Uflag, xflg;
+ const char *errstr;
char *nlistf, *memf, *swapf, *cols, errbuf[_POSIX2_LINE_MAX];
if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
@@ -188,7 +189,9 @@ main(int argc, char *argv[])
fmt = 1;
break;
case 'p':
- pid = atol(optarg);
+ pid = strtonum(optarg, -1, (SHRT_MAX - 1), &errstr);
+ if (errstr)
+ errx(1, "pid: %s", errstr);
xflg = 1;
break;
case 'r':
Index: bin/systrace/systrace.c
===================================================================
RCS file: /cvs/src/bin/systrace/systrace.c,v
retrieving revision 1.61
diff -u -p -r1.61 systrace.c
--- bin/systrace/systrace.c 26 Nov 2014 18:34:51 -0000 1.61
+++ bin/systrace/systrace.c 9 Dec 2014 16:51:51 -0000
@@ -644,6 +644,7 @@ int
main(int argc, char **argv)
{
int i, c;
+ const char *errstr;
char **args;
char *filename = NULL;
char *policypath = NULL;
@@ -707,8 +708,9 @@ main(int argc, char **argv)
case 'p':
if (setcredentials)
usage();
- if ((pidattach = atoi(optarg)) == 0) {
- warnx("bad pid: %s", optarg);
+ pidattach = strtonum(optarg, 1, (SHRT_MAX - 1), &errstr);
+ if (errstr) {
+ warnx("bad pid: %s (%s)", optarg, errstr);
usage();
}
break;
Index: usr.bin/sudo/sudo.c
===================================================================
RCS file: /cvs/src/usr.bin/sudo/sudo.c,v
retrieving revision 1.48
diff -u -p -r1.48 sudo.c
--- usr.bin/sudo/sudo.c 7 Dec 2013 14:53:29 -0000 1.48
+++ usr.bin/sudo/sudo.c 9 Dec 2014 16:52:09 -0000
@@ -37,6 +37,9 @@
# include <sys/time.h>
# include <sys/resource.h>
#endif
+#ifdef BSD
+# include <sys/sysctl.h>
+#endif
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
@@ -853,6 +856,10 @@ parse_args(argc, argv)
int argc;
char **argv;
{
+#ifdef BSD
+ const char *errstr;
+ size_t maxfiles, maxfileslen;
+#endif
int mode = 0; /* what mode is sudo to be run in? */
int flags = 0; /* mode flags */
int valid_flags, ch;
@@ -861,6 +868,12 @@ parse_args(argc, argv)
if (strcmp(getprogname(), "sudoedit") == 0)
mode = MODE_EDIT;
+#ifdef BSD
+ if (sysctl("kern.maxfiles", strlen("kern.maxfiles"), NULL, NULL,
+ &maxfiles, &maxfileslen) == -1)
+ errorx(1, "kern.maxfiles failed");
+#endif
+
/* Returns true if the last option string was "--" */
#define got_end_of_args (optind > 1 && argv[optind - 1][0] == '-' && \
argv[optind - 1][1] == '-' && argv[optind - 1][2] == '\0')
@@ -893,8 +906,14 @@ parse_args(argc, argv)
SET(flags, MODE_BACKGROUND);
break;
case 'C':
+#ifdef BSD
+ user_closefrom = strtonum(optarg, 3, maxfiles, &errstr);
+ if (errstr) {
+ warningx("the argument to -C is invalid: %s", errstr);
+#else
if ((user_closefrom = atoi(optarg)) < 3) {
warningx("the argument to -C must be at least 3");
+#endif
usage(1);
}
break;
Index: usr.bin/vmstat/vmstat.c
===================================================================
RCS file: /cvs/src/usr.bin/vmstat/vmstat.c,v
retrieving revision 1.134
diff -u -p -r1.134 vmstat.c
--- usr.bin/vmstat/vmstat.c 23 Nov 2014 04:34:48 -0000 1.134
+++ usr.bin/vmstat/vmstat.c 9 Dec 2014 16:52:10 -0000
@@ -136,7 +136,9 @@ main(int argc, char *argv[])
while ((c = getopt(argc, argv, "c:fiM:mN:stw:vz")) != -1) {
switch (c) {
case 'c':
- reps = atoi(optarg);
+ reps = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr)
+ errx(1, "-c %s: %s", optarg, errstr);
break;
case 'f':
todo |= FORKSTAT;
@@ -224,8 +226,11 @@ main(int argc, char *argv[])
if (errstr)
errx(1, "%s: %s", *argv, errstr);
- if (*++argv)
- reps = atoi(*argv);
+ if (*++argv) {
+ reps = strtonum(*argv, 0, INT_MAX, &errstr);
+ if (errstr)
+ errx(1, "%s: %s", *argv, errstr);
+ }
}
#endif
On 9 December 2014 at 13:38, David Carlier <[email protected]> wrote:
> Hi all,
>
> I made a small first diff to replace some ato* functions call to
> strtonum/l ones ...
> Althought not sure about the sudo one because it looks contrib code and
> also might appear a bit "overengineered" ... but in case !
>
> Thanks in advance.
>
> Index: bin/ps/ps.c
> ===================================================================
> RCS file: /cvs/src/bin/ps/ps.c,v
> retrieving revision 1.62
> diff -u -p -r1.62 ps.c
> --- bin/ps/ps.c 8 Jul 2014 23:31:22 -0000 1.62
> +++ bin/ps/ps.c 9 Dec 2014 13:34:44 -0000
> @@ -97,6 +97,7 @@ main(int argc, char *argv[])
> uid_t uid;
> int all, ch, flag, i, fmt, lineno, nentries;
> int prtheader, showthreads, wflag, kflag, what, Uflag, xflg;
> + const char *errstr;
> char *nlistf, *memf, *swapf, *cols, errbuf[_POSIX2_LINE_MAX];
>
> if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') {
> @@ -188,7 +189,9 @@ main(int argc, char *argv[])
> fmt = 1;
> break;
> case 'p':
> - pid = atol(optarg);
> + pid = (pid_t)strtonum(optarg, -1, (SHRT_MAX - 1), &errstr);
> + if (errstr)
> + errx(1, "pid: %s", errstr);
> xflg = 1;
> break;
> case 'r':
> Index: bin/systrace/systrace.c
> ===================================================================
> RCS file: /cvs/src/bin/systrace/systrace.c,v
> retrieving revision 1.61
> diff -u -p -r1.61 systrace.c
> --- bin/systrace/systrace.c 26 Nov 2014 18:34:51 -0000 1.61
> +++ bin/systrace/systrace.c 9 Dec 2014 13:34:44 -0000
> @@ -644,6 +644,7 @@ int
> main(int argc, char **argv)
> {
> int i, c;
> + const char *errstr;
> char **args;
> char *filename = NULL;
> char *policypath = NULL;
> @@ -707,8 +708,9 @@ main(int argc, char **argv)
> case 'p':
> if (setcredentials)
> usage();
> - if ((pidattach = atoi(optarg)) == 0) {
> - warnx("bad pid: %s", optarg);
> + pidattach = strtonum(optarg, 1, (SHRT_MAX - 1), &errstr);
> + if (errstr) {
> + warnx("bad pid: %s (%s)", optarg, errstr);
> usage();
> }
> break;
> Index: usr.bin/sudo/sudo.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/sudo/sudo.c,v
> retrieving revision 1.48
> diff -u -p -r1.48 sudo.c
> --- usr.bin/sudo/sudo.c 7 Dec 2013 14:53:29 -0000 1.48
> +++ usr.bin/sudo/sudo.c 9 Dec 2014 13:35:03 -0000
> @@ -37,6 +37,9 @@
> # include <sys/time.h>
> # include <sys/resource.h>
> #endif
> +#ifdef BSD
> +# include <sys/sysctl.h>
> +#endif
> #include <stdio.h>
> #ifdef STDC_HEADERS
> # include <stdlib.h>
> @@ -853,6 +856,10 @@ parse_args(argc, argv)
> int argc;
> char **argv;
> {
> +#ifdef BSD
> + const char *errstr;
> + size_t maxfiles, maxfileslen;
> +#endif
> int mode = 0; /* what mode is sudo to be run in? */
> int flags = 0; /* mode flags */
> int valid_flags, ch;
> @@ -861,6 +868,12 @@ parse_args(argc, argv)
> if (strcmp(getprogname(), "sudoedit") == 0)
> mode = MODE_EDIT;
>
> +#ifdef BSD
> + if (sysctl("kern.maxfiles", strlen("kern.maxfiles"), NULL, NULL,
> + &maxfiles, &maxfileslen) == -1)
> + errorx(1, "kern.maxfiles failed");
> +#endif
> +
> /* Returns true if the last option string was "--" */
> #define got_end_of_args (optind > 1 && argv[optind - 1][0] == '-' && \
> argv[optind - 1][1] == '-' && argv[optind - 1][2] == '\0')
> @@ -893,8 +906,14 @@ parse_args(argc, argv)
> SET(flags, MODE_BACKGROUND);
> break;
> case 'C':
> +#ifdef BSD
> + user_closefrom = (int)strtonum(optarg, 3, maxfiles, &errstr);
> + if (errstr) {
> + warningx("the argument to -C is invalid: %s", errstr);
> +#else
> if ((user_closefrom = atoi(optarg)) < 3) {
> warningx("the argument to -C must be at least 3");
> +#endif
> usage(1);
> }
> break;
> Index: usr.bin/vmstat/vmstat.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/vmstat/vmstat.c,v
> retrieving revision 1.134
> diff -u -p -r1.134 vmstat.c
> --- usr.bin/vmstat/vmstat.c 23 Nov 2014 04:34:48 -0000 1.134
> +++ usr.bin/vmstat/vmstat.c 9 Dec 2014 13:35:03 -0000
> @@ -129,6 +129,7 @@ int
> main(int argc, char *argv[])
> {
> char errbuf[_POSIX2_LINE_MAX];
> + char *preps;
> int c, todo = 0, reps = 0;
> const char *errstr;
> u_int interval = 0;
> @@ -136,7 +137,9 @@ main(int argc, char *argv[])
> while ((c = getopt(argc, argv, "c:fiM:mN:stw:vz")) != -1) {
> switch (c) {
> case 'c':
> - reps = atoi(optarg);
> + reps = (int)strtol(optarg, &preps, 10);
> + if (*preps != '\0' || errno != 0)
> + errx(1, "-c %s: invalid argument", optarg);
> break;
> case 'f':
> todo |= FORKSTAT;
> @@ -224,8 +227,11 @@ main(int argc, char *argv[])
> if (errstr)
> errx(1, "%s: %s", *argv, errstr);
>
> - if (*++argv)
> - reps = atoi(*argv);
> + if (*++argv) {
> + reps = (int)strtol(*argv, &preps, 10);
> + if (preps != '\0' || errno != 0)
> + errx(1, "%s: invalid argument", *argv);
> + }
> }
> #endif
>