Date: Tue, 31 Jul 2001 08:45:18 -0700 (PDT) From: John Baldwin <jhb@FreeBSD.org> To: "David O'Brien" <obrien@FreeBSD.org> Cc: cvs-all@FreeBSD.org, cvs-committers@FreeBSD.org, Garrett Wollman <wollman@khavrinen.lcs.mit.edu> Subject: Re: cvs commit: src/usr.sbin/newsyslog Makefile newsyslog.c Message-ID: <XFMail.010731084518.jhb@FreeBSD.org> In-Reply-To: <20010730163112.A83693@dragon.nuxi.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 30-Jul-01 David O'Brien wrote: > On Mon, Jul 30, 2001 at 11:20:17AM -0700, John Baldwin wrote: >> > It looks like someone cut-and-pasted my canonical `parse an integer >> > correctly' code into a new function where the results were not used >> > carefully. It would be an error to use `strtol' to parse a number >> > which is not supposed to be negative. > ... >> Not to mention, the code in question doesn't check to see if strtoul() >> failed (it doesnt' check to see what the 't' var points to) which it >> would do for a negative number, yes? > > It does check. strtoul() is documented to return INT_MAX and set errno > in the negative input case. The code exits if the value from strtoul() > is > N (where N is a very small number). Thus the return value from > strtoul() is checked for validity. A problem with negative input is not > distinguished between invalid positive input. Incorrect: The strtoul() function returns either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conver- sion, unless the original (non-negated) value would overflow; in the lat- ter case, strtoul() returns ULONG_MAX. It only returns ULONG_MAX if the original value would overflow. strtoul() of '-4' will return -4: const char *s = "-4"; int main() { int x; x = strtoul(s, NULL, 0); printf("s = \"%s\", x = %d\n", s, x); return 0; } s = "-4", x = -4 Granted, when x is unsigned, you get: s = "-4", x = 4294967292 Which will be large enough for the cases assumed here. Does go to show that strtol() wouldn't fail in the negative case however as I thought. Hmm. This means that you probably could just have done s/strtoul/strtol/ to get rid of the warning. :) -- John Baldwin <jhb@FreeBSD.org> -- http://www.FreeBSD.org/~jhb/ PGP Key: http://www.baldwin.cx/~john/pgpkey.asc "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe cvs-all" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.010731084518.jhb>