OSDN Git Service

Prevent overflow in the AVD creation dialog
authorXavier Ducrohet <xav@android.com>
Sat, 20 Mar 2010 00:48:39 +0000 (17:48 -0700)
committerXavier Ducrohet <xav@android.com>
Fri, 26 Mar 2010 21:12:55 +0000 (14:12 -0700)
Entering a sd card size too big would overflow
the int variable and revert to the message about
the SD Card being <9MB.

Fixed the overflow for normal values and added a check
on values too big (1TB+)

Bug: 2528357

Change-Id: I052e6ca20c4919ab621138250f21d8eaa7e1d01d

sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdCreationDialog.java

index 3734a42..01c5dcd 100644 (file)
@@ -742,19 +742,30 @@ final class AvdCreationDialog extends GridDialog {
             } else {
                 String valueString = mSdCardSize.getText();
                 if (valueString.length() > 0) {
-                    int value = Integer.parseInt(valueString); // verifier makes this
-                                                               // unlikely to fail.
-                    switch (mSdCardSizeCombo.getSelectionIndex()) {
-                        case 0:
-                            value *= 1024;
-                            break;
-                        case 1:
-                            value *= 1024 * 1024;
-                            break;
-                    }
-
-                    if (value < 9 * 1024 * 1024) {
-                        error = "SD Card size must be at least 9MB";
+                    // prevent overflow: no more than 999GB
+                    // 10 digit for MiB, 13 for KiB
+                    if (valueString.length() >= 10 +
+                            (mSdCardSizeCombo.getSelectionIndex() == 0 ? 3 : 0)) {
+                        error = "SD Card size is too big!";
+                    } else {
+                        try {
+                            long value = Long.parseLong(valueString);
+
+                            switch (mSdCardSizeCombo.getSelectionIndex()) {
+                                case 0:
+                                    value *= 1024L;
+                                    break;
+                                case 1:
+                                    value *= 1024L * 1024L;
+                                    break;
+                            }
+
+                            if (value < 9 * 1024 * 1024) {
+                                error = "SD Card size must be at least 9MB";
+                            }
+                        } catch (NumberFormatException e) {
+                            // will never happen thanks to the VerifyListener.
+                        }
                     }
                 }
             }