assert(s);
assert(ret);
+ s += strspn(s, WHITESPACE);
+ if (s[0] == '-')
+ return -ERANGE;
+
errno = 0;
l = strtol(s, &x, 8);
if (errno != 0)
return -errno;
-
if (!x || x == s || *x)
return -EINVAL;
if (l < 0 || l > 07777)
unsigned i;
p += strspn(p, WHITESPACE);
- if (*p == '-')
- return -ERANGE;
errno = 0;
l = strtoull(p, &e, 10);
- if (errno > 0)
+ if (errno != 0)
return -errno;
if (e == p)
return -EINVAL;
+ if (*p == '-')
+ return -ERANGE;
if (*e == '.') {
e++;
char *e2;
l2 = strtoull(e, &e2, 10);
- if (errno > 0)
+ if (errno != 0)
return -errno;
/* Ignore failure. E.g. 10.M is valid */
assert(s);
assert(ret_u);
- errno = 0;
- l = strtoul(s, &x, 0);
+ /* strtoul() is happy to parse negative values, and silently
+ * converts them to unsigned values without generating an
+ * error. We want a clean error, hence let's look for the "-"
+ * prefix on our own, and generate an error. But let's do so
+ * only after strtoul() validated that the string is clean
+ * otherwise, so that we return EINVAL preferably over
+ * ERANGE. */
- if (!x || x == s || *x || errno)
- return errno > 0 ? -errno : -EINVAL;
+ s += strspn(s, WHITESPACE);
+ errno = 0;
+ l = strtoul(s, &x, 0);
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (s[0] == '-')
+ return -ERANGE;
if ((unsigned long) (unsigned) l != l)
return -ERANGE;
errno = 0;
l = strtol(s, &x, 0);
-
- if (!x || x == s || *x || errno)
- return errno > 0 ? -errno : -EINVAL;
-
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
if ((long) (int) l != l)
return -ERANGE;
assert(s);
assert(ret_llu);
+ s += strspn(s, WHITESPACE);
+
errno = 0;
l = strtoull(s, &x, 0);
-
- if (!x || x == s || *x || errno)
- return errno ? -errno : -EINVAL;
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (*s == '-')
+ return -ERANGE;
*ret_llu = l;
return 0;
errno = 0;
l = strtoll(s, &x, 0);
-
- if (!x || x == s || *x || errno)
- return errno ? -errno : -EINVAL;
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
*ret_lli = l;
return 0;
assert(s);
assert(ret);
+ s += strspn(s, WHITESPACE);
+
errno = 0;
l = strtoul(s, &x, 0);
-
- if (!x || x == s || *x || errno)
- return errno > 0 ? -errno : -EINVAL;
-
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (s[0] == '-')
+ return -ERANGE;
if ((unsigned long) (uint8_t) l != l)
return -ERANGE;
assert(s);
assert(ret);
+ s += strspn(s, WHITESPACE);
+
errno = 0;
l = strtoul(s, &x, 0);
-
- if (!x || x == s || *x || errno)
- return errno > 0 ? -errno : -EINVAL;
-
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (s[0] == '-')
+ return -ERANGE;
if ((unsigned long) (uint16_t) l != l)
return -ERANGE;
errno = 0;
l = strtol(s, &x, 0);
-
- if (!x || x == s || *x || errno)
- return errno > 0 ? -errno : -EINVAL;
-
+ if (errno != 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
if ((long) (int16_t) l != l)
return -ERANGE;
errno = 0;
d = strtod_l(s, &x, loc);
-
- if (!x || x == s || *x || errno) {
+ if (errno != 0) {
freelocale(loc);
- return errno ? -errno : -EINVAL;
+ return -errno;
+ }
+ if (!x || x == s || *x) {
+ freelocale(loc);
+ return -EINVAL;
}
freelocale(loc);