AC_REPLACE_FUNCS([strlcpy
strnlen
strndup
+ strtonum
getline
asprintf
vsyslog
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;
}
}
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
--- /dev/null
+/* -*- 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);
+}
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 */
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();
}
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;
#include <stdio.h>
#include <string.h>
+#include <limits.h>
#include "lldpctl.h"
#include "atom.h"
#include "../log.h"
{
lldpctl_atom_t *result = NULL;
char *end;
- long int converted = 0;
+ const char *errstr;
+ long long converted = 0;
int isint = 0;
int bad = 0;
}
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);