lib: modify string conversion wrapper to handle signed input
authorSwen Schillig <swen@linux.ibm.com>
Tue, 5 Feb 2019 07:39:14 +0000 (08:39 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 1 Mar 2019 00:32:12 +0000 (00:32 +0000)
The standard string conversion routines convert a "signed string"
into the positive representation of the resulting value.
This is not wanted and therefore now detected and flag'ed as an error.

Signed-off-by: Swen Schillig <swen@linux.ibm.com>
Reviewed-by: Ralph Böhme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/util.c

index 855642c06bcc3a6f3ee5fcab07f48e94184b5e9d..19824b550c0a6511d7c8b3b30be435f408b31f12 100644 (file)
  * @param err          error occured during conversion
  * @result             result of the conversion as provided by strtoul
  *
- * Currently the only errors detected are wrong base and a value overflow.
+ * The following errors are detected
+ * - wrong base
+ * - value overflow
+ * - string with a leading "-" indicating a negative number
  */
 unsigned long int
 strtoul_err(const char *nptr, char **endptr, int base, int *err)
 {
        unsigned long int val;
        int saved_errno = errno;
+       char *needle = NULL;
+       char *tmp_endptr = NULL;
 
        errno = 0;
-       val = strtoul(nptr, endptr, base);
-       *err = errno;
+       *err = 0;
+
+       val = strtoul(nptr, &tmp_endptr, base);
+
+       if (endptr != NULL) {
+               *endptr = tmp_endptr;
+       }
+
+       if (errno != 0) {
+               *err = errno;
+               errno = saved_errno;
+               return val;
+       }
+
+       /* did we convert a negative "number" ? */
+       needle = strchr(nptr, '-');
+       if (needle != NULL && needle < tmp_endptr) {
+               *err = EINVAL;
+       }
 
        errno = saved_errno;
        return val;
@@ -81,17 +103,39 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err)
  * @param err          error occured during conversion
  * @result             result of the conversion as provided by strtoull
  *
- * Currently the only errors detected are wrong base and a value overflow.
+ * The following errors are detected
+ * - wrong base
+ * - value overflow
+ * - string with a leading "-" indicating a negative number
  */
 unsigned long long int
 strtoull_err(const char *nptr, char **endptr, int base, int *err)
 {
        unsigned long long int val;
        int saved_errno = errno;
+       char *needle = NULL;
+       char *tmp_endptr = NULL;
 
        errno = 0;
-       val = strtoull(nptr, endptr, base);
-       *err = errno;
+       *err = 0;
+
+       val = strtoull(nptr, &tmp_endptr, base);
+
+       if (endptr != NULL) {
+               *endptr = tmp_endptr;
+       }
+
+       if (errno != 0) {
+               *err = errno;
+               errno = saved_errno;
+               return val;
+       }
+
+       /* did we convert a negative "number" ? */
+       needle = strchr(nptr, '-');
+       if (needle != NULL && needle < tmp_endptr) {
+               *err = EINVAL;
+       }
 
        errno = saved_errno;
        return val;