diff --git a/src/platform/common.hpp b/src/platform/common.hpp index 61324cc..87fb40d 100644 --- a/src/platform/common.hpp +++ b/src/platform/common.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -259,6 +260,12 @@ T load_bytes(FILE* obj_file, off_t offset) { return object; } +class file_error : std::exception { + virtual const char* what() const noexcept { + return "Unable to read file"; + } +}; + #ifdef _MSC_VER #pragma warning(pop) #endif diff --git a/src/platform/elf.hpp b/src/platform/elf.hpp index 2e9c73f..22e9979 100644 --- a/src/platform/elf.hpp +++ b/src/platform/elf.hpp @@ -53,6 +53,9 @@ static uintptr_t elf_get_module_image_base_from_program_table( static uintptr_t elf_get_module_image_base(const std::string& obj_path) { FILE* file = fopen(obj_path.c_str(), "rb"); + if(file == nullptr) { + throw file_error(); + } // Initial checks/metadata auto magic = load_bytes>(file, 0); internal_verify(magic == (std::array{0x7F, 'E', 'L', 'F'})); diff --git a/src/platform/mach-o.hpp b/src/platform/mach-o.hpp index 8877d74..e519d56 100644 --- a/src/platform/mach-o.hpp +++ b/src/platform/mach-o.hpp @@ -136,6 +136,9 @@ static uintptr_t macho_get_text_vmaddr_fat(FILE* obj_file, bool should_swap) { static uintptr_t macho_get_text_vmaddr(const char* path) { FILE* obj_file = fopen(path, "rb"); + if(obj_file == nullptr) { + throw file_error(); + } uint32_t magic = load_bytes(obj_file, 0); bool is_64 = is_magic_64(magic); bool should_swap = should_swap_bytes(magic); diff --git a/src/platform/pe.hpp b/src/platform/pe.hpp index d9e3e08..94f5ad1 100644 --- a/src/platform/pe.hpp +++ b/src/platform/pe.hpp @@ -24,6 +24,9 @@ T pe_byteswap_if_needed(T value) { static uintptr_t pe_get_module_image_base(const std::string& obj_path) { FILE* file = fopen(obj_path.c_str(), "rb"); + if(file == nullptr) { + throw file_error(); + } auto magic = load_bytes>(file, 0); internal_verify(memcmp(magic.data(), "MZ", 2) == 0); DWORD e_lfanew = pe_byteswap_if_needed(load_bytes(file, 0x3c)); // dos header + 0x3c diff --git a/src/symbols/symbols_with_addr2line.cpp b/src/symbols/symbols_with_addr2line.cpp index 9911aee..7647d9d 100644 --- a/src/symbols/symbols_with_addr2line.cpp +++ b/src/symbols/symbols_with_addr2line.cpp @@ -317,10 +317,16 @@ namespace cpptrace { // on macos when looking up the shared object containing `start`. if(!entry.obj_path.empty()) { ///fprintf(stderr, "%s %s\n", to_hex(entry.raw_address).c_str(), to_hex(entry.raw_address - entry.obj_base + base).c_str()); - entries[entry.obj_path].emplace_back( - to_hex(entry.raw_address - entry.obj_base + get_module_image_base(entry)), - trace[i] - ); + try { + entries[entry.obj_path].emplace_back( + to_hex(entry.raw_address - entry.obj_base + get_module_image_base(entry)), + trace[i] + ); + } catch(file_error) { + // + } catch(...) { + throw; + } // Set what is known for now, and resolutions from addr2line should overwrite trace[i].filename = entry.obj_path; trace[i].symbol = entry.symbol;