From 5452e14db4f424d24bafd2dd0fe668de51b7115a Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Wed, 29 Jan 2025 06:00:09 -0500 Subject: [PATCH] GP-5321: Fixing PE debug coff symbol NPE --- .../format/pe/debug/DebugCOFFSymbolTable.java | 49 +++++++++---------- .../util/opinion/AbstractPeDebugLoader.java | 3 +- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbolTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbolTable.java index d89b0c88ce..4ffac5d065 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbolTable.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbolTable.java @@ -4,9 +4,9 @@ * 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. @@ -16,6 +16,8 @@ package ghidra.app.util.bin.format.pe.debug; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import ghidra.app.util.bin.BinaryReader; import ghidra.app.util.bin.format.pe.NTHeader; @@ -24,37 +26,34 @@ import ghidra.app.util.bin.format.pe.NTHeader; * A class to represent the COFF Symbol Table. */ public class DebugCOFFSymbolTable { - private int ptrToSymbolTable; - private int symbolCount; + private int ptrToSymbolTable; + private int symbolCount; - private DebugCOFFSymbol [] symbols; + private List symbols = new ArrayList<>(); public DebugCOFFSymbolTable(BinaryReader reader, DebugCOFFSymbolsHeader coffHeader, int offset) throws IOException { - this.ptrToSymbolTable = coffHeader.getFirstSymbolLVA() + offset; - this.symbolCount = coffHeader.getNumberOfSymbols(); + this.ptrToSymbolTable = coffHeader.getFirstSymbolLVA() + offset; + this.symbolCount = coffHeader.getNumberOfSymbols(); -//TODO: -//should symbol table info in NT Header agree with info in COFF Header? + //TODO: should symbol table info in NT Header agree with info in COFF Header? - if (symbolCount > 0 && symbolCount < NTHeader.MAX_SANE_COUNT) { - symbols = new DebugCOFFSymbol[symbolCount]; - for (int i = 0 ; i < symbolCount ; ++i) { - symbols[i] = new DebugCOFFSymbol(reader, - ptrToSymbolTable + (i * DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL), this); - } - } - } + if (symbolCount < NTHeader.MAX_SANE_COUNT) { + for (int i = 0; i < symbolCount; ++i) { + symbols.add(new DebugCOFFSymbol(reader, + ptrToSymbolTable + (i * DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL), this)); + } + } + } - int getStringTableIndex() { - return ptrToSymbolTable + (symbolCount * DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL); - } + int getStringTableIndex() { + return ptrToSymbolTable + (symbolCount * DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL); + } /** - * Returns the COFF symbols defined in this COFF symbol table. - * @return the COFF symbols defined in this COFF symbol table + * {@return the COFF symbols defined in this COFF symbol table} */ - public DebugCOFFSymbol [] getSymbols() { - return symbols; - } + public List getSymbols() { + return symbols; + } } diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java index 813204067f..676197706e 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java @@ -345,9 +345,8 @@ abstract class AbstractPeDebugLoader extends AbstractOrdinalSupportLoader { if (dcst == null) { return; } - DebugCOFFSymbol[] symbols = dcst.getSymbols(); int errorCount = 0; - for (DebugCOFFSymbol symbol : symbols) { + for (DebugCOFFSymbol symbol : dcst.getSymbols()) { if (monitor.isCancelled()) { return; }