Fix StringIndexOutOfBoundsException in PackageManagerService
The method packageManagerService.getNextCodePath(String oldCodePath,
String prefix, String suffix) threw StringIndexOutOfBoundsException if
oldCodePath does not contain prefix, and prefix is longer than
oldCodePath, or if the preix and suffix overlap.
Fixes http://b/issue?id=2404232
Change-Id: Ib8abb16f8bfd08f607476d9289f46d170c43a076
diff --git a/services/java/com/android/server/PackageManagerService.java b/services/java/com/android/server/PackageManagerService.java
index 3ffb8bf..cd4ae4cc 100644
--- a/services/java/com/android/server/PackageManagerService.java
+++ b/services/java/com/android/server/PackageManagerService.java
@@ -4369,18 +4369,13 @@
String idxStr = "";
int idx = 1;
if (oldCodePath != null) {
- int eidx = -1;
- if (suffix != null) {
- eidx = oldCodePath.indexOf(suffix);
+ String subStr = oldCodePath;
+ if (subStr.startsWith(prefix)) {
+ subStr = subStr.substring(prefix.length());
}
- if (eidx == -1) {
- eidx = oldCodePath.length();
+ if (subStr.endsWith(suffix)) {
+ subStr = subStr.substring(0, subStr.length() - suffix.length());
}
- int sidx = oldCodePath.indexOf(prefix);
- if (sidx == -1) {
- sidx = 0;
- }
- String subStr = oldCodePath.substring(sidx + prefix.length(), eidx);
if (subStr != null) {
if (subStr.startsWith("-")) {
subStr = subStr.substring(1);