From: Swen Schillig Date: Wed, 6 Mar 2019 08:03:27 +0000 (+0100) Subject: lib: modify string conversion wrapper to handle invalid strings X-Git-Tag: tdb-1.4.1~457 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da80b6d2a14c58a7fc618bcb7750bbc4ea8cb95a;p=thirdparty%2Fsamba.git lib: modify string conversion wrapper to handle invalid strings The standard string conversion routines convert a "non-number string" to zero and do not flag an error. This is changed now by returning EINVAL if no conversion occured. 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 b8eed3ca28c..83af14cac1e 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -7,7 +7,8 @@ Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003. Copyright (C) James J Myers 2003 Copyright (C) Volker Lendecke 2010 - + Copyright (C) Swen Schillig 2019 + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or @@ -60,6 +61,7 @@ * - wrong base * - value overflow * - string with a leading "-" indicating a negative number + * - no conversion due to empty string or not representing a number */ unsigned long int strtoul_err(const char *nptr, char **endptr, int base, int *err) @@ -84,6 +86,13 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err) return val; } + /* got an invalid number-string resulting in no conversion */ + if (nptr == tmp_endptr) { + *err = EINVAL; + errno = saved_errno; + return val; + } + /* did we convert a negative "number" ? */ needle = strchr(nptr, '-'); if (needle != NULL && needle < tmp_endptr) { @@ -107,6 +116,7 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err) * - wrong base * - value overflow * - string with a leading "-" indicating a negative number + * - no conversion due to empty string or not representing a number */ unsigned long long int strtoull_err(const char *nptr, char **endptr, int base, int *err) @@ -131,6 +141,13 @@ strtoull_err(const char *nptr, char **endptr, int base, int *err) return val; } + /* got an invalid number-string resulting in no conversion */ + if (nptr == tmp_endptr) { + *err = EINVAL; + errno = saved_errno; + return val; + } + /* did we convert a negative "number" ? */ needle = strchr(nptr, '-'); if (needle != NULL && needle < tmp_endptr) {