summaryrefslogtreecommitdiff
path: root/cmds/installd/utils.cpp
diff options
context:
space:
mode:
author Calin Juravle <calin@google.com> 2017-03-10 22:18:26 +0000
committer android-build-merger <android-build-merger@google.com> 2017-03-10 22:18:26 +0000
commit0c90e1c416ca5e6b24e00955ccde23705790e226 (patch)
treea2df0d1bcabf17c8ddd05317bf3648f963e79342 /cmds/installd/utils.cpp
parenta15409a340ac4a18e786e1924c3f0d0fb5abe187 (diff)
parentdc9fcf4218567ca800e0c03606484315fc8eeb79 (diff)
Merge changes I20d546fd,I15363803 am: 632dbe2c77
am: dc9fcf4218 Change-Id: I6f091a21f326682deaec7527b40a235fecc4c267
Diffstat (limited to 'cmds/installd/utils.cpp')
-rw-r--r--cmds/installd/utils.cpp66
1 files changed, 31 insertions, 35 deletions
diff --git a/cmds/installd/utils.cpp b/cmds/installd/utils.cpp
index 7c40ef75f3..c59f4816e7 100644
--- a/cmds/installd/utils.cpp
+++ b/cmds/installd/utils.cpp
@@ -346,46 +346,42 @@ int create_move_path(char path[PKG_PATH_MAX],
* 0 on success.
*/
bool is_valid_package_name(const std::string& packageName) {
- const char* pkgname = packageName.c_str();
- const char *x = pkgname;
- int alpha = -1;
-
- if (strlen(pkgname) > PKG_NAME_MAX) {
- return false;
- }
-
- while (*x) {
- if (isalnum(*x) || (*x == '_')) {
- /* alphanumeric or underscore are fine */
- } else if (*x == '.') {
- if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
- /* periods must not be first, last, or doubled */
- ALOGE("invalid package name '%s'\n", pkgname);
- return false;
+ // This logic is borrowed from PackageParser.java
+ bool hasSep = false;
+ bool front = true;
+
+ auto it = packageName.begin();
+ for (; it != packageName.end() && *it != '-'; it++) {
+ char c = *it;
+ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
+ front = false;
+ continue;
+ }
+ if (!front) {
+ if ((c >= '0' && c <= '9') || c == '_') {
+ continue;
}
- } else if (*x == '-') {
- /* Suffix -X is fine to let versioning of packages.
- But whatever follows should be alphanumeric.*/
- alpha = 1;
- } else {
- /* anything not A-Z, a-z, 0-9, _, or . is invalid */
- ALOGE("invalid package name '%s'\n", pkgname);
- return false;
}
+ if (c == '.') {
+ hasSep = true;
+ front = true;
+ continue;
+ }
+ LOG(WARNING) << "Bad package character " << c << " in " << packageName;
+ return false;
+ }
- x++;
+ if (front) {
+ LOG(WARNING) << "Missing separator in " << packageName;
+ return false;
}
- if (alpha == 1) {
- // Skip current character
- x++;
- while (*x) {
- if (!isalnum(*x)) {
- ALOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
- return false;
- }
- x++;
- }
+ for (; it != packageName.end(); it++) {
+ char c = *it;
+ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
+ if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
+ LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
+ return false;
}
return true;