aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Nykryn <lnykryn@redhat.com>2014-06-06 10:56:21 +0200
committerLukas Nykryn <lnykryn@redhat.com>2014-06-06 10:56:21 +0200
commit12bd8cf96b2af106470bcf1711048fa9ea513016 (patch)
treebf3dd322a8f9216372d537a7648234a868c40d62
parent536098c8896cde81b1f238a3b3ec6db76a20490a (diff)
downloadinitscripts-12bd8cf96b2af106470bcf1711048fa9ea513016.tar
initscripts-12bd8cf96b2af106470bcf1711048fa9ea513016.tar.gz
initscripts-12bd8cf96b2af106470bcf1711048fa9ea513016.tar.bz2
initscripts-12bd8cf96b2af106470bcf1711048fa9ea513016.tar.xz
initscripts-12bd8cf96b2af106470bcf1711048fa9ea513016.zip
ipcalc: parse prefix more safely
-rw-r--r--src/ipcalc.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/ipcalc.c b/src/ipcalc.c
index bec14eb5..6a803f11 100644
--- a/src/ipcalc.c
+++ b/src/ipcalc.c
@@ -30,6 +30,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
+#include <errno.h>
/*!
\file ipcalc.c
@@ -48,6 +49,24 @@
return host byte order, but there are some exceptions.
*/
+int safe_atoi(const char *s, int *ret_i) {
+ char *x = NULL;
+ long l;
+
+ errno = 0;
+ l = strtol(s, &x, 0);
+
+ if (!x || x == s || *x || errno)
+ return errno > 0 ? -errno : -EINVAL;
+
+ if ((long) (int) l != l)
+ return -ERANGE;
+
+ *ret_i = (int) l;
+ return 0;
+}
+
+
/*!
\fn struct in_addr prefix2mask(int bits)
\brief creates a netmask from a specified number of bits
@@ -291,8 +310,9 @@ int main(int argc, const char **argv) {
}
if (prefixStr != NULL) {
- prefix = atoi(prefixStr);
- if (prefix < 0 || ((familyIPv6 && prefix > 128) || (!familyIPv6 && prefix > 32))) {
+ int r = 0;
+ r = safe_atoi(prefixStr, &prefix);
+ if (r != 0 || prefix < 0 || ((familyIPv6 && prefix > 128) || (!familyIPv6 && prefix > 32))) {
if (!beSilent)
fprintf(stderr, "ipcalc: bad prefix: %s\n", prefixStr);
return 1;