From 8a378b1638d3bda2407ad29a3ddc4704e879c149 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 20 May 2016 10:45:12 +0200 Subject: [PATCH] compat: use strtonum instead of atoi When possible, use strtonum as it is more robust than atoi(). Also replace some strtol() call when possible. --- configure.ac | 1 + src/client/conf-lldp.c | 8 +++-- src/compat/compat.h | 4 +++ src/compat/strtonum.c | 67 ++++++++++++++++++++++++++++++++++++++++++ src/daemon/lldpd.c | 13 ++++++-- src/lib/atom.c | 8 +++-- 6 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 src/compat/strtonum.c diff --git a/configure.ac b/configure.ac index b7f4c8af..539384cb 100644 --- a/configure.ac +++ b/configure.ac @@ -181,6 +181,7 @@ AC_CHECK_FUNCS([setproctitle_init]) AC_REPLACE_FUNCS([strlcpy strnlen strndup + strtonum getline asprintf vsyslog diff --git a/src/client/conf-lldp.c b/src/client/conf-lldp.c index b8a5a130..8b6aa2cb 100644 --- a/src/client/conf-lldp.c +++ b/src/client/conf-lldp.c @@ -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; } } diff --git a/src/compat/compat.h b/src/compat/compat.h index 027e6481..3994265f 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -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 index 00000000..547f39af --- /dev/null +++ b/src/compat/strtonum.c @@ -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 +#include +#include + +#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); +} diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index d742b7e1..ba0b422b 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -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; diff --git a/src/lib/atom.c b/src/lib/atom.c index d7fd831c..39820739 100644 --- a/src/lib/atom.c +++ b/src/lib/atom.c @@ -17,6 +17,7 @@ #include #include +#include #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); -- 2.39.5