Address fopen errors

This commit is contained in:
Jeremy 2023-08-27 20:46:00 -04:00
parent 4b11b87e4d
commit 0977253505
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
5 changed files with 26 additions and 4 deletions

View File

@ -16,6 +16,7 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <ios>
#include <sstream>
#include <string>
@ -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

View File

@ -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<std::array<char, 4>>(file, 0);
internal_verify(magic == (std::array<char, 4>{0x7F, 'E', 'L', 'F'}));

View File

@ -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<uint32_t>(obj_file, 0);
bool is_64 = is_magic_64(magic);
bool should_swap = should_swap_bytes(magic);

View File

@ -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<std::array<char, 2>>(file, 0);
internal_verify(memcmp(magic.data(), "MZ", 2) == 0);
DWORD e_lfanew = pe_byteswap_if_needed(load_bytes<DWORD>(file, 0x3c)); // dos header + 0x3c

View File

@ -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;