]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
compat: use strtonum instead of atoi
authorVincent Bernat <vincent@bernat.im>
Fri, 20 May 2016 08:45:12 +0000 (10:45 +0200)
committerVincent Bernat <vincent@bernat.im>
Fri, 20 May 2016 08:45:12 +0000 (10:45 +0200)
When possible, use strtonum as it is more robust than atoi(). Also
replace some strtol() call when possible.

configure.ac
src/client/conf-lldp.c
src/compat/compat.h
src/compat/strtonum.c [new file with mode: 0644]
src/daemon/lldpd.c
src/lib/atom.c

index b7f4c8afbb0ca5a90417f46c1d2ed2c6dfa9a9c5..539384cbbd48ba35bc1e4f929909b964bb1f90b8 100644 (file)
@@ -181,6 +181,7 @@ AC_CHECK_FUNCS([setproctitle_init])
 AC_REPLACE_FUNCS([strlcpy
                   strnlen
                   strndup
+                  strtonum
                   getline
                   asprintf
                   vsyslog
index b8a5a130b1ed05d1e727ba202a947b166e36d0ee..8b6aa2cbadad4a05691fd764d708ed7b2a3f4798 100644 (file)
@@ -261,9 +261,11 @@ cmd_custom_tlv_set(struct lldpctl_conn_t *conn, struct writer *w,
                log_warnx("lldpctl", "no subtype specified");
                return 0;
        } else {
-               subtype = (uint16_t)atoi(s);
-               if (subtype > 255) {
-                       log_warnx("lldpctl", "invalid subtype range  value '%s'", s);
+               const char *errstr;
+               subtype = strtonum(s, 0, 255, &errstr);
+               if (errstr != NULL) {
+                       log_warnx("lldpctl", "invalid subtype value `%s': %s",
+                           s, errstr);
                        return 0;
                }
        }
index 027e64811e3ac81b12b3250bd9712c5a688dd3c5..3994265fa04a2f0f0d091fc9655ae0629a076c30 100644 (file)
@@ -70,6 +70,10 @@ size_t       strnlen(const char *, size_t);
 char   *strndup(const char *, size_t);
 #endif
 
+#if !HAVE_STRTONUM
+long long strtonum(const char *, long long, long long, const char **);
+#endif
+
 #if !HAVE_GETLINE
 ssize_t getline(char **, size_t *, FILE *);
 #endif
diff --git a/src/compat/strtonum.c b/src/compat/strtonum.c
new file mode 100644 (file)
index 0000000..547f39a
--- /dev/null
@@ -0,0 +1,67 @@
+/* -*- mode: c; c-file-style: "openbsd" -*- */
+
+/*     $OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $   */
+
+/*
+ * Copyright (c) 2004 Ted Unangst and Todd Miller
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#define        INVALID         1
+#define        TOOSMALL        2
+#define        TOOLARGE        3
+
+long long
+strtonum(const char *numstr, long long minval, long long maxval,
+    const char **errstrp)
+{
+       long long ll = 0;
+       int error = 0;
+       char *ep;
+       struct errval {
+               const char *errstr;
+               int err;
+       } ev[4] = {
+               { NULL,         0 },
+               { "invalid",    EINVAL },
+               { "too small",  ERANGE },
+               { "too large",  ERANGE },
+       };
+
+       ev[0].err = errno;
+       errno = 0;
+       if (minval > maxval) {
+               error = INVALID;
+       } else {
+               ll = strtoll(numstr, &ep, 10);
+               if (numstr == ep || *ep != '\0')
+                       error = INVALID;
+               else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
+                       error = TOOSMALL;
+               else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
+                       error = TOOLARGE;
+       }
+       if (errstrp != NULL)
+               *errstrp = ev[error].errstr;
+       errno = ev[error].err;
+       if (error)
+               ll = 0;
+
+       return (ll);
+}
index d742b7e19d756a8fef5661827e702b9a358237fa..ba0b422b92b3338978f9a85ff7993a6145519785 100644 (file)
@@ -1432,6 +1432,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
        struct lldpd *cfg;
        struct lldpd_chassis *lchassis;
        int ch, debug = 0, use_syslog = 1, daemonize = 1;
+       const char *errstr;
 #ifdef USE_SNMP
        int snmp = 0;
        const char *agentx = NULL;      /* AgentX socket */
@@ -1541,8 +1542,8 @@ lldpd_main(int argc, char *argv[], char *envp[])
                        break;
 #ifdef ENABLE_LLDPMED
                case 'M':
-                       lldpmed = atoi(optarg);
-                       if ((lldpmed < 1) || (lldpmed > 4)) {
+                       lldpmed = strtonum(optarg, 1, 4, &errstr);
+                       if (errstr) {
                                fprintf(stderr, "-M requires an argument between 1 and 4\n");
                                usage();
                        }
@@ -1591,7 +1592,13 @@ lldpd_main(int argc, char *argv[], char *envp[])
                        platform_override = strdup(optarg);
                        break;
                case 'H':
-                       smart = atoi(optarg);
+                       smart = strtonum(optarg, 0, sizeof(filters)/sizeof(filters[0]),
+                           &errstr);
+                       if (errstr) {
+                               fprintf(stderr, "-H requires an int between 0 and %zu\n",
+                                   sizeof(filters)/sizeof(filters[0]));
+                               usage();
+                       }
                        break;
                default:
                        found = 0;
index d7fd831ca9c18fed04f4301c1cbc9bcb2af873bd..39820739609d0a99ed20211a0ddee607abeee956 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <limits.h>
 #include "lldpctl.h"
 #include "atom.h"
 #include "../log.h"
@@ -137,7 +138,8 @@ lldpctl_atom_set_str(lldpctl_atom_t *atom, lldpctl_key_t key,
 {
        lldpctl_atom_t *result = NULL;
        char *end;
-       long int converted = 0;
+       const char *errstr;
+       long long converted = 0;
        int isint = 0;
        int bad = 0;
 
@@ -154,8 +156,8 @@ lldpctl_atom_set_str(lldpctl_atom_t *atom, lldpctl_key_t key,
        }
 
        if (value) {
-               converted = strtol(value, &end, 0);
-               isint = (end != value && *end == '\0');
+               converted = strtonum(value, LLONG_MIN, LLONG_MAX, &errstr);
+               isint = (errstr == NULL);
        }
 
        RESET_ERROR(atom->conn);