[patch] have rtprio check that arguments are numeric; change atoi to strtol

Giorgos Keramidas keramida at freebsd.org
Tue Jan 4 10:41:09 UTC 2011


On Tue, 04 Jan 2011 11:36:38 +0100, Giorgos Keramidas <keramida at freebsd.org> wrote:
> On Sun, 2 Jan 2011 18:46:47 -0500, Eitan Adler <lists at eitanadler.com> wrote:
>> What about this patch? I incorporated  your feedback so I am not going
>> to reply inline.
>
> Since the pattern of converting strings to int-derivative values appears
> multiple times, I'd probably prefer something like a new function that
> does the parsing *and* error-checking, to avoid duplication of errno
> checks, invalidchar checks, and so on, e.g. something like this near the
> top of rtprio.c:
>
>         #include <errno.h>
>         #include <limits.h>
>         #include <string.h>
>
>         /*
>          * Parse an integer from 'string' into 'value'.  Return the first
>          * invalid character in 'endp', if endp is non-NULL.  The return value
>          * of parseint is 0 on success, -1 for any parse error.
>          */
>
>         int
>         parseint(const char *string, const char **endp, int base, int *value)
>         {
>                 long x;
>
>                 errno = 0;
>                 x = strtol(string, endp, base);
>                 if (errno != 0 || endp == str || (endp != NULL &&
>                     endp != str && *endp != '\0' && (isdigit(*endp) == 0 ||
>                     isspace(*endp) != 0)))
>                         return -1;
>                 if (x >= INT_MAX) {
>                         errno = ERANGE;
>                         return -1;
>                 }
>                 return (int)x;
>         }

instead of `return (int)x' the last bits should be slightly different,
of course:

                  if (value != NULL)
                          *value = (int)x;
                  return 0;
          }



More information about the freebsd-hackers mailing list