From f0d1339ed5e417915964bf4612123d67bc10f2f2 Mon Sep 17 00:00:00 2001 From: Swen Schillig Date: Thu, 11 Apr 2019 14:42:37 +0200 Subject: [PATCH] lib: Add check for full string consumption when converting string to int Some callers want to have the entire string being used for a string to integer conversion, otherwise flag an error. This is possible by providing the SAMBA_STR_FULL_STR_CONV flag. Signed-off-by: Swen Schillig Reviewed-by: Ralph Boehme Reviewed-by: Christof Schmitt --- lib/util/util.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/util/util.c b/lib/util/util.c index ebb418465c3..77d66b3c59e 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -61,6 +61,7 @@ * The following flags are supported * SMB_STR_STANDARD # raise error if negative or non-numeric * SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-" + * SMB_STR_FULL_STR_CONV # entire string must be converted * * The following errors are detected * - wrong base @@ -103,9 +104,19 @@ smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags) needle = strchr(nptr, '-'); if (needle != NULL && needle < tmp_endptr) { *err = EINVAL; + goto out; } } + if ((flags & SMB_STR_FULL_STR_CONV) != 0) { + /* did we convert the entire string ? */ + if (tmp_endptr[0] != '\0') { + *err = EINVAL; + goto out; + } + } + +out: errno = saved_errno; return val; } @@ -123,6 +134,7 @@ smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags) * The following flags are supported * SMB_STR_STANDARD # raise error if negative or non-numeric * SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-" + * SMB_STR_FULL_STR_CONV # entire string must be converted * * The following errors are detected * - wrong base @@ -165,9 +177,19 @@ smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags) needle = strchr(nptr, '-'); if (needle != NULL && needle < tmp_endptr) { *err = EINVAL; + goto out; } } + if ((flags & SMB_STR_FULL_STR_CONV) != 0) { + /* did we convert the entire string ? */ + if (tmp_endptr[0] != '\0') { + *err = EINVAL; + goto out; + } + } + +out: errno = saved_errno; return val; }