COMPRESS-223 fix NPE in TarBuffer.tryToConsumeSecondEOFRecord - patch by Jeremy Gustie

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@1475758 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cdfc2d9..9d2dc1c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,11 @@
   <body>
     <release version="1.6" date="not released, yet"
              description="Release 1.6">
+      <action type="fix" date="2013-04-25" issue="COMPRESS-223"
+              due-to="Jeremy Gustie">
+        TarBuffer.tryToConsumeSecondEOFRecord could throw a
+        NullPointerException
+      </action>
     </release>
     <release version="1.5" date="2013-03-14"
              description="Release 1.5">
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index eb8cd70..d20d039 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -311,11 +311,11 @@
 
         byte[] headerBuf = buffer.readRecord();
 
-        if (headerBuf == null) {
+        if (buffer.isEOFRecord(headerBuf)) {
             hasHitEOF = true;
-        } else if (buffer.isEOFRecord(headerBuf)) {
-            hasHitEOF = true;
-            buffer.tryToConsumeSecondEOFRecord();
+            if (headerBuf != null) {
+                buffer.tryToConsumeSecondEOFRecord();
+            }
         }
 
         return hasHitEOF ? null : headerBuf;
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java
index 9c41ae5..4e13f54 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java
@@ -153,12 +153,13 @@
      * @return true if the record data is an End of Archive
      */
     public boolean isEOFRecord(byte[] record) {
-        for (int i = 0, sz = getRecordSize(); i < sz; ++i) {
-            if (record[i] != 0) {
-                return false;
+        if (record != null) {
+            for (int i = 0, sz = getRecordSize(); i < sz; ++i) {
+                if (record[i] != 0) {
+                    return false;
+                }
             }
         }
-
         return true;
     }
 
@@ -181,7 +182,7 @@
     /**
      * Read a record from the input stream and return the data.
      *
-     * @return The record data.
+     * @return The record data or null if EOF has been hit.
      * @throws IOException on error
      */
     public byte[] readRecord() throws IOException {
@@ -407,12 +408,12 @@
     }
 
     /**
-     * Tries to read the next record rewinding the stream if if is not a EOF record.
+     * Tries to read the next record rewinding the stream if it is not a EOF record.
      *
      * <p>This is meant to protect against cases where a tar
      * implemenation has written only one EOF record when two are
      * expected.  Actually this won't help since a non-conforming
-     * implementation likely won't fill full blocks consisting of - be
+     * implementation likely won't fill full blocks consisting of - by
      * default - ten records either so we probably have already read
      * beyond the archive anyway.</p>
      */