Set errno for all error return cases.
Test: Updated unit tests pass.
Change-Id: I2c1f14ed44ce144a46cb2866c146fca89a05539e
diff --git a/include/android-base/parseint.h b/include/android-base/parseint.h
index 5bfa47e..9444fdd 100644
--- a/include/android-base/parseint.h
+++ b/include/android-base/parseint.h
@@ -38,6 +38,7 @@
}
if (s[0] == '-') {
+ errno = EINVAL;
return false;
}
@@ -45,14 +46,22 @@
errno = 0;
char* end;
unsigned long long int result = strtoull(s, &end, base);
- if (errno != 0 || end == s) return false;
+ if (errno != 0) return false;
+ if (end == s) {
+ errno = EINVAL;
+ return false;
+ }
if (*end != '\0') {
const char* suffixes = "bkmgtpe";
const char* suffix;
- if (!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) == nullptr) return false;
- if (__builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) return false;
+ if ((!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) == nullptr) ||
+ __builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) {
+ errno = EINVAL;
+ return false;
+ }
}
if (max < result) {
+ errno = ERANGE;
return false;
}
if (out != nullptr) {
@@ -95,10 +104,15 @@
errno = 0;
char* end;
long long int result = strtoll(s, &end, base);
- if (errno != 0 || s == end || *end != '\0') {
+ if (errno != 0) {
+ return false;
+ }
+ if (s == end || *end != '\0') {
+ errno = EINVAL;
return false;
}
if (result < min || max < result) {
+ errno = ERANGE;
return false;
}
if (out != nullptr) {