No need for temporary variables either, strtonum guarantees through
maxval that its return value won't overflow when casted.
Index: brconfig.c
===================================================================
RCS file: /cvs/src/sbin/ifconfig/brconfig.c,v
retrieving revision 1.15
diff -u -p -r1.15 brconfig.c
--- brconfig.c 7 Jun 2017 16:47:29 -0000 1.15
+++ brconfig.c 9 Jun 2017 17:33:10 -0000
@@ -220,7 +220,6 @@ bridge_ifsetflag(const char *ifsname, u_
err(1, "%s: ioctl SIOCBRDGGIFFLGS %s", name, ifsname);
req.ifbr_ifsflags |= flag & ~IFBIF_RO_MASK;
-
if (ioctl(s, SIOCBRDGSIFFLGS, (caddr_t)&req) < 0)
err(1, "%s: ioctl SIOCBRDGSIFFLGS %s", name, ifsname);
}
@@ -232,12 +231,10 @@ bridge_ifclrflag(const char *ifsname, u_
strlcpy(req.ifbr_name, name, sizeof(req.ifbr_name));
strlcpy(req.ifbr_ifsname, ifsname, sizeof(req.ifbr_ifsname));
-
if (ioctl(s, SIOCBRDGGIFFLGS, (caddr_t)&req) < 0)
err(1, "%s: ioctl SIOCBRDGGIFFLGS %s", name, ifsname);
req.ifbr_ifsflags &= ~(flag | IFBIF_RO_MASK);
-
if (ioctl(s, SIOCBRDGSIFFLGS, (caddr_t)&req) < 0)
err(1, "%s: ioctl SIOCBRDGSIFFLGS %s", name, ifsname);
}
@@ -400,18 +397,13 @@ void
bridge_timeout(const char *arg, int d)
{
struct ifbrparam bp;
- long newtime;
- char *endptr;
+ const char *errstr;
- errno = 0;
- newtime = strtol(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' ||
- (newtime & ~INT_MAX) != 0L ||
- (errno == ERANGE && newtime == LONG_MAX))
- errx(1, "invalid arg for timeout: %s", arg);
+ bp.ifbrp_ctime = strtonum(arg, 0, INT_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for timeout: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_ctime = newtime;
if (ioctl(s, SIOCBRDGSTO, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
@@ -420,17 +412,13 @@ void
bridge_maxage(const char *arg, int d)
{
struct ifbrparam bp;
- unsigned long v;
- char *endptr;
+ const char *errstr;
- errno = 0;
- v = strtoul(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' || v > 0xffUL ||
- (errno == ERANGE && v == ULONG_MAX))
- errx(1, "invalid arg for maxage: %s", arg);
+ bp.ifbrp_maxage = strtonum(arg, 0, UINT8_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for maxage: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_maxage = v;
if (ioctl(s, SIOCBRDGSMA, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
@@ -439,17 +427,13 @@ void
bridge_priority(const char *arg, int d)
{
struct ifbrparam bp;
- unsigned long v;
- char *endptr;
+ const char *errstr;
- errno = 0;
- v = strtoul(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' || v > 0xffffUL ||
- (errno == ERANGE && v == ULONG_MAX))
- errx(1, "invalid arg for spanpriority: %s", arg);
+ bp.ifbrp_prio = strtonum(arg, 0, UINT16_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for spanpriority: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_prio = v;
if (ioctl(s, SIOCBRDGSPRI, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
@@ -478,17 +462,13 @@ void
bridge_fwddelay(const char *arg, int d)
{
struct ifbrparam bp;
- unsigned long v;
- char *endptr;
+ const char *errstr;
- errno = 0;
- v = strtoul(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' || v > 0xffUL ||
- (errno == ERANGE && v == ULONG_MAX))
- errx(1, "invalid arg for fwddelay: %s", arg);
+ bp.ifbrp_fwddelay = strtonum(arg, 0, UINT8_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for fwddelay: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_fwddelay = v;
if (ioctl(s, SIOCBRDGSFD, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
@@ -497,17 +477,13 @@ void
bridge_hellotime(const char *arg, int d)
{
struct ifbrparam bp;
- unsigned long v;
- char *endptr;
+ const char *errstr;
- errno = 0;
- v = strtoul(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' || v > 0xffUL ||
- (errno == ERANGE && v == ULONG_MAX))
- errx(1, "invalid arg for hellotime: %s", arg);
+ bp.ifbrp_hellotime = strtonum(arg, 0, UINT8_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for hellotime: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_hellotime = v;
if (ioctl(s, SIOCBRDGSHT, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
@@ -516,17 +492,13 @@ void
bridge_maxaddr(const char *arg, int d)
{
struct ifbrparam bp;
- unsigned long newsize;
- char *endptr;
+ const char *errstr;
- errno = 0;
- newsize = strtoul(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' || newsize > 0xffffffffUL ||
- (errno == ERANGE && newsize == ULONG_MAX))
- errx(1, "invalid arg for maxaddr: %s", arg);
+ bp.ifbrp_csize = strtonum(arg, 0, UINT32_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for maxaddr: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_csize = newsize;
if (ioctl(s, SIOCBRDGSCACHE, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
@@ -537,58 +509,46 @@ bridge_deladdr(const char *addr, int d)
struct ifbareq ifba;
struct ether_addr *ea;
- strlcpy(ifba.ifba_name, name, sizeof(ifba.ifba_name));
ea = ether_aton(addr);
if (ea == NULL)
err(1, "Invalid address: %s", addr);
+ strlcpy(ifba.ifba_name, name, sizeof(ifba.ifba_name));
bcopy(ea, &ifba.ifba_dst, sizeof(struct ether_addr));
-
if (ioctl(s, SIOCBRDGDADDR, &ifba) < 0)
err(1, "%s: %s", name, addr);
}
void
-bridge_ifprio(const char *ifname, const char *val)
+bridge_ifprio(const char *ifname, const char *arg)
{
struct ifbreq breq;
- unsigned long v;
- char *endptr;
+ const char *errstr;
+
+ breq.ifbr_priority = strtonum(arg, 0, UINT8_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for ifpriority: %s", errstr, arg);
strlcpy(breq.ifbr_name, name, sizeof(breq.ifbr_name));
strlcpy(breq.ifbr_ifsname, ifname, sizeof(breq.ifbr_ifsname));
-
- errno = 0;
- v = strtoul(val, &endptr, 0);
- if (val[0] == '\0' || endptr[0] != '\0' || v > 0xffUL ||
- (errno == ERANGE && v == ULONG_MAX))
- err(1, "invalid arg for ifpriority: %s", val);
- breq.ifbr_priority = v;
-
if (ioctl(s, SIOCBRDGSIFPRIO, (caddr_t)&breq) < 0)
- err(1, "%s: %s", name, val);
+ err(1, "%s: %s", name, arg);
}
void
-bridge_ifcost(const char *ifname, const char *val)
+bridge_ifcost(const char *ifname, const char *arg)
{
struct ifbreq breq;
- unsigned long v;
- char *endptr;
+ const char *errstr;
+
+ breq.ifbr_path_cost = strtonum(arg, 0, UINT32_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for ifcost: %s", errstr, arg);
strlcpy(breq.ifbr_name, name, sizeof(breq.ifbr_name));
strlcpy(breq.ifbr_ifsname, ifname, sizeof(breq.ifbr_ifsname));
-
- errno = 0;
- v = strtoul(val, &endptr, 0);
- if (val[0] == '\0' || endptr[0] != '\0' || v > 0xffffffffUL ||
- (errno == ERANGE && v == ULONG_MAX))
- errx(1, "invalid arg for ifcost: %s", val);
-
- breq.ifbr_path_cost = v;
-
if (ioctl(s, SIOCBRDGSIFCOST, (caddr_t)&breq) < 0)
- err(1, "%s: %s", name, val);
+ err(1, "%s: %s", name, arg);
}
void
@@ -598,9 +558,7 @@ bridge_noifcost(const char *ifname, int
strlcpy(breq.ifbr_name, name, sizeof(breq.ifbr_name));
strlcpy(breq.ifbr_ifsname, ifname, sizeof(breq.ifbr_ifsname));
-
breq.ifbr_path_cost = 0;
-
if (ioctl(s, SIOCBRDGSIFCOST, (caddr_t)&breq) < 0)
err(1, "%s", name);
}
@@ -611,16 +569,14 @@ bridge_addaddr(const char *ifname, const
struct ifbareq ifba;
struct ether_addr *ea;
- strlcpy(ifba.ifba_name, name, sizeof(ifba.ifba_name));
- strlcpy(ifba.ifba_ifsname, ifname, sizeof(ifba.ifba_ifsname));
-
ea = ether_aton(addr);
if (ea == NULL)
errx(1, "Invalid address: %s", addr);
+ strlcpy(ifba.ifba_name, name, sizeof(ifba.ifba_name));
+ strlcpy(ifba.ifba_ifsname, ifname, sizeof(ifba.ifba_ifsname));
bcopy(ea, &ifba.ifba_dst, sizeof(struct ether_addr));
ifba.ifba_flags = IFBAF_STATIC;
-
if (ioctl(s, SIOCBRDGSADDR, &ifba) < 0)
err(1, "%s: %s", name, addr);
}
@@ -649,7 +605,6 @@ bridge_addrs(const char *delim, int d)
ifbac.ifbac_buf = inbuf = inb;
strlcpy(ifbac.ifbac_name, name, sizeof(ifbac.ifbac_name));
if (ioctl(s, SIOCBRDGRTS, &ifbac) < 0) {
- if (errno == ENETDOWN)
return;
err(1, "%s", name);
}
@@ -676,14 +631,14 @@ bridge_addrs(const char *delim, int d)
}
void
-bridge_holdcnt(const char *value, int d)
+bridge_holdcnt(const char *arg, int d)
{
struct ifbrparam bp;
const char *errstr;
- bp.ifbrp_txhc = strtonum(value, 0, UINT8_MAX, &errstr);
- if (errstr)
- err(1, "holdcnt %s %s", value, errstr);
+ bp.ifbrp_txhc = strtonum(arg, 0, UINT8_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for holdcnt: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
if (ioctl(s, SIOCBRDGSTXHC, (caddr_t)&bp) < 0)
@@ -700,7 +655,6 @@ is_bridge(char *brdg)
struct ifbaconf ifbac;
strlcpy(ifr.ifr_name, brdg, sizeof(ifr.ifr_name));
-
if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0)
return (0);
@@ -1046,36 +1000,29 @@ void
switch_datapathid(const char *arg, int d)
{
struct ifbrparam bp;
- uint64_t newdpid;
- char *endptr;
+ const char *errstr;
- errno = 0;
- newdpid = strtoull(arg, &endptr, 0);
- if (arg[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
- errx(1, "invalid arg for datapath-id: %s", arg);
+ bp.ifbrp_datapath = strtonum(arg, 0, UINT64_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for datapath-id: %s", errstr, arg);
strlcpy(bp.ifbrp_name, name, sizeof(bp.ifbrp_name));
- bp.ifbrp_datapath = newdpid;
if (ioctl(s, SIOCSWSDPID, (caddr_t)&bp) < 0)
err(1, "%s", name);
}
void
-switch_portno(const char *ifname, const char *val)
+switch_portno(const char *ifname, const char *arg)
{
struct ifbreq breq;
- uint32_t newportidx;
- char *endptr;
+ const char *errstr;
+
+ breq.ifbr_portno = strtonum(arg, 0, UINT32_MAX, &errstr);
+ if (errstr != NULL)
+ errx(1, "%s arg for portidx: %s", errstr, arg);
strlcpy(breq.ifbr_name, name, sizeof(breq.ifbr_name));
strlcpy(breq.ifbr_ifsname, ifname, sizeof(breq.ifbr_ifsname));
-
- errno = 0;
- newportidx = strtol(val, &endptr, 0);
- if (val[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
- errx(1, "invalid arg for portidx: %s", val);
-
- breq.ifbr_portno = newportidx;
if (ioctl(s, SIOCSWSPORTNO, (caddr_t)&breq) < 0)
err(1, "%s", name);
}