Date: Tue, 04 Jan 2011 11:40:45 +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: <xeia7heluf2q.fsf@kobe.laptop> In-Reply-To: <xeiad3oduf9l.fsf@kobe.laptop> (Giorgos Keramidas's message of "Tue, 04 Jan 2011 11:36:38 %2B0100") 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> <xeiad3oduf9l.fsf@kobe.laptop>
next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 04 Jan 2011 11:36:38 +0100, Giorgos Keramidas <keramida@freebsd.org> wrote: > 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; > } instead of `return (int)x' the last bits should be slightly different, of course: if (value != NULL) *value = (int)x; return 0; }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xeia7heluf2q.fsf>