summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-11-02 09:01:53 -0800
committerElliott Hughes <enh@google.com>2015-11-02 09:01:53 -0800
commit3ab05869d84357dd54ee27f971bde6514475bcb6 (patch)
treef98366cd6eedf1e313148eaafe62308f640d1edf /base
parent1e2382a0277eb36fb57a3a54202945556dfd234b (diff)
Fix ParseInt/ParseUint to handle explicit "0x" hex.
Also improve fastboot error reporting around max-download-size. Change-Id: Ic3aec9460de01e5264a2803a0a6be3706d73026b
Diffstat (limited to 'base')
-rw-r--r--base/include/base/parseint.h6
-rw-r--r--base/parseint_test.cpp10
2 files changed, 14 insertions, 2 deletions
diff --git a/base/include/base/parseint.h b/base/include/base/parseint.h
index 9ecbfbce9..0543795e1 100644
--- a/base/include/base/parseint.h
+++ b/base/include/base/parseint.h
@@ -31,9 +31,10 @@ namespace base {
template <typename T>
bool ParseUint(const char* s, T* out,
T max = std::numeric_limits<T>::max()) {
+ int base = (s[0] == '0' && s[1] == 'x') ? 16 : 10;
errno = 0;
char* end;
- unsigned long long int result = strtoull(s, &end, 10);
+ unsigned long long int result = strtoull(s, &end, base);
if (errno != 0 || s == end || *end != '\0') {
return false;
}
@@ -52,9 +53,10 @@ template <typename T>
bool ParseInt(const char* s, T* out,
T min = std::numeric_limits<T>::min(),
T max = std::numeric_limits<T>::max()) {
+ int base = (s[0] == '0' && s[1] == 'x') ? 16 : 10;
errno = 0;
char* end;
- long long int result = strtoll(s, &end, 10);
+ long long int result = strtoll(s, &end, base);
if (errno != 0 || s == end || *end != '\0') {
return false;
}
diff --git a/base/parseint_test.cpp b/base/parseint_test.cpp
index e19c6e3cd..8a11d293f 100644
--- a/base/parseint_test.cpp
+++ b/base/parseint_test.cpp
@@ -66,3 +66,13 @@ TEST(parseint, no_implicit_octal) {
ASSERT_TRUE(android::base::ParseUint("0123", &u));
ASSERT_EQ(123u, u);
}
+
+TEST(parseint, explicit_hex) {
+ int i;
+ ASSERT_TRUE(android::base::ParseInt("0x123", &i));
+ ASSERT_EQ(0x123, i);
+
+ unsigned int u;
+ ASSERT_TRUE(android::base::ParseUint("0x123", &u));
+ ASSERT_EQ(0x123u, u);
+}