COMPRESS-197: Tar file for Android backup cannot be read
Allow more than one or two NUL or space characters at the end of a field
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@1369655 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
index 4940d1d..0a38336 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
@@ -130,10 +130,11 @@
throw new IllegalArgumentException(
exceptionMessage(buffer, offset, length, end-1, trailer));
}
- // May have additional NUL or space
- trailer = buffer[end-1];
- if (trailer == 0 || trailer == ' '){
+ // May have additional NULs or spaces
+ trailer = buffer[end - 1];
+ while (start < end - 1 && (trailer == 0 || trailer == ' ')) {
end--;
+ trailer = buffer[end - 1];
}
for ( ;start < end; start++) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index 680cb8c..d76360f 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -20,10 +20,12 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Calendar;
@@ -120,4 +122,24 @@
}
}
-}
\ No newline at end of file
+ @Test
+ public void testCompress197() throws Exception {
+ TarArchiveInputStream tar = getTestStream("/COMPRESS-197.tar");
+ try {
+ TarArchiveEntry entry = tar.getNextTarEntry();
+ while (entry != null) {
+ entry = tar.getNextTarEntry();
+ }
+ } catch (IOException e) {
+ fail("COMPRESS-197: " + e.getMessage());
+ } finally {
+ tar.close();
+ }
+ }
+
+ private TarArchiveInputStream getTestStream(String name) {
+ return new TarArchiveInputStream(
+ TarArchiveInputStreamTest.class.getResourceAsStream(name));
+ }
+
+}
diff --git a/src/test/resources/COMPRESS-197.tar b/src/test/resources/COMPRESS-197.tar
new file mode 100644
index 0000000..2f42ee0
--- /dev/null
+++ b/src/test/resources/COMPRESS-197.tar
Binary files differ