diff options
author | Adam Lesinski <adamlesinski@google.com> | 2017-06-27 12:27:43 -0700 |
---|---|---|
committer | Adam Lesinski <adamlesinski@google.com> | 2017-06-27 12:45:00 -0700 |
commit | 8a3bffea4926aa691ea1fbc0e7b67ed954e83d2c (patch) | |
tree | fe30071c024754dba8b062afed96a21c68558c28 | |
parent | 79385ec3b0b1a8753aefd2973273560f0461ff80 (diff) |
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
-rw-r--r-- | tools/aapt2/ResourceUtils.cpp | 4 | ||||
-rw-r--r-- | tools/aapt2/ResourceUtils_test.cpp | 14 | ||||
-rw-r--r-- | tools/aapt2/readme.md | 1 |
3 files changed, 17 insertions, 2 deletions
diff --git a/tools/aapt2/ResourceUtils.cpp b/tools/aapt2/ResourceUtils.cpp index deeef6ebbcb7..6e6a2ba6fc50 100644 --- a/tools/aapt2/ResourceUtils.cpp +++ b/tools/aapt2/ResourceUtils.cpp @@ -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 {}; diff --git a/tools/aapt2/ResourceUtils_test.cpp b/tools/aapt2/ResourceUtils_test.cpp index 3a5e685e6b94..e637c3ee2f3c 100644 --- a/tools/aapt2/ResourceUtils_test.cpp +++ b/tools/aapt2/ResourceUtils_test.cpp @@ -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 diff --git a/tools/aapt2/readme.md b/tools/aapt2/readme.md index 62945b1aea8d..d6483ec1c0e8 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 ...` |