diff options
author | Swen Schillig <swen@linux.ibm.com> | 2019-01-28 12:54:07 +0100 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2019-03-01 00:32:10 +0000 |
commit | c9f4b92a6131dedcaa38d6fe907d17a30a595f06 (patch) | |
tree | 58464a590db1ffce09885e24b5b3f16e43b3e3f1 /source3/lib | |
parent | cef18c2dfd60be372eee10cbcefd5849ae979d31 (diff) | |
download | samba-c9f4b92a6131dedcaa38d6fe907d17a30a595f06.tar.gz |
lib: Use wrapper for string to integer conversion
In order to detect an value overflow error during
the string to integer conversion with strtoul/strtoull,
the errno variable must be set to zero before the execution and
checked after the conversion is performed. This is achieved by
using the wrapper function strtoul_err and strtoull_err.
Signed-off-by: Swen Schillig <swen@linux.ibm.com>
Reviewed-by: Ralph Böhme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/interface.c | 18 | ||||
-rw-r--r-- | source3/lib/messages_dgm.c | 15 | ||||
-rw-r--r-- | source3/lib/namemap_cache.c | 17 | ||||
-rw-r--r-- | source3/lib/sysquotas.c | 7 | ||||
-rw-r--r-- | source3/lib/tldap_util.c | 11 | ||||
-rw-r--r-- | source3/lib/util_str.c | 5 |
6 files changed, 50 insertions, 23 deletions
diff --git a/source3/lib/interface.c b/source3/lib/interface.c index a3bc5d24e91..342c92a61a2 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -358,6 +358,7 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap, while (key != NULL && *key != '\0') { char *next_key; char *val; + int error = 0; next_key = strchr_m(key, ','); if (next_key != NULL) { @@ -369,7 +370,10 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap, *val++ = 0; if (strequal_m(key, "speed")) { - *speed = (uint64_t)strtoull(val, NULL, 0); + *speed = (uint64_t)strtoull_err(val, NULL, 0, &error); + if (error != 0) { + DBG_DEBUG("Invalid speed value (%s)\n", val); + } } else if (strequal_m(key, "capability")) { if (strequal_m(val, "RSS")) { *cap |= FSCTL_NET_IFACE_RSS_CAPABLE; @@ -380,7 +384,10 @@ static void parse_extra_info(char *key, uint64_t *speed, uint32_t *cap, "'%s'\n", val); } } else if (strequal_m(key, "if_index")) { - *if_index = (uint32_t)strtoul(val, NULL, 0); + *if_index = (uint32_t)strtoul_err(val, NULL, 0, &error); + if (error != 0) { + DBG_DEBUG("Invalid key value (%s)\n", val); + } } else { DBG_DEBUG("Key unknown: '%s'\n", key); } @@ -515,9 +522,12 @@ static void interpret_interface(char *token) return; } } else { + int error = 0; char *endp = NULL; - unsigned long val = strtoul(p, &endp, 0); - if (p == endp || (endp && *endp != '\0')) { + unsigned long val; + + val = strtoul_err(p, &endp, 0, &error); + if (p == endp || (endp && *endp != '\0') || error != 0) { DEBUG(2,("interpret_interface: " "can't determine netmask value from %s\n", p)); diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c index aaafcc10307..d73a6ad6a7c 100644 --- a/source3/lib/messages_dgm.c +++ b/source3/lib/messages_dgm.c @@ -18,6 +18,7 @@ */ #include "replace.h" +#include "util/util.h" #include "system/network.h" #include "system/filesys.h" #include "system/dir.h" @@ -1458,6 +1459,7 @@ static int messaging_dgm_read_unique(int fd, uint64_t *punique) { char buf[25]; ssize_t rw_ret; + int error = 0; unsigned long long unique; char *endptr; @@ -1467,13 +1469,15 @@ static int messaging_dgm_read_unique(int fd, uint64_t *punique) } buf[rw_ret] = '\0'; - unique = strtoull(buf, &endptr, 10); + unique = strtoull_err(buf, &endptr, 10, &error); if ((unique == 0) && (errno == EINVAL)) { return EINVAL; } - if ((unique == ULLONG_MAX) && (errno == ERANGE)) { - return ERANGE; + + if (error != 0) { + return error; } + if (endptr[0] != '\n') { return EINVAL; } @@ -1615,6 +1619,7 @@ int messaging_dgm_forall(int (*fn)(pid_t pid, void *private_data), struct messaging_dgm_context *ctx = global_dgm_context; DIR *msgdir; struct dirent *dp; + int error = 0; if (ctx == NULL) { return ENOTCONN; @@ -1637,8 +1642,8 @@ int messaging_dgm_forall(int (*fn)(pid_t pid, void *private_data), unsigned long pid; int ret; - pid = strtoul(dp->d_name, NULL, 10); - if (pid == 0) { + pid = strtoul_err(dp->d_name, NULL, 10, &error); + if ((pid == 0) || (error != 0)) { /* * . and .. and other malformed entries */ diff --git a/source3/lib/namemap_cache.c b/source3/lib/namemap_cache.c index fa179517f9f..42656ede0b7 100644 --- a/source3/lib/namemap_cache.c +++ b/source3/lib/namemap_cache.c @@ -22,6 +22,7 @@ #include "source3/lib/gencache.h" #include "lib/util/debug.h" #include "lib/util/strv.h" +#include "lib/util/util.h" #include "lib/util/talloc_stack.h" #include "lib/util/charset/charset.h" #include "libcli/security/dom_sid.h" @@ -105,6 +106,7 @@ static void namemap_cache_find_sid_parser( const char *domain; const char *name; const char *typebuf; + int error = 0; char *endptr; unsigned long type; @@ -123,11 +125,8 @@ static void namemap_cache_find_sid_parser( return; } - type = strtoul(typebuf, &endptr, 10); - if (*endptr != '\0') { - return; - } - if ((type == ULONG_MAX) && (errno == ERANGE)) { + type = strtoul_err(typebuf, &endptr, 10, &error); + if ((*endptr != '\0') || (error != 0)) { return; } @@ -253,6 +252,7 @@ static void namemap_cache_find_name_parser( const char *sid_endptr; const char *typebuf; char *endptr; + int error = 0; struct dom_sid sid; unsigned long type; bool ok; @@ -276,11 +276,8 @@ static void namemap_cache_find_name_parser( return; } - type = strtoul(typebuf, &endptr, 10); - if (*endptr != '\0') { - return; - } - if ((type == ULONG_MAX) && (errno == ERANGE)) { + type = strtoul_err(typebuf, &endptr, 10, &error); + if ((*endptr != '\0') || (error != 0)) { return; } diff --git a/source3/lib/sysquotas.c b/source3/lib/sysquotas.c index 40b421c056f..43a451da596 100644 --- a/source3/lib/sysquotas.c +++ b/source3/lib/sysquotas.c @@ -253,6 +253,7 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t char *p2; char *syscmd = NULL; int _id = -1; + int error = 0; switch(qtype) { case SMB_USER_QUOTA_TYPE: @@ -285,7 +286,11 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t /* we need to deal with long long unsigned here, if supported */ - dp->qflags = strtoul(line, &p2, 10); + dp->qflags = strtoul_err(line, &p2, 10, &error); + if (error != 0) { + goto invalid_param; + } + p = p2; while (p && *p && isspace(*p)) { p++; diff --git a/source3/lib/tldap_util.c b/source3/lib/tldap_util.c index 54a9eb30bbe..3938fca901f 100644 --- a/source3/lib/tldap_util.c +++ b/source3/lib/tldap_util.c @@ -389,13 +389,22 @@ bool tldap_pull_uint64(struct tldap_message *msg, const char *attr, { char *str; uint64_t result; + int error = 0; str = tldap_talloc_single_attribute(msg, attr, talloc_tos()); if (str == NULL) { DEBUG(10, ("Could not find attribute %s\n", attr)); return false; } - result = strtoull(str, NULL, 10); + + result = strtoull_err(str, NULL, 10, &error); + if (error != 0) { + DBG_DEBUG("Attribute conversion failed (%s)\n", + strerror(error)); + TALLOC_FREE(str); + return false; + } + TALLOC_FREE(str); *presult = result; return true; diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8568af46c17..a0095d23978 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -851,14 +851,15 @@ uint64_t conv_str_size(const char * str) { uint64_t lval; char * end; + int error = 0; if (str == NULL || *str == '\0') { return 0; } - lval = strtoull(str, &end, 10 /* base */); + lval = strtoull_err(str, &end, 10, &error); - if (end == NULL || end == str) { + if (end == NULL || end == str || error != 0) { return 0; } |