Skip to content

Commit 7f1b476

Browse files
rbranWeitao-Sun
authored andcommitted
fix macho ImportEntry::ReadChainedImport32 string_view overrun
Apply the same name_offset bounds check and strnlen guard already present in ReadChainedImport32 to ReadChainedImportAddend32 and ReadChainedImportAddend64, and extract the shared logic into a SymbolNameAt helper so the three functions share one implementation instead of three slightly different copies.
1 parent fa01a8b commit 7f1b476

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

view/macho/chained_fixups.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,21 @@ auto FixupReaderForFormat(int format) -> std::pair<uint64_t, FixupInfo>(*)(Binar
240240
throw std::invalid_argument("Unknown chained pointer format: " + std::to_string(format));
241241
}
242242

243+
// Returns the NUL-terminated string starting at `offset` within `symbolData`, or an
244+
// empty view if `offset` does not fall within `symbolData`.
245+
std::string_view SymbolNameAt(std::span<const char> symbolData, uint32_t offset)
246+
{
247+
if (symbolData.size() <= offset)
248+
return std::string_view();
249+
return std::string_view(&symbolData[offset], strnlen(&symbolData[offset], symbolData.size() - offset));
250+
}
251+
243252
ImportEntry ReadChainedImport32(BinaryReader& reader, std::span<const char> symbolData)
244253
{
245254
dyld_chained_import import;
246255
reader.Read(&import, sizeof(import));
247-
std::string_view view;
248-
if (symbolData.size() > import.name_offset) {
249-
view = std::string_view(&symbolData[import.name_offset]);
250-
} else {
251-
view = std::string_view();
252-
}
253256
return {
254-
view,
257+
SymbolNameAt(symbolData, import.name_offset),
255258
0,
256259
import.lib_ordinal > 0xF0 ? static_cast<int8_t>(import.lib_ordinal) : static_cast<int32_t>(import.lib_ordinal),
257260
(bool)import.weak_import,
@@ -263,7 +266,7 @@ ImportEntry ReadChainedImportAddend32(BinaryReader& reader, std::span<const char
263266
dyld_chained_import_addend import;
264267
reader.Read(&import, sizeof(import));
265268
return {
266-
std::string_view(&symbolData[import.name_offset]),
269+
SymbolNameAt(symbolData, import.name_offset),
267270
static_cast<uint32_t>(import.addend),
268271
import.lib_ordinal > 0xF0 ? static_cast<int8_t>(import.lib_ordinal) : static_cast<int32_t>(import.lib_ordinal),
269272
(bool)import.weak_import,
@@ -275,7 +278,7 @@ ImportEntry ReadChainedImportAddend64(BinaryReader& reader, std::span<const char
275278
dyld_chained_import_addend64 import;
276279
reader.Read(&import, sizeof(import));
277280
return {
278-
std::string_view(&symbolData[import.name_offset]),
281+
SymbolNameAt(symbolData, import.name_offset),
279282
import.addend,
280283
import.lib_ordinal > 0xFFF0 ? static_cast<int16_t>(import.lib_ordinal) : static_cast<int32_t>(import.lib_ordinal),
281284
(bool)import.weak_import,

0 commit comments

Comments
 (0)