Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Jul 2003 13:51:23 +0200 (CEST)
From:      Lukas Ertl <l.ertl@univie.ac.at>
To:        Bruce Evans <bde@zeta.org.au>
Cc:        FreeBSD-gnats-submit@freebsd.org
Subject:   Re: bin/54672: [PATCH] fix gcc 3.3 compiler warning for ifconfig(8)
Message-ID:  <20030721134859.I625@korben.in.tern>
In-Reply-To: <20030721161710.Q3367@gamplex.bde.org>
References:  <200307202006.h6KK62li015250@korben.in.tern> <20030721161710.Q3367@gamplex.bde.org>

next in thread | previous in thread | raw e-mail | index | archive | help

On Mon, 21 Jul 2003, Bruce Evans wrote:

> On Sun, 20 Jul 2003, Lukas Ertl wrote:
>
> > first and last are both declared as u_short, which can't hold values larger
> > then 0xffff, so the comparison isn't needed.
>
> This is machine-dependent.  u_short can hold values up to USHRT_MAX, which
> is >= 0xffff (perhaps strictly larger).
>
> There doesn't seem to be any good reason to use u_shorts; similar code in
> at_getaddr() uses u_ints so it accidentally avoids the warning except on
> machines with 16-bit u_ints.
>
> Using strtoul() as mentioned in the XXX before the above code would avoid
> the warning less accidentally since 0xffff < ULONG_MAX on all machines.

Well, would this patch be better:

---8<---
Index: sbin/ifconfig/ifconfig.c
===================================================================
RCS file: /usr/local/bsdcvs/src/sbin/ifconfig/ifconfig.c,v
retrieving revision 1.90
diff -u -u -r1.90 ifconfig.c
--- sbin/ifconfig/ifconfig.c	28 Apr 2003 16:37:38 -0000	1.90
+++ sbin/ifconfig/ifconfig.c	21 Jul 2003 11:50:33 -0000
@@ -1680,17 +1680,34 @@
 	bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
 }

-/* XXX  FIXME -- should use strtoul for better parsing. */
 void
 setatrange(const char *range, int dummy __unused, int s,
     const struct afswtch *afp)
 {
-	u_short	first = 123, last = 123;
+	u_int	first = 123, last = 123;
+	char	*p, *endptr;
+
+	if ((p = strchr(range, '-')) == NULL)
+		errx(1, "illegal range '%s'", range);
+
+	*p = '\0';
+	p++;
+
+	if ((*range == '\0') || (*p == '\0'))
+		errx(1, "illegal range '%s-%s'", range, p);
+
+	first = strtoul(range, &endptr, 10);
+	if (endptr == range || *endptr != '\0')
+		errx(1, "illegal range '%s-%s'", range, p);
+
+	last = strtoul(p, &endptr, 10);
+	if (endptr == p || *endptr != '\0')
+		errx(1, "illegal range '%s-%s'", range, p);
+
+	if (first == 0 || first > 0xffff || last == 0 || last > 0xffff
+	    || first > last)
+		errx(1, "%s-%s: illegal net range: %u-%u", range, p, first, last);

-	if (sscanf(range, "%hu-%hu", &first, &last) != 2
-	    || first == 0 || first > 0xffff
-	    || last == 0 || last > 0xffff || first > last)
-		errx(1, "%s: illegal net range: %u-%u", range, first, last);
 	at_nr.nr_firstnet = htons(first);
 	at_nr.nr_lastnet = htons(last);
 }
---8<---

regards,
le

-- 
Lukas Ertl                             eMail: l.ertl@univie.ac.at
UNIX-Systemadministrator               Tel.:  (+43 1) 4277-14073
Zentraler Informatikdienst (ZID)       Fax.:  (+43 1) 4277-9140
der Universität Wien                   http://mailbox.univie.ac.at/~le/



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