On Tue, Dec 09, 2014 at 13:38, David Carlier wrote:

> -            pid = atol(optarg);
> +            pid = (pid_t)strtonum(optarg, -1, (SHRT_MAX - 1), &errstr);
> +            if (errstr)
> +                errx(1, "pid: %s", errstr);

This should definitely use PID_MAX I think. (If that's not exported to
userland, then... I don't know. But I have on/off plans to increase
pid max to greater than short max.)

Also, as a matter of style, the return value should not need a cast.


> 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);

Why not use strtonum here? Also, in order to use the errno check, it
has to be set to 0 before the call to strtol, otherwise you can't be
sure what damaged it.

> +        if (*++argv) {
> +            reps = (int)strtol(*argv, &preps, 10);
> +            if (preps != '\0' || errno != 0)
> +                errx(1, "%s: invalid argument", *argv);

Watch preps. Missing deref.

Reply via email to