summaryrefslogtreecommitdiffstats
path: root/sys/netgraph/ng_base.c
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/netgraph/ng_base.c
parent379a856804bca45181540d407b067f13b50206fa (diff)
downloadFreeBSD-src-a481e1e52bd7ec5a593408ac4a14285997f6d2b9.zip
FreeBSD-src-a481e1e52bd7ec5a593408ac4a14285997f6d2b9.tar.gz
Simplify checking/parsing of strings using strtoul(), isdigit(), etc.
Diffstat (limited to 'sys/netgraph/ng_base.c')
-rw-r--r--sys/netgraph/ng_base.c42
1 files changed, 13 insertions, 29 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;
}
/*
OpenPOWER on IntegriCloud