Skip to content

Commit 6aeafdd

Browse files
JDevlieghereadrian-prantl
authored andcommitted
[lldb] Fix a off-by-one error in ParseSupportFilesFromPrologue (llvm#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 llvm#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 llvm#71458. Once that PR is merged, this will be covered by all the DWARF v5 tests. (cherry picked from commit fa7e07e)
1 parent 1eeb3fa commit 6aeafdd

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

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

0 commit comments

Comments
 (0)