Copilot commented on code in PR #685:
URL: https://github.com/apache/commons-compress/pull/685#discussion_r2265367933


##########
src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java:
##########
@@ -241,102 +232,172 @@ private String getExtendedName(final int offset) throws 
IOException {
      */
     @Deprecated
     public ArArchiveEntry getNextArEntry() throws IOException {
-        if (currentEntry != null) {
-            final long entryEnd = entryOffset + currentEntry.getLength();
-            final long skipped = org.apache.commons.io.IOUtils.skip(in, 
entryEnd - offset);
-            trackReadBytes(skipped);
-            currentEntry = null;
-        }
-        if (offset == 0) {
-            final byte[] expected = 
ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
-            final byte[] realized = IOUtils.readRange(in, expected.length);
-            final int read = realized.length;
-            trackReadBytes(read);
-            if (read != expected.length) {
-                throw new ArchiveException("Failed to read header. Occurred at 
byte: %,d", getBytesRead());
-            }
-            if (!Arrays.equals(expected, realized)) {
-                throw new ArchiveException("Invalid header " + 
ArchiveUtils.toAsciiString(realized));
+        return getNextEntry();
+    }
+
+    /*
+     * Returns the next AR file entry in this stream.
+     * <p>
+     *    The method skips special AR file entries, such as those used by GNU.
+     * </p>
+     * @return The next AR file entry.
+     * @throws IOException if the entry could not be read or is malformed.
+     */
+    @Override
+    public ArArchiveEntry getNextEntry() throws IOException {
+        skipGlobalSignature();
+
+        // Handle special GNU ar entries
+        boolean foundGNUStringTable = false;
+        do {
+            // If there is a current entry, skip any unread data and padding
+            if (currentEntry != null) {
+                IOUtils.skip(this, Long.MAX_VALUE); // Skip to end of current 
entry
+                skipRecordPadding(); // Skip padding to align to the next 
record
             }
-        }
-        if (offset % 2 != 0) {
-            if (in.read() < 0) {
-                // hit eof
-                return null;
+
+            // Read the next header record
+            final byte[] headerBuf = getRecord();
+            if (headerBuf == null) {
+                // If we encountered special records but no file entry, the 
archive is malformed
+                if (foundGNUStringTable) {
+                    throw new EOFException("Premature end of ar archive: no 
regular entry after GNU string table.");
+                }

Review Comment:
   This check incorrectly assumes that a GNU string table must be followed by a 
regular entry. However, a GNU string table could be the last entry in an 
archive if it's not actually referenced by any subsequent entries. This would 
cause valid archives to be rejected.
   ```suggestion
                   // End of archive (even if the last entry was a GNU string 
table, this is valid)
   ```



##########
src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java:
##########
@@ -211,25 +202,25 @@ private String getBSDLongName(final String bsdLongName) 
throws IOException {
      */
     private String getExtendedName(final int offset) throws IOException {
         if (namebuffer == null) {
-            throw new ArchiveException("Cannot process GNU long file name as 
no // record was found");
+            throw new ArchiveException("Cannot process GNU long file name as 
no GNU string table was found");
+        }
+        if (offset < 0 || offset >= namebuffer.length) {

Review Comment:
   The condition `offset < 0` is unnecessary because the offset is parsed from 
a string using `ParsingUtils.parseIntValue(name.substring(1))` at line 290, 
which would throw an exception for negative values. The check should focus on 
the upper bound only.
   ```suggestion
           if (offset >= namebuffer.length) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to