From d423361d8ac21ac42fb2a4294aa7cef0d34bae70 Mon Sep 17 00:00:00 2001 From: Omar Sandoval Date: Fri, 12 Jul 2019 01:25:25 -0700 Subject: [PATCH] libdrgn: dwarf_index: move .debug_str null-termination check Check it right after we read the section instead of when updating the index. --- libdrgn/dwarf_index.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/libdrgn/dwarf_index.c b/libdrgn/dwarf_index.c index 6fe016a8..8ae05de1 100644 --- a/libdrgn/dwarf_index.c +++ b/libdrgn/dwarf_index.c @@ -296,6 +296,7 @@ static struct drgn_error *read_sections(struct drgn_dwarf_index_file *file) Elf_Scn *scn = NULL; size_t section_index[DRGN_DWARF_INDEX_NUM_SECTIONS] = {}; size_t i; + Elf_Data *debug_str; ehdr = gelf_getehdr(file->elf, &ehdr_mem); if (!ehdr) @@ -345,6 +346,13 @@ static struct drgn_error *read_sections(struct drgn_dwarf_index_file *file) } } + debug_str = file->sections[SECTION_DEBUG_STR]; + if (debug_str->d_size == 0 || + ((char *)debug_str->d_buf)[debug_str->d_size - 1] != '\0') { + return drgn_error_create(DRGN_ERROR_DWARF_FORMAT, + ".debug_str is not null terminated"); + } + if (ehdr->e_type != ET_REL) return NULL; @@ -1638,16 +1646,6 @@ struct drgn_error *drgn_dwarf_index_update(struct drgn_dwarf_index *dindex) file = first; do { - Elf_Data *debug_str; - - debug_str = file->sections[SECTION_DEBUG_STR]; - if (debug_str->d_size == 0 || - ((char *)debug_str->d_buf)[debug_str->d_size - 1] != '\0') { - err = drgn_error_create(DRGN_ERROR_DWARF_FORMAT, - ".debug_str is not null terminated"); - goto out; - } - if ((err = read_cus(file, &cus, &num_cus, &cus_capacity))) goto out; file = file->next;