lib: Prepare for strtoul_err(), strtoull_err() API change
authorSwen Schillig <swen@linux.ibm.com>
Thu, 11 Apr 2019 09:22:02 +0000 (11:22 +0200)
committerRalph Boehme <slow@samba.org>
Sun, 30 Jun 2019 11:32:18 +0000 (11:32 +0000)
In order to still be bisectable when changing the API for the wrappers
strtoul_err() and strtoull_err() some preparations need to be performed.

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
lib/util/util.h

index 83af14cac1e47cf98624fd7dfff4d5b0f286f5bc..ebb418465c30eaf19c49975ae43245cc5b9dc63a 100644 (file)
  * @param endptr       [optional] reference to remainder of the string
  * @param base         base of the numbering scheme
  * @param err          error occured during conversion
+ * @flags              controlling conversion feature
  * @result             result of the conversion as provided by strtoul
  *
+ * The following flags are supported
+ *     SMB_STR_STANDARD # raise error if negative or non-numeric
+ *     SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
+ *
  * The following errors are detected
  * - wrong base
  * - value overflow
@@ -64,7 +69,7 @@
  * - 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)
+smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags)
 {
        unsigned long int val;
        int saved_errno = errno;
@@ -93,10 +98,12 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err)
                return val;
        }
 
-       /* did we convert a negative "number" ? */
-       needle = strchr(nptr, '-');
-       if (needle != NULL && needle < tmp_endptr) {
-               *err = EINVAL;
+       if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
+               /* did we convert a negative "number" ? */
+               needle = strchr(nptr, '-');
+               if (needle != NULL && needle < tmp_endptr) {
+                       *err = EINVAL;
+               }
        }
 
        errno = saved_errno;
@@ -110,8 +117,13 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err)
  * @param endptr       [optional] reference to remainder of the string
  * @param base         base of the numbering scheme
  * @param err          error occured during conversion
+ * @flags              controlling conversion feature
  * @result             result of the conversion as provided by strtoull
  *
+ * The following flags are supported
+ *     SMB_STR_STANDARD # raise error if negative or non-numeric
+ *     SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
+ *
  * The following errors are detected
  * - wrong base
  * - value overflow
@@ -119,7 +131,7 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err)
  * - 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)
+smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags)
 {
        unsigned long long int val;
        int saved_errno = errno;
@@ -148,10 +160,12 @@ strtoull_err(const char *nptr, char **endptr, int base, int *err)
                return val;
        }
 
-       /* did we convert a negative "number" ? */
-       needle = strchr(nptr, '-');
-       if (needle != NULL && needle < tmp_endptr) {
-               *err = EINVAL;
+       if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
+               /* did we convert a negative "number" ? */
+               needle = strchr(nptr, '-');
+               if (needle != NULL && needle < tmp_endptr) {
+                       *err = EINVAL;
+               }
        }
 
        errno = saved_errno;
index 2ed036358d042204f113c8b3f3a4d97999ec5ab5..d65d8c9ff24adb9ca5689019b7ba3e8ce197c3e9 100644 (file)
@@ -3,6 +3,7 @@
    Utility functions for Samba
    Copyright (C) Andrew Tridgell 1992-1999
    Copyright (C) Jelmer Vernooij 2005
+   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
                                SMB_STR_ALLOW_NEGATIVE)
 
 unsigned long int
-strtoul_err(const char *nptr, char **endptr, int base, int *err);
+smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags);
 
 unsigned long long int
-strtoull_err(const char *nptr, char **endptr, int base, int *err);
+smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags);
 
+#define strtoul_err(nptr, endptr, base, err) \
+       smb_strtoul(nptr, endptr, base, err, SMB_STR_STANDARD)
+#define strtoull_err(nptr, endptr, base, err) \
+       smb_strtoull(nptr, endptr, base, err, SMB_STR_STANDARD)
 
 /**
  * Write dump of binary data to a callback