adb: make ParseUint reject garbage at the end by default.

Test: adb_test on host
Change-Id: Ie63d89bd08f0b296a3d54ff66aae85fe52d8cd0f
This commit is contained in:
Josh Gao 2019-02-20 19:46:26 -08:00
parent 3be9bff517
commit aeca208385
2 changed files with 12 additions and 1 deletions

View File

@ -110,7 +110,7 @@ inline std::string_view StripTrailingNulls(std::string_view str) {
// Base-10 stroll on a string_view.
template <typename T>
inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining) {
inline bool ParseUint(T* result, std::string_view str, std::string_view* remaining = nullptr) {
if (str.empty() || !isdigit(str[0])) {
return false;
}
@ -135,6 +135,9 @@ inline bool ParseUint(T* result, std::string_view str, std::string_view* remaini
*result = value;
if (remaining) {
*remaining = str.substr(it - str.begin());
} else {
return it == str.end();
}
return true;
}

View File

@ -206,6 +206,14 @@ void TestParseUint(std::string_view string, bool expected_success, uint32_t expe
EXPECT_EQ(remaining, "foo");
}
}
// With trailing text, without remaining.
{
std::string text = std::string(string) + "foo";
uint32_t value;
bool success = ParseUint(&value, text, nullptr);
EXPECT_EQ(success, false);
}
}
TEST(adb_utils, ParseUint) {