lib: Add check for full string consumption when converting string to int
authorSwen Schillig <swen@linux.ibm.com>
Thu, 11 Apr 2019 12:42:37 +0000 (14:42 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:18 +0000 (11:32 +0000)
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 <swen@linux.ibm.com>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Christof Schmitt <cs@samba.org>
lib/util/util.c

index ebb418465c30eaf19c49975ae43245cc5b9dc63a..77d66b3c59ec9e0b7f1a280c8d60e5adef9826bc 100644 (file)
@@ -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;
 }