Date: Tue, 04 Jan 2011 11:36:38 +0100 From: Giorgos Keramidas <keramida@freebsd.org> To: Eitan Adler <lists@eitanadler.com> Cc: Kostik Belousov <kostikbel@gmail.com>, hackers@freebsd.org Subject: Re: [patch] have rtprio check that arguments are numeric; change atoi to strtol Message-ID: <xeiad3oduf9l.fsf@kobe.laptop> In-Reply-To: <AANLkTinfKYYy0s3_LLvgRZtx=hGjbZP4%2Bqtnk31oi9GP@mail.gmail.com> (Eitan Adler's message of "Sun, 2 Jan 2011 18:46:47 -0500") References: <AANLkTimiJPiHBSw5i5TVJYfh9uGOyrNJx%2BoUPeB%2Bt%2BY_@mail.gmail.com> <6BEFA669-2E1F-44E6-897A-0A51DA939A74@gmail.com> <AANLkTimmQHRoQOZBgfheds=_sv5SMzD9DrPfYk9em7j8@mail.gmail.com> <AANLkTimdEpZ1DBoKU9g6w-j=F9KS0ONvo9racgOesdPf@mail.gmail.com> <20110102101845.GC90883@deviant.kiev.zoral.com.ua> <AANLkTinfKYYy0s3_LLvgRZtx=hGjbZP4%2Bqtnk31oi9GP@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 2 Jan 2011 18:46:47 -0500, Eitan Adler <lists@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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xeiad3oduf9l.fsf>