ParseInt/ParseUint: allow validation only.

Removes segfault if T* out != nullptr and just
returns validation result.

Bug: 110758329
Test: libbase_test
Change-Id: I0f304533a7076bba977fbd1a715b9cc0d9e58e75
This commit is contained in:
Steven Moreland 2018-07-20 11:02:47 -07:00
parent 74be24d696
commit a96e43d3d6
2 changed files with 14 additions and 2 deletions

View File

@ -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;
}

View File

@ -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) {