summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorSteven Moreland2018-07-20 13:02:47 -0500
committerSteven Moreland2018-07-20 13:02:47 -0500
commita96e43d3d6736ebe4985ac3a818135768a9672f8 (patch)
tree16f21aea3a73c4cdb0be0ae8331cec5fc5e3a231 /base
parent74be24d696f2d67293de6b9bb6e715c1728abed7 (diff)
downloadplatform-system-core-a96e43d3d6736ebe4985ac3a818135768a9672f8.tar.gz
platform-system-core-a96e43d3d6736ebe4985ac3a818135768a9672f8.tar.xz
platform-system-core-a96e43d3d6736ebe4985ac3a818135768a9672f8.zip
ParseInt/ParseUint: allow validation only.
Removes segfault if T* out != nullptr and just returns validation result. Bug: 110758329 Test: libbase_test Change-Id: I0f304533a7076bba977fbd1a715b9cc0d9e58e75
Diffstat (limited to 'base')
-rw-r--r--base/include/android-base/parseint.h8
-rw-r--r--base/parseint_test.cpp8
2 files changed, 14 insertions, 2 deletions
diff --git a/base/include/android-base/parseint.h b/base/include/android-base/parseint.h
index fc68d56b4..bb54c99b8 100644
--- a/base/include/android-base/parseint.h
+++ b/base/include/android-base/parseint.h
@@ -47,7 +47,9 @@ bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(),
47 if (max < result) { 47 if (max < result) {
48 return false; 48 return false;
49 } 49 }
50 *out = static_cast<T>(result); 50 if (out != nullptr) {
51 *out = static_cast<T>(result);
52 }
51 return true; 53 return true;
52} 54}
53 55
@@ -87,7 +89,9 @@ bool ParseInt(const char* s, T* out,
87 if (result < min || max < result) { 89 if (result < min || max < result) {
88 return false; 90 return false;
89 } 91 }
90 *out = static_cast<T>(result); 92 if (out != nullptr) {
93 *out = static_cast<T>(result);
94 }
91 return true; 95 return true;
92} 96}
93 97
diff --git a/base/parseint_test.cpp b/base/parseint_test.cpp
index fb1c33993..8f9ed776b 100644
--- a/base/parseint_test.cpp
+++ b/base/parseint_test.cpp
@@ -36,6 +36,10 @@ TEST(parseint, signed_smoke) {
36 ASSERT_EQ(12, i); 36 ASSERT_EQ(12, i);
37 ASSERT_FALSE(android::base::ParseInt("-12", &i, 0, 15)); 37 ASSERT_FALSE(android::base::ParseInt("-12", &i, 0, 15));
38 ASSERT_FALSE(android::base::ParseInt("16", &i, 0, 15)); 38 ASSERT_FALSE(android::base::ParseInt("16", &i, 0, 15));
39
40 ASSERT_FALSE(android::base::ParseInt<int>("x", nullptr));
41 ASSERT_FALSE(android::base::ParseInt<int>("123x", nullptr));
42 ASSERT_TRUE(android::base::ParseInt<int>("1234", nullptr));
39} 43}
40 44
41TEST(parseint, unsigned_smoke) { 45TEST(parseint, unsigned_smoke) {
@@ -55,6 +59,10 @@ TEST(parseint, unsigned_smoke) {
55 ASSERT_EQ(12u, i); 59 ASSERT_EQ(12u, i);
56 ASSERT_FALSE(android::base::ParseUint("-12", &i, 15u)); 60 ASSERT_FALSE(android::base::ParseUint("-12", &i, 15u));
57 ASSERT_FALSE(android::base::ParseUint("16", &i, 15u)); 61 ASSERT_FALSE(android::base::ParseUint("16", &i, 15u));
62
63 ASSERT_FALSE(android::base::ParseUint<unsigned short>("x", nullptr));
64 ASSERT_FALSE(android::base::ParseUint<unsigned short>("123x", nullptr));
65 ASSERT_TRUE(android::base::ParseUint<unsigned short>("1234", nullptr));
58} 66}
59 67
60TEST(parseint, no_implicit_octal) { 68TEST(parseint, no_implicit_octal) {