[patch] have rtprio check that arguments are numeric;
change atoi to strtol
Giorgos Keramidas
keramida at freebsd.org
Tue Jan 4 10:36:55 UTC 2011
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;
}
Then you can replace all the atoi() calls with:
int x;
if (parseint(argv[1], NULL, 0, &x) != 0)
errx(1, "your message");
which is a relatively cleaner way of handling strtol() parsing errors,
without the associated clutter of checking properly all possible edge
cases at every call-point.
More information about the freebsd-hackers
mailing list