summaryrefslogtreecommitdiffstats
path: root/sys
diff options
context:
space:
mode:
authorarchie <archie@FreeBSD.org>1999-11-02 23:18:01 +0000
committerarchie <archie@FreeBSD.org>1999-11-02 23:18:01 +0000
commita481e1e52bd7ec5a593408ac4a14285997f6d2b9 (patch)
tree933dc4cbd306643fb4cfaef4087fa80525fd8420 /sys
parent379a856804bca45181540d407b067f13b50206fa (diff)
downloadFreeBSD-src-a481e1e52bd7ec5a593408ac4a14285997f6d2b9.zip
FreeBSD-src-a481e1e52bd7ec5a593408ac4a14285997f6d2b9.tar.gz
Simplify checking/parsing of strings using strtoul(), isdigit(), etc.
Diffstat (limited to 'sys')
-rw-r--r--sys/netgraph/ng_base.c42
-rw-r--r--sys/netgraph/ng_frame_relay.c15
-rw-r--r--sys/netgraph/ng_ppp.c29
-rw-r--r--sys/netgraph/ng_sample.c18
4 files changed, 38 insertions, 66 deletions
diff --git a/sys/netgraph/ng_base.c b/sys/netgraph/ng_base.c
index 48df023..2fb0fc7 100644
--- a/sys/netgraph/ng_base.c
+++ b/sys/netgraph/ng_base.c
@@ -55,6 +55,7 @@
#include <sys/queue.h>
#include <sys/mbuf.h>
#include <sys/socketvar.h>
+#include <machine/limits.h>
#include <net/netisr.h>
@@ -410,37 +411,20 @@ ng_findname(node_p this, const char *name)
static ng_ID_t
ng_decodeidname(const char *name)
{
- ng_ID_t val;
- int k, len;
-
- /* Basic checks */
- for (len = 0; name[len] != '\0'; len++) {
- const char ch = name[len];
-
- if (len == 0) {
- if (ch != '[')
- return (NULL);
- } else if (name[len + 1] == '\0') {
- if (ch != ']')
- return (NULL);
- } else if (!((ch >= '0' && ch <= '9')
- || (ch >= 'a' && ch <= 'f')
- || (ch >= 'A' && ch <= 'F')))
- return (NULL);
- }
- if (len < 3 || len > (sizeof(val) * 2) + 2 || name[1] == '0')
- return (NULL);
+ const int len = strlen(name);
+ const char *eptr;
+ u_long val;
- /* Convert number to binary */
- for (val = 0, k = 1; k < len - 1; k++) {
- const char ch = name[k];
+ /* Check for proper length, brackets, no leading junk */
+ if (len < 3 || name[0] != '[' || name[len - 1] != ']'
+ || !isxdigit(name[1]) || name[1] == '0') /* "[0]" is not valid */
+ return (0);
- if (ch <= '9')
- val = (val << 4) | ((ch - '0') & 0xf);
- else
- val = (val << 4) | (((ch & 0xdf) - 'A' + 10) & 0xf);
- }
- return (val);
+ /* Decode number */
+ val = strtoul(name + 1, &eptr, 16);
+ if (eptr - name != len - 1 || val == ULONG_MAX)
+ return (0);
+ return (ng_ID_t)val;
}
/*
diff --git a/sys/netgraph/ng_frame_relay.c b/sys/netgraph/ng_frame_relay.c
index a0466d0..6be8d17 100644
--- a/sys/netgraph/ng_frame_relay.c
+++ b/sys/netgraph/ng_frame_relay.c
@@ -246,9 +246,7 @@ static int
ngfrm_newhook(node_p node, hook_p hook, const char *name)
{
const sc_p sc = node->private;
- const char *cp;
- char c = '\0';
- int digits = 0;
+ const char *cp, *eptr;
int dlci = 0;
int ctxnum;
@@ -285,13 +283,12 @@ ngfrm_newhook(node_p node, hook_p hook, const char *name)
/* Must be a dlci hook at this point */
cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
- while ((digits < 5) && ((c = *cp++) >= '0') && (c <= '9')) {
- dlci *= 10;
- dlci += c - '0';
- digits++;
- }
- if ((c != 0) || (digits == 5) || (dlci < 0) || (dlci > 1023))
+ if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
+ return (EINVAL);
+ dlci = (int)strtoul(cp, &eptr, 10);
+ if (*eptr != '\0' || dlci < 0 || dlci > 1023)
return (EINVAL);
+
/*
* We have a dlci, now either find it, or allocate it. It's possible
* that we might have seen packets for it already and made an entry
diff --git a/sys/netgraph/ng_ppp.c b/sys/netgraph/ng_ppp.c
index 46ce2aa..960726ee 100644
--- a/sys/netgraph/ng_ppp.c
+++ b/sys/netgraph/ng_ppp.c
@@ -60,14 +60,14 @@
#include <netgraph/ng_vjc.h>
#define PROT_VALID(p) (((p) & 0x0101) == 0x0001)
-#define PROT_COMPRESSIBLE(p) (((p) & 0xff00) == 0x0000)
+#define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000)
/* Some PPP protocol numbers we're interested in */
#define PROT_APPLETALK 0x0029
#define PROT_COMPD 0x00fd
#define PROT_CRYPTD 0x0053
#define PROT_IP 0x0021
-#define PROT_IPX 0x002B
+#define PROT_IPX 0x002b
#define PROT_MP 0x003d
#define PROT_VJCOMP 0x002d
#define PROT_VJUNCOMP 0x002f
@@ -267,22 +267,13 @@ ng_ppp_newhook(node_p node, hook_p hook, const char *name)
/* Figure out which hook it is */
if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */
strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
- int gotDigit = 0;
- const char *cp;
-
- for (cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
- *cp >= '0' && *cp <= '9'; cp++) {
- if (!gotDigit) {
- if (*cp == '0') /* no leading zeros */
- return (EINVAL);
- linkNum = *cp - '0';
- gotDigit = 1;
- } else
- linkNum = (linkNum * 10) + (*cp - '0');
- if (linkNum >= NG_PPP_MAX_LINKS)
- return (EINVAL);
- }
- if (!gotDigit || *cp != '\0')
+ const char *cp, *eptr;
+
+ cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
+ if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
+ return (EINVAL);
+ linkNum = (int)strtoul(cp, &eptr, 10);
+ if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
return (EINVAL);
hookPtr = &priv->links[linkNum];
hookIndex = ~linkNum;
@@ -1292,7 +1283,7 @@ ng_ppp_intcmp(const void *v1, const void *v2)
static struct mbuf *
ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
{
- int psize = (PROT_COMPRESSIBLE(proto) && compOK) ? 1 : 2;
+ int psize = (PROT_COMPRESSABLE(proto) && compOK) ? 1 : 2;
/* Add protocol number */
M_PREPEND(m, psize, M_NOWAIT);
diff --git a/sys/netgraph/ng_sample.c b/sys/netgraph/ng_sample.c
index 6cc365c..8e2da75 100644
--- a/sys/netgraph/ng_sample.c
+++ b/sys/netgraph/ng_sample.c
@@ -176,17 +176,17 @@ ng_xxx_newhook(node_p node, hook_p hook, const char *name)
* hooks start with 'dlci' and have a decimal trailing channel
* number up to 4 digits Use the leadin defined int he associated .h
* file. */
- if (strncmp(name, NG_XXX_HOOK_DLCI_LEADIN, 4) == 0) {
+ if (strncmp(name,
+ NG_XXX_HOOK_DLCI_LEADIN, strlen(NG_XXX_HOOK_DLCI_LEADIN)) == 0) {
+ const char *eptr;
+
cp = name + sizeof(NG_XXX_HOOK_DLCI_LEADIN);
- while ((digits < 5)
- && ((c = *cp++) > '0') && (c < '9')) {
- dlci *= 10;
- dlci += c - '0';
- digits++;
- }
- if ((c != 0) || (digits == 5)
- || (dlci <= 0) || (dlci > 1023))
+ if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
return (EINVAL);
+ dlci = (int)strtoul(cp, &eptr, 10);
+ if (*eptr != '\0' || dlci < 0 || dlci > 1023)
+ return (EINVAL);
+
/* We have a dlci, now either find it, or allocate it */
for (chan = 0; chan < XXX_NUM_DLCIS; chan++)
if (xxxp->channel[chan].dlci == dlci)
OpenPOWER on IntegriCloud