AAPT2: Fix issues with parsing integers + floats
If whitespace was present at the end of an integer or float, parsing
would fail.
Bug: 62902869
Test: make aapt2_tests
Change-Id: I6c54f25ad73913d8ea90969fca9de24f726deb96
diff --git a/tools/aapt2/ResourceUtils.cpp b/tools/aapt2/ResourceUtils.cpp
index deeef6e..6e6a2ba 100644
--- a/tools/aapt2/ResourceUtils.cpp
+++ b/tools/aapt2/ResourceUtils.cpp
@@ -512,7 +512,7 @@
}
std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
- std::u16string str16 = util::Utf8ToUtf16(str);
+ std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
android::Res_value value;
if (!android::ResTable::stringToInt(str16.data(), str16.size(), &value)) {
return {};
@@ -521,7 +521,7 @@
}
std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
- std::u16string str16 = util::Utf8ToUtf16(str);
+ std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
android::Res_value value;
if (!android::ResTable::stringToFloat(str16.data(), str16.size(), &value)) {
return {};
diff --git a/tools/aapt2/ResourceUtils_test.cpp b/tools/aapt2/ResourceUtils_test.cpp
index 3a5e685e..e637c3e 100644
--- a/tools/aapt2/ResourceUtils_test.cpp
+++ b/tools/aapt2/ResourceUtils_test.cpp
@@ -36,6 +36,8 @@
EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(Maybe<bool>(false)));
EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(Maybe<bool>(false)));
EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(Maybe<bool>(false)));
+
+ EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(Maybe<bool>(false)));
}
TEST(ResourceUtilsTest, ParseResourceName) {
@@ -199,4 +201,16 @@
ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
}
+TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
+ EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n ", ResTable_map::TYPE_INTEGER),
+ Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
+ EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n ", ResTable_map::TYPE_BOOLEAN),
+ Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
+
+ const float expected_float = 12.0f;
+ const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
+ EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n ", ResTable_map::TYPE_FLOAT),
+ Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
+}
+
} // namespace aapt
diff --git a/tools/aapt2/readme.md b/tools/aapt2/readme.md
index 62945b1..d6483ec 100644
--- a/tools/aapt2/readme.md
+++ b/tools/aapt2/readme.md
@@ -3,6 +3,7 @@
## Version 2.18
### `aapt2 ...`
- Fixed issue where enum values were interpreted as integers and range checked. (bug 62358540)
+- Fixed issue where ints and floats with trailing whitespace would not be parsed. (bug 62902869)
## Version 2.17
### `aapt2 ...`