Merge "AAPT2: Fixed id parsing error" into pi-dev

am: f4c089dd90

Change-Id: I81147ee8629edc98ba19565083c09d7dce8e6bf7
This commit is contained in:
Ryan Mitchell
2018-05-02 09:42:11 -07:00
committed by android-build-merger
2 changed files with 21 additions and 8 deletions

View File

@@ -598,10 +598,13 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
// If no inner element exists, represent a unique identifier
out_resource->value = util::make_unique<Id>();
} else {
// If an inner element exists, the inner element must be a reference to
// another resource id
Reference* ref = ValueCast<Reference>(out_resource->value.get());
if (!ref || ref->name.value().type != ResourceType::kId) {
if (ref && !ref->name && !ref->id) {
// A null reference also means there is no inner element when ids are in the form:
// <id name="name"/>
out_resource->value = util::make_unique<Id>();
} else if (!ref || ref->name.value().type != ResourceType::kId) {
// If an inner element exists, the inner element must be a reference to another resource id
diag_->Error(DiagMessage(out_resource->source)
<< "<" << parser->element_name()
<< "> inner element must either be a resource reference or empty");

View File

@@ -944,20 +944,30 @@ TEST_F(ResourceParserTest, ParseIdItem) {
ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar"), NotNull());
ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz"), NotNull());
input = R"(
<id name="foo2">@id/bar</id>
<id name="bar2"/>
<id name="baz2"></id>)";
ASSERT_TRUE(TestParse(input));
ASSERT_THAT(test::GetValue<Reference>(&table_, "id/foo2"), NotNull());
ASSERT_THAT(test::GetValue<Id>(&table_, "id/bar2"), NotNull());
ASSERT_THAT(test::GetValue<Id>(&table_, "id/baz2"), NotNull());
// Reject attribute references
input = R"(<item name="foo2" type="id">?attr/bar"</item>)";
input = R"(<item name="foo3" type="id">?attr/bar"</item>)";
ASSERT_FALSE(TestParse(input));
// Reject non-references
input = R"(<item name="foo3" type="id">0x7f010001</item>)";
input = R"(<item name="foo4" type="id">0x7f010001</item>)";
ASSERT_FALSE(TestParse(input));
input = R"(<item name="foo4" type="id">@drawable/my_image</item>)";
input = R"(<item name="foo5" type="id">@drawable/my_image</item>)";
ASSERT_FALSE(TestParse(input));
input = R"(<item name="foo5" type="id"><string name="biz"></string></item>)";
input = R"(<item name="foo6" type="id"><string name="biz"></string></item>)";
ASSERT_FALSE(TestParse(input));
// Ids that reference other resource ids cannot be public
input = R"(<public name="foo6" type="id">@id/bar6</item>)";
input = R"(<public name="foo7" type="id">@id/bar7</item>)";
ASSERT_FALSE(TestParse(input));
}