OSDN Git Service

Fix BufferOverflowException when merging dexes
authorBenoit Lamarche <benoitlamarche@google.com>
Wed, 27 Nov 2013 15:38:17 +0000 (16:38 +0100)
committerYohann Roussel <yroussel@google.com>
Tue, 3 Dec 2013 13:04:48 +0000 (13:04 +0000)
Bug: 11519714

The bug was due to the fact that when merging 2 dexes, not enough size may be reserved for the "typeLists" section.
This is because we only aligned the sum of the size of both sections.
If both typeLists sections were aligned with 2 and not with 4, we are 4 bytes short when writing.

Change-Id: I73b51eb25434a622143011741a69b88d42507f43
(cherry picked from commit 2241dbe132cf90b58f93c014bdd807405b7f82f5)

dx/src/com/android/dx/merge/DexMerger.java

index 8080947..507c076 100644 (file)
@@ -28,6 +28,7 @@ import com.android.dex.ProtoId;
 import com.android.dex.SizeOf;
 import com.android.dex.TableOfContents;
 import com.android.dex.TypeList;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -1049,7 +1050,12 @@ public final class DexMerger {
                     + contents.methodIds.size * SizeOf.MEMBER_ID_ITEM
                     + contents.classDefs.size * SizeOf.CLASS_DEF_ITEM;
             mapList = SizeOf.UINT + (contents.sections.length * SizeOf.MAP_ITEM);
-            typeList += contents.typeLists.byteCount;
+            typeList += fourByteAlign(contents.typeLists.byteCount); // We count each dex's
+            // typelists section as realigned on 4 bytes, because each typelist of each dex's
+            // typelists section is aligned on 4 bytes. If we didn't, there is a case where each
+            // size of both dex's typelists section is a multiple of 2 but not a multiple of 4,
+            // and the sum of both sizes is a multiple of 4 but would not be sufficient to write
+            // each typelist aligned on 4 bytes.
             stringData += contents.stringDatas.byteCount;
             annotationsDirectory += contents.annotationsDirectories.byteCount;
             annotationsSet += contents.annotationSets.byteCount;