Skip to content

Commit fa7e07e

Browse files
authored
[lldb] Fix a off-by-one error in ParseSupportFilesFromPrologue (#71984)
This fixes a subtle and previously harmless off-by-one bug in ParseSupportFilesFromPrologue. The function accounts for the start index being one-based for DWARF v4 and earlier and zero-based for DWARF v5 and later. However, the same care wasn't taken for the end index. This bug existed unnoticed because GetFileByIndex gracefully handles an invalid index. However, the bug manifested itself after #71458, which added a call to getFileNameEntry which requires the index to be valid. No test as the bug cannot be observed without the changes from #71458. Once that PR is merged, this will be covered by all the DWARF v5 tests.
1 parent 0448a1c commit fa7e07e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,24 @@ ParseSupportFilesFromPrologue(const lldb::ModuleSP &module,
210210
FileSpec::Style style,
211211
llvm::StringRef compile_dir = {}) {
212212
FileSpecList support_files;
213-
size_t first_file = 0;
214-
if (prologue.getVersion() <= 4) {
215-
// File index 0 is not valid before DWARF v5. Add a dummy entry to ensure
216-
// support file list indices match those we get from the debug info and line
217-
// tables.
213+
214+
// Handle the case where there are no files first to avoid having to special
215+
// case this later.
216+
if (prologue.FileNames.empty())
217+
return support_files;
218+
219+
// Before DWARF v5, the line table indexes were one based.
220+
const bool is_one_based = prologue.getVersion() < 5;
221+
const size_t file_names = prologue.FileNames.size();
222+
const size_t first_file_idx = is_one_based ? 1 : 0;
223+
const size_t last_file_idx = is_one_based ? file_names : file_names - 1;
224+
225+
// Add a dummy entry to ensure the support file list indices match those we
226+
// get from the debug info and line tables.
227+
if (is_one_based)
218228
support_files.Append(FileSpec());
219-
first_file = 1;
220-
}
221229

222-
const size_t number_of_files = prologue.FileNames.size();
223-
for (size_t idx = first_file; idx <= number_of_files; ++idx) {
230+
for (size_t idx = first_file_idx; idx <= last_file_idx; ++idx) {
224231
std::string remapped_file;
225232
if (auto file_path = GetFileByIndex(prologue, idx, compile_dir, style)) {
226233
if (auto remapped = module->RemapSourceFile(llvm::StringRef(*file_path)))

0 commit comments

Comments
 (0)