release-request-cffd31b2-8adb-42f8-bff0-d688abeb72ba-for-git_studio-3.0-release-4326479 snap-temp-L53800000101079434
Change-Id: I6dada8ecbe6c10873a42e58d1aaf2afc862c9e43
diff --git a/dx/junit-tests/com/android/dx/util/ByteArrayAnnotatedOutputTest.java b/dx/junit-tests/com/android/dx/util/ByteArrayAnnotatedOutputTest.java
new file mode 100644
index 0000000..da5a1df
--- /dev/null
+++ b/dx/junit-tests/com/android/dx/util/ByteArrayAnnotatedOutputTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dx.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import org.junit.Test;
+
+public final class ByteArrayAnnotatedOutputTest {
+ @Test
+ public void testArrayZeroedOut() {
+ int length = 100;
+ byte[] data = new byte[length];
+ Arrays.fill(data, (byte) 0xFF);
+
+ ByteArrayAnnotatedOutput output = new ByteArrayAnnotatedOutput(data);
+
+ output.writeZeroes(length);
+
+ for (int i = 0; i < length; i++) {
+ assertEquals("Position " + i + " has not been zeroed out", 0, data[i]);
+ }
+ }
+
+ @Test
+ public void testArrayAligned() {
+ int length = 16;
+ byte[] data = new byte[length];
+ Arrays.fill(data, (byte) 0xFF);
+
+ ByteArrayAnnotatedOutput output = new ByteArrayAnnotatedOutput(data);
+
+ // write at least one byte, so alignment is not correct
+ output.writeByte(0);
+ output.alignTo(length);
+
+ for (int i = 0; i < length; i++) {
+ assertEquals("Position " + i + " has not been zeroed out", 0, data[i]);
+ }
+ }
+}
diff --git a/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java b/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java
index 187d886..7c2a26b 100644
--- a/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java
+++ b/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
+import java.util.Arrays;
/**
* Implementation of {@link AnnotatedOutput} which stores the written data
@@ -317,9 +318,9 @@
}
/*
- * There is no need to actually write zeroes, since the array is
- * already preinitialized with zeroes.
+ * We need to write zeroes, since the array might be reused across different dx invocations.
*/
+ Arrays.fill(data, cursor, end, (byte) 0);
cursor = end;
}
@@ -342,9 +343,9 @@
}
/*
- * There is no need to actually write zeroes, since the array is
- * already preinitialized with zeroes.
+ * We need to write zeroes, since the array might be reused across different dx invocations.
*/
+ Arrays.fill(data, cursor, end, (byte) 0);
cursor = end;
}