Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 Jul 2003 03:30:15 -0700 (PDT)
From:      Bruce Evans <bde@zeta.org.au>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/54672: [PATCH] fix gcc 3.3 compiler warning for ifconfig(8)
Message-ID:  <200307221030.h6MAUFns098287@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/54672; it has been noted by GNATS.

From: Bruce Evans <bde@zeta.org.au>
To: Lukas Ertl <l.ertl@univie.ac.at>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/54672: [PATCH] fix gcc 3.3 compiler warning for ifconfig(8)
Date: Tue, 22 Jul 2003 20:22:36 +1000 (EST)

 On Mon, 21 Jul 2003, Lukas Ertl wrote:
 
 > On Mon, 21 Jul 2003, Bruce Evans wrote:
 >
 > > On Mon, 21 Jul 2003, Lukas Ertl wrote:
 > > > ...
 > > > +
 > > > +	if ((p = strchr(range, '-')) == NULL)
 > > > +		errx(1, "illegal range '%s'", range);
 > >
 > > strtoul() could be used to read up to the '-' more directly.
 > >
 > > There are complications for invalid formats with '-' signs in the
 > > numbers.  Both strtoul() and sscanf() will parse the '-' signs as
 > > parts of numbers, but we don't want that here.  I think we also
 > > don't want any leading whitespace.
 >
 > So we need strchr(), don't we?
 
 No.  strchr() will find the first '-' in the invalid input "-123-456",
 and I think recovering from this mess would be harder than not getting
 into it.  Another example of invalid input that tends to get accepted
 if any character except the digits is parsed in a non-per-char way:
 " \t-123- \t-456".  Pseudocode for rejecting minus signs and whitespace:
 
 	assert(isdigit((u_char)*range);
 	first = strtoul(range, &endptr, 10);
 	assert(*endptr == '-');
 	endptr++;
 	assert(isdigit((u_char)*endptr);
 	last = strtoul(endptr, &endptr, 10);
 	assert(*endptr == '\0');
 
 Hmm.  It wouldn't hurt to accept whitespace, and strtoul will handle
 minus signs in a way that will cause an error later ("-<digits>" -> 0
 or -ULONG_MAX), so we can let strtoul() parse almost everything after
 all:
 
 	first = strtoul(range, &endptr, 10);
 	assert(endptr != range && *endptr == '-');
 	lastptr = endptr + 1;
 	last = strtoul(lastptr, &endptr, 10);
 	assert(endptr != lastptr && *endptr == '\0');
 
 Bruce



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200307221030.h6MAUFns098287>