Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5999 fix NPE in Coff archive fs when opening almost empty file

dev74736881b8b160be43
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/archive/CoffArchiveHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/archive/CoffArchiveHeader.java
index 4213d9aca3..1f50dce5d1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/archive/CoffArchiveHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/archive/CoffArchiveHeader.java
@@ -16,8 +16,7 @@
package ghidra.app.util.bin.format.coff.archive;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
import ghidra.app.util.bin.*;
import ghidra.app.util.bin.format.coff.CoffException;
@@ -42,13 +41,14 @@ public final class CoffArchiveHeader implements StructConverter {
* Returns true if the data contained in the {@link ByteProvider provider} contains
* a COFF Archive file.
*
- * @param provider
- * @return
- * @throws IOException
+ * @param provider {@link ByteProvider} stream
+ * @return boolean true if stream contains a CoffArchiveHeader at position 0
+ * @throws IOException if error reading
*/
public static boolean isMatch(ByteProvider provider) throws IOException {
- return (provider.length() > CoffArchiveConstants.MAGIC_LEN) && CoffArchiveConstants.MAGIC
- .equals(new String(provider.readBytes(0, CoffArchiveConstants.MAGIC_LEN)));
+ return (provider.length() >= CoffArchiveConstants.MAGIC_LEN) &&
+ Arrays.equals(CoffArchiveConstants.MAGIC_BYTES,
+ provider.readBytes(0, CoffArchiveConstants.MAGIC_LEN));
}
/**
@@ -75,9 +75,7 @@ public final class CoffArchiveHeader implements StructConverter {
CoffArchiveHeader cah = new CoffArchiveHeader();
- long eofPos = reader.length() - CoffArchiveMemberHeader.CAMH_MIN_SIZE;
-
- while (reader.getPointerIndex() < eofPos) {
+ while (reader.hasNext(CoffArchiveMemberHeader.CAMH_MIN_SIZE)) {
if (monitor.isCancelled()) {
break;
}