Skip to content

Commit c8f40ad

Browse files
authored
Fix max array size (#39)
* Fix max array size <mahboobe.haddadi@cafebazaar.ir>
1 parent 4fe3923 commit c8f40ad

6 files changed

Lines changed: 17 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Bundle signer is a command-line tool. You need to install **Java 8 or above** to
1616
shows how to generate signed binary file of your bundle application assuming you are using Java keystore to sign APK
1717
files of your application:
1818
```sh
19-
java -jar bundlesigner-0.1.12.jar genbin -v --bundle app.aab --bin . --v2-signing-enabled true --v3-signing-enabled false --ks key.jks
19+
java -jar bundlesigner-0.1.13.jar genbin -v --bundle app.aab --bin . --v2-signing-enabled true --v3-signing-enabled false --ks key.jks
2020
```
2121
This generates signed digest of the provided bundle. The output of this command is a binary file that contains signed
2222
digest of all APK files that can be extracted from the bundle. Signing is performed using one or more signers, each

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>ir.cafebazaar</groupId>
66
<artifactId>bundlesigner</artifactId>
7-
<version>0.1.12</version>
7+
<version>0.1.13</version>
88

99
<properties>
1010
<maven.compiler.target>1.8</maven.compiler.target>

src/main/java/ir/cafebazaar/apksig/internal/util/ByteArrayDataSink.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ private void ensureAvailable(int minAvailable) throws IOException {
101101
if (minCapacity <= mArray.length) {
102102
return;
103103
}
104-
if (minCapacity > Integer.MAX_VALUE) {
104+
int memoryGuard = 1024;
105+
int maxArraySize = Integer.MAX_VALUE - memoryGuard;
106+
if (minCapacity > maxArraySize) {
105107
throw new IOException(
106-
"Required capacity too large: " + minCapacity + ", max: " + Integer.MAX_VALUE);
108+
"Required capacity too large: " + minCapacity + ", max: " + maxArraySize);
107109
}
108-
int doubleCurrentSize = (int) Math.min(mArray.length * 2L, Integer.MAX_VALUE);
110+
int doubleCurrentSize = (int) Math.min(mArray.length * 2L, maxArraySize);
109111
int newSize = (int) Math.max(minCapacity, doubleCurrentSize);
110112
mArray = Arrays.copyOf(mArray, newSize);
111113
}

src/main/java/ir/cafebazaar/bundlesigner/BundleSignerTool.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ public static void main(String[] params) throws Exception {
167167
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
168168
exitCode = 4;
169169

170-
} finally {
171-
if (!exitMessage.isEmpty())
170+
} catch (OutOfMemoryError e){
171+
exitMessage = e.getMessage() + "\n" + Arrays.toString(e.getStackTrace());
172+
exitCode = 9;
173+
}finally {
174+
if (!exitMessage.isEmpty()) {
172175
System.err.println(exitMessage);
176+
logger.info(exitMessage);
177+
}
173178
System.exit(exitCode);
174179
}
175180
}

src/main/java/ir/cafebazaar/bundlesigner/command/GenBinCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ public void execute() throws Exception {
118118
logger.info("started genbin command.");
119119
String apksPath = BundleToolWrapper.buildApkSet(bundle, TMP_DIR_PATH, false);
120120
String universalPath = BundleToolWrapper.buildApkSet(bundle, TMP_DIR_PATH, true);
121+
System.gc();
121122

122123
File binV1 = new File(TMP_DIR_PATH + File.separator + "binv1");
123124
File binV2V3 = new File(TMP_DIR_PATH + File.separator + "binv2_v3");
124125

125126
extractAndSignApkSet(apksPath, binV1, binV2V3);
126-
127127
extractAndSignApkSet(universalPath, binV1, binV2V3);
128128

129129
generateFinalBinFile(binV1, binV2V3);
@@ -144,6 +144,7 @@ private void extractAndSignApkSet(String apksPath, File binV1, File binV2V3) thr
144144
for (FileHeader apkSetEntry : apkSetEntries) {
145145
if (!apkSetEntry.getFileName().contains("apk"))
146146
continue;
147+
logger.info("signing " + apkSetEntry.getFileName());
147148

148149
File apk = new File(TMP_DIR_PATH + File.separator + apkSetEntry.getFileName());
149150
new File(apk.getParent()).mkdirs();
@@ -152,6 +153,7 @@ private void extractAndSignApkSet(String apksPath, File binV1, File binV2V3) thr
152153
apkName = apkName.replace("/", "_");
153154

154155
calculateSignOfApk(apkName, binV1, binV2V3, apk);
156+
logger.info("signed " + apkSetEntry.getFileName());
155157
}
156158
}
157159

src/main/resources/log4j.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
log4j.rootLogger=INFO, file
2-
32
log4j.appender.file=org.apache.log4j.RollingFileAppender
43
log4j.appender.file.File=${java.io.tmpdir}/bundlesigner.log
54
log4j.appender.file.layout=org.apache.log4j.PatternLayout

0 commit comments

Comments
 (0)