lib: modify string conversion wrapper to handle invalid strings
authorSwen Schillig <swen@linux.ibm.com>
Wed, 6 Mar 2019 08:03:27 +0000 (09:03 +0100)
committerChristof Schmitt <cs@samba.org>
Thu, 11 Apr 2019 22:29:25 +0000 (22:29 +0000)
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 <swen@linux.ibm.com>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Christof Schmitt <cs@samba.org>
lib/util/util.c

index b8eed3ca28c526de78807cf2936d0618538d794b..83af14cac1e47cf98624fd7dfff4d5b0f286f5bc 100644 (file)
@@ -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) {