diff options
| author | 2017-11-18 03:19:30 +0000 | |
|---|---|---|
| committer | 2017-11-18 03:19:30 +0000 | |
| commit | f84b4204c3109e7f9feabe2536247193301a698d (patch) | |
| tree | bc7a86b87837e85a61094f367c04b236272eef63 | |
| parent | 06b73ece42cb6775224763763a1322ac0c68a4fa (diff) | |
| parent | e7b0a330db58c21366390c39896a4d1ca306495e (diff) | |
Merge "AAPT2: whitelist alphanums instead of blacklisting hyphens in package names." into oc-mr1-dev
am: e7b0a330db
Change-Id: I7943dc35ceeca0c3fdb4df668a761889cd0add2d
| -rw-r--r-- | tools/aapt2/cmd/Util.cpp | 19 | ||||
| -rw-r--r-- | tools/aapt2/cmd/Util_test.cpp | 9 |
2 files changed, 22 insertions, 6 deletions
diff --git a/tools/aapt2/cmd/Util.cpp b/tools/aapt2/cmd/Util.cpp index 708bed80555b..90fc10add7e6 100644 --- a/tools/aapt2/cmd/Util.cpp +++ b/tools/aapt2/cmd/Util.cpp @@ -140,12 +140,27 @@ static xml::NamespaceDecl CreateAndroidNamespaceDecl() { return decl; } +// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by +// replacing nonconforming characters with underscores. +// +// See frameworks/base/core/java/android/content/pm/PackageParser.java which +// checks this at runtime. static std::string MakePackageSafeName(const std::string &name) { std::string result(name); + bool first = true; for (char &c : result) { - if (c == '-') { - c = '_'; + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { + first = false; + continue; } + if (!first) { + if (c >= '0' && c <= '9') { + continue; + } + } + + c = '_'; + first = false; } return result; } diff --git a/tools/aapt2/cmd/Util_test.cpp b/tools/aapt2/cmd/Util_test.cpp index 9c33135f228d..0c527f6a90dc 100644 --- a/tools/aapt2/cmd/Util_test.cpp +++ b/tools/aapt2/cmd/Util_test.cpp @@ -24,15 +24,16 @@ namespace aapt { TEST(UtilTest, SplitNamesAreSanitized) { AppInfo app_info{"com.pkg"}; - SplitConstraints split_constraints{{test::ParseConfigOrDie("en-rUS-land")}}; + SplitConstraints split_constraints{ + {test::ParseConfigOrDie("en-rUS-land"), test::ParseConfigOrDie("b+sr+Latn")}}; const auto doc = GenerateSplitManifest(app_info, split_constraints); const auto &root = doc->root; EXPECT_EQ(root->name, "manifest"); - // split names cannot contain hyphens - EXPECT_EQ(root->FindAttribute("", "split")->value, "config.en_rUS_land"); + // split names cannot contain hyphens or plus signs. + EXPECT_EQ(root->FindAttribute("", "split")->value, "config.b_sr_Latn_en_rUS_land"); // but we should use resource qualifiers verbatim in 'targetConfig'. - EXPECT_EQ(root->FindAttribute("", "targetConfig")->value, "en-rUS-land"); + EXPECT_EQ(root->FindAttribute("", "targetConfig")->value, "b+sr+Latn,en-rUS-land"); } } // namespace aapt |