Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3261: Fixing possible IllegalArgumentException in sort comparator. Integer subtraction could result in overflow.

Ryan Kurtz9edca9afec0c
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/SymbolTableCommand.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/SymbolTableCommand.java
index 4ffceafaf0..5bdca403cc 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/SymbolTableCommand.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/SymbolTableCommand.java
@@ -84,9 +84,11 @@ public class SymbolTableCommand extends LoadCommand {
nlistList.add(NList.createNList(reader, is32bit));
}
// sort the entries by the index in the string table, so don't jump around reading
- List<NList> sortedList = nlistList.stream().sorted(
- (o1, o2) -> o1.getStringTableIndex() - o2.getStringTableIndex()).collect(
- Collectors.toList());
+ List<NList> sortedList = nlistList
+ .stream()
+ .sorted((o1, o2) -> Integer.valueOf(o1.getStringTableIndex())
+ .compareTo(Integer.valueOf(o2.getStringTableIndex())))
+ .collect(Collectors.toList());
// initialize the NList strings from string table
long stringTableOffset = stroff;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheLocalSymbolsInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheLocalSymbolsInfo.java
index c2bf823f48..9811e8c6f6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheLocalSymbolsInfo.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheLocalSymbolsInfo.java
@@ -159,9 +159,11 @@ public class DyldCacheLocalSymbolsInfo implements StructConverter {
monitor.incrementProgress(1);
}
// sort the entries by the index in the string table, so don't jump around reading
- List<NList> sortedList = nlistList.stream().sorted(
- (o1, o2) -> o1.getStringTableIndex() - o2.getStringTableIndex()).collect(
- Collectors.toList());
+ List<NList> sortedList = nlistList
+ .stream()
+ .sorted((o1, o2) -> Integer.valueOf(o1.getStringTableIndex())
+ .compareTo(Integer.valueOf(o2.getStringTableIndex())))
+ .collect(Collectors.toList());
// initialize the NList strings from string table
long stringTableOffset = startIndex + stringsOffset;