From a068ff609288b61f86b37b02f06755f84c5aaca7 Mon Sep 17 00:00:00 2001 From: Guillaume Nault Date: Wed, 6 Feb 2013 17:29:11 +0100 Subject: utils: Add function for strict str to int conversion Define the u_readlong() function as a wrapper for strtol(). It performs stricter checks on the input value and let the caller specify an interval of acceptable values. Signed-off-by: Guillaume Nault --- accel-pppd/utils.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'accel-pppd/utils.c') diff --git a/accel-pppd/utils.c b/accel-pppd/utils.c index 491e650..a6a76c9 100644 --- a/accel-pppd/utils.c +++ b/accel-pppd/utils.c @@ -1,4 +1,6 @@ +#include #include +#include #include "triton.h" #include "utils.h" @@ -9,3 +11,22 @@ void __export u_inet_ntoa(in_addr_t addr, char *str) { sprintf(str, "%i.%i.%i.%i", addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff); } + +int __export u_readlong(long int *dst, const char *src, + long int min, long int max) +{ + char *src_stop = NULL; + long int rv; + + if (dst == NULL || src == NULL || src[0] == '\0') + return -1; + + errno = 0; + rv = strtol(src, &src_stop, 0); + if (errno != 0 || *src_stop != '\0' || rv < min || rv > max) { + return -1; + } else { + *dst = rv; + return 0; + } +} -- cgit v1.2.3