summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMary Ruthven <mruthven@chromium.org>2021-11-09 14:22:42 -0600
committerCommit Bot <commit-bot@chromium.org>2021-11-18 04:48:04 +0000
commit16989bbb49e821adb4d3626f021210c9d7a16ba4 (patch)
treed3408d1aa4639a0168b8c412c95b417e103ee2dd /common
parent35083b5b9d2e7329da3546ea115a985e38ecd31b (diff)
downloadchrome-ec-16989bbb49e821adb4d3626f021210c9d7a16ba4.tar.gz
Revert "Align behavior of strtoi() and strtoul() to match Linux manpage description of strtol()."
This reverts commit ac8a13329b0b321daeb87f6afb79c163beb21372. BUG=b:200823466 TEST=make buildall -j Change-Id: I379e5fc1ebd15030715561732ea71eb6fda8406d Signed-off-by: Mary Ruthven <mruthven@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3273383 Reviewed-by: Vadim Sukhomlinov <sukhomlinov@chromium.org>
Diffstat (limited to 'common')
-rw-r--r--common/util.c59
1 files changed, 28 insertions, 31 deletions
diff --git a/common/util.c b/common/util.c
index 1087cc371a..2b667d16ab 100644
--- a/common/util.c
+++ b/common/util.c
@@ -309,18 +309,6 @@ __stdlib_compat int atoi(const char *nptr)
return neg ? -result : result;
}
-static int find_base(int base, int *c, const char **nptr) {
- if ((base == 0 || base == 16) && *c == '0'
- && (**nptr == 'x' || **nptr == 'X')) {
- *c = (*nptr)[1];
- (*nptr) += 2;
- base = 16;
- } else if (base == 0) {
- base = *c == '0' ? 8 : 10;
- }
- return base;
-}
-
/* Like strtol(), but for integers */
__stdlib_compat int strtoi(const char *nptr, char **endptr, int base)
@@ -329,18 +317,24 @@ __stdlib_compat int strtoi(const char *nptr, char **endptr, int base)
int neg = 0;
int c = '\0';
+ if (endptr)
+ *endptr = (char *)nptr;
+
while ((c = *nptr++) && isspace(c))
;
- if (c == '+') {
- c = *nptr++;
- } else if (c == '-') {
- neg = 1;
- c = *nptr++;
+ if (c == '0' && *nptr == 'x') {
+ base = 16;
+ c = nptr[1];
+ nptr += 2;
+ } else if (base == 0) {
+ base = 10;
+ if (c == '-') {
+ neg = 1;
+ c = *nptr++;
+ }
}
- base = find_base(base, &c, &nptr);
-
while (c) {
if (c >= '0' && c < '0' + MIN(base, 10))
result = result * base + (c - '0');
@@ -351,11 +345,11 @@ __stdlib_compat int strtoi(const char *nptr, char **endptr, int base)
else
break;
+ if (endptr)
+ *endptr = (char *)nptr;
c = *nptr++;
}
- if (endptr)
- *endptr = (char *)nptr - 1;
return neg ? -result : result;
}
@@ -364,18 +358,21 @@ __stdlib_compat uint64_t strtoul(const char *nptr, char **endptr, int base)
uint64_t result = 0;
int c = '\0';
+ if (endptr)
+ *endptr = (char *)nptr;
+
while ((c = *nptr++) && isspace(c))
;
- if (c == '+') {
- c = *nptr++;
- } else if (c == '-') {
- if (endptr)
- *endptr = (char *)nptr - 1;
- return result;
+ if (c == '0' && *nptr == 'x') {
+ base = 16;
+ c = nptr[1];
+ nptr += 2;
+ } else if (base == 0) {
+ base = 10;
+ if (c == '-')
+ return result;
}
-
- base = find_base(base, &c, &nptr);
while (c) {
if (c >= '0' && c < '0' + MIN(base, 10))
@@ -387,11 +384,11 @@ __stdlib_compat uint64_t strtoul(const char *nptr, char **endptr, int base)
else
break;
+ if (endptr)
+ *endptr = (char *)nptr;
c = *nptr++;
}
- if (endptr)
- *endptr = (char *)nptr - 1;
return result;
}