Fix ubsan failure in android::base::Split.

ubsan doesn't like us incrementing std::string::npos, even if we won't use
the result.

Bug: http://b/28729303
Test: ran tests
Change-Id: I8227eca57dc6f3e49626c7025514caa04ef18f0a
This commit is contained in:
Elliott Hughes 2017-02-07 15:30:24 -08:00
parent 01b25ab149
commit bf0dd7cb03
3 changed files with 13 additions and 2 deletions

View file

@ -37,6 +37,9 @@ cc_library {
cppflags: libbase_cppflags,
export_include_dirs: ["include"],
shared_libs: ["liblog"],
sanitize: {
misc_undefined: ["integer"],
},
target: {
android: {
srcs: [
@ -86,6 +89,9 @@ cc_test {
"strings_test.cpp",
"test_main.cpp",
],
sanitize: {
misc_undefined: ["integer"],
},
target: {
android: {
srcs: ["properties_test.cpp"],

View file

@ -36,11 +36,12 @@ std::vector<std::string> Split(const std::string& s,
size_t base = 0;
size_t found;
do {
while (true) {
found = s.find_first_of(delimiters, base);
result.push_back(s.substr(base, found - base));
if (found == s.npos) break;
base = found + 1;
} while (found != s.npos);
}
return result;
}

View file

@ -251,3 +251,7 @@ TEST(strings, EqualsIgnoreCase) {
ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar"));
ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool"));
}
TEST(strings, ubsan_28729303) {
android::base::Split("/dev/null", ":");
}