]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Add capability to enable standard glibc behaviour for string to int conversion
authorSwen Schillig <swen@linux.ibm.com>
Thu, 11 Apr 2019 12:46:49 +0000 (14:46 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:18 +0000 (11:32 +0000)
Adding two addtl. flags SAMBA_STR_ALLOW_NO_CONVERSION and SAMBA_STR_GLIBC_STANDARD
for the wrappers strtoul_err() and strtoull_err() providing the possibility
to get standard glibc behaviour for string to integer conversion.

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 77d66b3c59ec9e0b7f1a280c8d60e5adef9826bc..3bdeded5c1b6e29095f04ddfacea27235ac4fae8 100644 (file)
@@ -62,6 +62,8 @@
  *     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
+ *     SMB_STR_ALLOW_NO_CONVERSION # allow empty strings or non-numeric
+ *     SMB_STR_GLIBC_STANDARD # act exactly as the standard glibc strtoul
  *
  * The following errors are detected
  * - wrong base
@@ -92,11 +94,12 @@ smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags)
                return val;
        }
 
-       /* got an invalid number-string resulting in no conversion */
-       if (nptr == tmp_endptr) {
-               *err = EINVAL;
-               errno = saved_errno;
-               return val;
+       if ((flags & SMB_STR_ALLOW_NO_CONVERSION) == 0) {
+               /* got an invalid number-string resulting in no conversion */
+               if (nptr == tmp_endptr) {
+                       *err = EINVAL;
+                       goto out;
+               }
        }
 
        if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
@@ -135,6 +138,8 @@ out:
  *     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
+ *     SMB_STR_ALLOW_NO_CONVERSION # allow empty strings or non-numeric
+ *     SMB_STR_GLIBC_STANDARD # act exactly as the standard glibc strtoul
  *
  * The following errors are detected
  * - wrong base
@@ -165,11 +170,12 @@ smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags)
                return val;
        }
 
-       /* got an invalid number-string resulting in no conversion */
-       if (nptr == tmp_endptr) {
-               *err = EINVAL;
-               errno = saved_errno;
-               return val;
+       if ((flags & SMB_STR_ALLOW_NO_CONVERSION) == 0) {
+               /* got an invalid number-string resulting in no conversion */
+               if (nptr == tmp_endptr) {
+                       *err = EINVAL;
+                       goto out;
+               }
        }
 
        if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {