diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h index 44a19c7b13f9a..da2f43bfb56c7 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h @@ -543,6 +543,19 @@ class DWARFDebugNames : public DWARFAcceleratorTable { return StrData.getCStr(&Off); } + /// Compares the name of this entry against Target, returning true if they + /// are equal. This is more efficient in hot code paths that do not need the + /// length of the name. + bool sameNameAs(StringRef Target) const { + // Note: this is not the name, but the rest of debug_str starting from + // name. This handles corrupt data (non-null terminated) without + // overrunning the buffer. + StringRef Data = StrData.getData().substr(StringOffset); + size_t TargetSize = Target.size(); + return Data.size() > TargetSize && !Data[TargetSize] && + strncmp(Data.data(), Target.data(), TargetSize) == 0; + } + /// Returns the offset of the first Entry in the list. uint64_t getEntryOffset() const { return EntryOffset; } }; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp index 03ad5d133cadd..94d52b6c4ade5 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp @@ -921,7 +921,7 @@ DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() { if (Hdr.BucketCount == 0) { // No Hash Table, We need to search through all names in the Name Index. for (const NameTableEntry &NTE : *CurrentIndex) { - if (NTE.getString() == Key) + if (NTE.sameNameAs(Key)) return NTE.getEntryOffset(); } return std::nullopt; @@ -942,7 +942,7 @@ DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() { return std::nullopt; // End of bucket NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index); - if (NTE.getString() == Key) + if (NTE.sameNameAs(Key)) return NTE.getEntryOffset(); } return std::nullopt;