Skip to content

Commit 110d7d7

Browse files
committed
gh-137293 Clear exceptions before search_elf_file_for_section
1 parent c6b5c64 commit 110d7d7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Python/remote_debug.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,23 +572,35 @@ search_elf_file_for_section(
572572

573573
int fd = open(elf_file, O_RDONLY);
574574
if (fd < 0) {
575+
PyErr_Format(PyExc_OSError,
576+
"Cannot open ELF file '%s' for section '%s' search: %s",
577+
elf_file, secname, strerror(errno));
575578
goto exit;
576579
}
577580

578581
struct stat file_stats;
579582
if (fstat(fd, &file_stats) != 0) {
583+
PyErr_Format(PyExc_OSError,
584+
"Cannot get file size for ELF file '%s' during section '%s' search: %s",
585+
elf_file, secname, strerror(errno));
580586
goto exit;
581587
}
582588

583589
file_memory = mmap(NULL, file_stats.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
584590
if (file_memory == MAP_FAILED) {
591+
PyErr_Format(PyExc_OSError,
592+
"Cannot memory map ELF file '%s' (size: %lld bytes) for section '%s' search: %s",
593+
elf_file, (long long)file_stats.st_size, secname, strerror(errno));
585594
goto exit;
586595
}
587596

588597
Elf_Ehdr* elf_header = (Elf_Ehdr*)file_memory;
589598

590599
// Validate ELF header
591600
if (elf_header->e_shstrndx >= elf_header->e_shnum) {
601+
PyErr_Format(PyExc_RuntimeError,
602+
"Invalid ELF file '%s': string table index %u >= section count %u",
603+
elf_file, elf_header->e_shstrndx, elf_header->e_shnum);
592604
goto exit;
593605
}
594606

@@ -623,6 +635,9 @@ search_elf_file_for_section(
623635
}
624636

625637
if (first_load_segment == NULL) {
638+
PyErr_Format(PyExc_RuntimeError,
639+
"No PT_LOAD segment found in ELF file '%s' (%u program headers examined)",
640+
elf_file, elf_header->e_phnum);
626641
goto exit;
627642
}
628643

@@ -635,6 +650,9 @@ search_elf_file_for_section(
635650
munmap(file_memory, file_stats.st_size);
636651
}
637652
if (fd >= 0 && close(fd) != 0) {
653+
PyErr_Format(PyExc_OSError,
654+
"Failed to close ELF file '%s': %s",
655+
elf_file, strerror(errno));
638656
result = 0;
639657
}
640658
return result;
@@ -710,6 +728,7 @@ search_linux_map_for_section(proc_handle_t *handle, const char* secname, const c
710728
}
711729

712730
if (strstr(filename, substr)) {
731+
PyErr_Clear();
713732
retval = search_elf_file_for_section(handle, secname, start, path);
714733
if (retval) {
715734
break;

0 commit comments

Comments
 (0)