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
This commit is contained in:
Adam Lesinski
2017-06-27 12:27:43 -07:00
parent 79385ec3b0
commit 8a3bffea49
3 changed files with 17 additions and 2 deletions

View File

@@ -512,7 +512,7 @@ std::unique_ptr<BinaryPrimitive> MakeBool(bool val) {
}
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> TryParseInt(const StringPiece& str) {
}
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 {};

View File

@@ -36,6 +36,8 @@ TEST(ResourceUtilsTest, ParseBool) {
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 @@ TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
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

View File

@@ -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 ...`