From owner-cvs-all Tue Jul 31 8:45:48 2001 Delivered-To: cvs-all@freebsd.org Received: from mail.wrs.com (unknown-1-11.windriver.com [147.11.1.11]) by hub.freebsd.org (Postfix) with ESMTP id AF7D937B406; Tue, 31 Jul 2001 08:45:31 -0700 (PDT) (envelope-from jhb@FreeBSD.org) Received: from laptop.baldwin.cx (john@[147.11.46.217]) by mail.wrs.com (8.9.3/8.9.1) with ESMTP id IAA27114; Tue, 31 Jul 2001 08:45:30 -0700 (PDT) Message-ID: X-Mailer: XFMail 1.4.0 on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: <20010730163112.A83693@dragon.nuxi.com> Date: Tue, 31 Jul 2001 08:45:18 -0700 (PDT) From: John Baldwin To: "David O'Brien" Subject: Re: cvs commit: src/usr.sbin/newsyslog Makefile newsyslog.c Cc: cvs-all@FreeBSD.org, cvs-committers@FreeBSD.org, Garrett Wollman Sender: owner-cvs-all@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 -- 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