aboutsummaryrefslogtreecommitdiffstats
path: root/sdkmanager
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2010-05-24 17:27:55 -0700
committerXavier Ducrohet <xav@android.com>2010-05-24 17:33:17 -0700
commitb0973d97be079161c6e92457edaf0bf927585c95 (patch)
tree7f8e1fbec9469db31f5b3d141caaaf115145348d /sdkmanager
parent1e8a0225f544baa63098badd68a25e69dfd966bc (diff)
downloadsdk-b0973d97be079161c6e92457edaf0bf927585c95.zip
sdk-b0973d97be079161c6e92457edaf0bf927585c95.tar.gz
sdk-b0973d97be079161c6e92457edaf0bf927585c95.tar.bz2
Fix SD Card size check to prevent int overflow.
Also added a check for sd card that are too big (>999GB) to prevent long overflow. Change-Id: Ie3ae57e679151dacf0e1fbbec23b29ccfdad3b6a
Diffstat (limited to 'sdkmanager')
-rw-r--r--sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java42
1 files changed, 30 insertions, 12 deletions
diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java
index c4914b2..35ce0b0 100644
--- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java
+++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java
@@ -607,18 +607,36 @@ public final class AvdManager {
Matcher m = SDCARD_SIZE_PATTERN.matcher(sdcard);
if (m.matches()) {
// get the sdcard values for checks
- int sdcardSize = Integer.parseInt(m.group(1)); // pattern check
- // above makes
- // this unlikely to fail
- String sdcardSizeModifier = m.group(2);
- if ("K".equals(sdcardSizeModifier)) {
- sdcardSize *= 1024;
- } else { // must be "M" per the pattern
- sdcardSize *= 1024 * 1024;
- }
-
- if (sdcardSize < 9 * 1024 * 1024) {
- log.error(null, "SD Card size must be at least 9MB");
+ try {
+ long sdcardSize = Long.parseLong(m.group(1));
+
+ // prevent overflow: no more than 999GB
+ // 10 digit for MiB, 13 for KiB
+ int digitCount = m.group(1).length();
+
+ String sdcardSizeModifier = m.group(2);
+ if ("K".equals(sdcardSizeModifier)) {
+ sdcardSize *= 1024L;
+ } else { // must be "M" per the pattern
+ sdcardSize *= 1024L * 1024L;
+ digitCount += 3; // convert the number of digit into "KiB"
+ }
+
+ if (digitCount >= 13) {
+ log.error(null, "SD Card size is too big!");
+ needCleanup = true;
+ return null;
+ }
+
+ if (sdcardSize < 9 * 1024 * 1024) {
+ log.error(null, "SD Card size must be at least 9MB");
+ needCleanup = true;
+ return null;
+ }
+ } catch (NumberFormatException e) {
+ // this should never happen since the string is validated
+ // by the regexp
+ log.error(null, "Unable to parse SD Card size");
needCleanup = true;
return null;
}