summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2018-07-20 21:53:27 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2018-07-20 21:53:27 +0000
commit3ba85c4d179cffe12d801271f73c65272204519a (patch)
treecc544b3c5ed9e9cf303f1119500fa0735f43c1e0
parent767506fc5a5023e815cdc36e56688adaac2fe0d5 (diff)
parenta96e43d3d6736ebe4985ac3a818135768a9672f8 (diff)
downloadsystem_core-3ba85c4d179cffe12d801271f73c65272204519a.tar.gz
system_core-3ba85c4d179cffe12d801271f73c65272204519a.tar.bz2
system_core-3ba85c4d179cffe12d801271f73c65272204519a.zip
Merge "ParseInt/ParseUint: allow validation only."
-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(),
if (max < result) {
return false;
}
- *out = static_cast<T>(result);
+ if (out != nullptr) {
+ *out = static_cast<T>(result);
+ }
return true;
}
@@ -87,7 +89,9 @@ bool ParseInt(const char* s, T* out,
if (result < min || max < result) {
return false;
}
- *out = static_cast<T>(result);
+ if (out != nullptr) {
+ *out = static_cast<T>(result);
+ }
return true;
}
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) {
ASSERT_EQ(12, i);
ASSERT_FALSE(android::base::ParseInt("-12", &i, 0, 15));
ASSERT_FALSE(android::base::ParseInt("16", &i, 0, 15));
+
+ ASSERT_FALSE(android::base::ParseInt<int>("x", nullptr));
+ ASSERT_FALSE(android::base::ParseInt<int>("123x", nullptr));
+ ASSERT_TRUE(android::base::ParseInt<int>("1234", nullptr));
}
TEST(parseint, unsigned_smoke) {
@@ -55,6 +59,10 @@ TEST(parseint, unsigned_smoke) {
ASSERT_EQ(12u, i);
ASSERT_FALSE(android::base::ParseUint("-12", &i, 15u));
ASSERT_FALSE(android::base::ParseUint("16", &i, 15u));
+
+ ASSERT_FALSE(android::base::ParseUint<unsigned short>("x", nullptr));
+ ASSERT_FALSE(android::base::ParseUint<unsigned short>("123x", nullptr));
+ ASSERT_TRUE(android::base::ParseUint<unsigned short>("1234", nullptr));
}
TEST(parseint, no_implicit_octal) {