From: Swen Schillig Date: Thu, 11 Apr 2019 12:42:37 +0000 (+0200) Subject: lib: Add check for full string consumption when converting string to int X-Git-Tag: ldb-2.0.5~140 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f0d1339ed5e417915964bf4612123d67bc10f2f2;p=thirdparty%2Fsamba.git 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 --- 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; }