bin/54672: [PATCH] fix gcc 3.3 compiler warning for ifconfig(8)
Bruce Evans
bde at zeta.org.au
Tue Jul 22 03:30:16 PDT 2003
The following reply was made to PR bin/54672; it has been noted by GNATS.
From: Bruce Evans <bde at zeta.org.au>
To: Lukas Ertl <l.ertl at univie.ac.at>
Cc: FreeBSD-gnats-submit at 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
More information about the freebsd-bugs
mailing list