From ffb1b8ed68976049cc12c30f3eb5ce67f8b75ae3 Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 31 Mar 2024 16:02:39 -0500 Subject: [PATCH] A little more error handling cleanup --- src/binary/pe.hpp | 16 ++++++++-------- src/symbols/symbols_with_dbghelp.cpp | 4 ++-- src/unwind/unwind_with_dbghelp.cpp | 4 ++-- src/utils/dwarf.hpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/binary/pe.hpp b/src/binary/pe.hpp index 0a7b6d3..683c57e 100644 --- a/src/binary/pe.hpp +++ b/src/binary/pe.hpp @@ -34,30 +34,30 @@ namespace detail { errno_t ret = fopen_s(&file_ptr, object_path.c_str(), "rb"); auto file = raii_wrap(std::move(file_ptr), file_deleter); if(ret != 0 || file == nullptr) { - throw internal_error("Unable to read object file {}", object_path); + return internal_error("Unable to read object file {}", object_path); } auto magic = load_bytes>(file, 0); if(!magic) { - return magic.unwrap_error(); + return std::move(magic).unwrap_error(); } if(std::memcmp(magic.unwrap_value().data(), "MZ", 2) != 0) { return internal_error("File is not a PE file {}", object_path); } auto e_lfanew = load_bytes(file, 0x3c); // dos header + 0x3c if(!e_lfanew) { - return e_lfanew.unwrap_error(); + return std::move(e_lfanew).unwrap_error(); } DWORD nt_header_offset = pe_byteswap_if_needed(e_lfanew.unwrap_value()); auto signature = load_bytes>(file, nt_header_offset); // nt header + 0 if(!signature) { - return signature.unwrap_error(); + return std::move(signature).unwrap_error(); } if(std::memcmp(signature.unwrap_value().data(), "PE\0\0", 4) != 0) { return internal_error("File is not a PE file {}", object_path); } auto size_of_optional_header_raw = load_bytes(file, nt_header_offset + 4 + 0x10); // file header + 0x10 if(!size_of_optional_header_raw) { - return size_of_optional_header_raw.unwrap_error(); + return std::move(size_of_optional_header_raw).unwrap_error(); } WORD size_of_optional_header = pe_byteswap_if_needed(size_of_optional_header_raw.unwrap_value()); if(size_of_optional_header == 0) { @@ -65,7 +65,7 @@ namespace detail { } auto optional_header_magic_raw = load_bytes(file, nt_header_offset + 0x18); // optional header + 0x0 if(!optional_header_magic_raw) { - return optional_header_magic_raw.unwrap_error(); + return std::move(optional_header_magic_raw).unwrap_error(); } WORD optional_header_magic = pe_byteswap_if_needed(optional_header_magic_raw.unwrap_value()); VERIFY( @@ -77,7 +77,7 @@ namespace detail { // 32 bit auto bytes = load_bytes(file, nt_header_offset + 0x18 + 0x1c); // optional header + 0x1c if(!bytes) { - return bytes.unwrap_error(); + return std::move(bytes).unwrap_error(); } return to(pe_byteswap_if_needed(bytes.unwrap_value())); } else { @@ -85,7 +85,7 @@ namespace detail { // I get an "error: 'QWORD' was not declared in this scope" for some reason when using QWORD auto bytes = load_bytes(file, nt_header_offset + 0x18 + 0x18); // optional header + 0x18 if(!bytes) { - return bytes.unwrap_error(); + return std::move(bytes).unwrap_error(); } return to(pe_byteswap_if_needed(bytes.unwrap_value())); } diff --git a/src/symbols/symbols_with_dbghelp.cpp b/src/symbols/symbols_with_dbghelp.cpp index 107ba0b..0efbecf 100644 --- a/src/symbols/symbols_with_dbghelp.cpp +++ b/src/symbols/symbols_with_dbghelp.cpp @@ -426,7 +426,7 @@ namespace dbghelp { get_syminit_manager().init(proc); } else { if(!SymInitialize(proc, NULL, TRUE)) { - throw internal_error("Cpptrace SymInitialize failed"); + throw internal_error("SymInitialize failed"); } } for(const auto frame : frames) { @@ -443,7 +443,7 @@ namespace dbghelp { } if(get_cache_mode() != cache_mode::prioritize_speed) { if(!SymCleanup(proc)) { - throw internal_error("Cpptrace SymCleanup failed"); + throw internal_error("SymCleanup failed"); } } return trace; diff --git a/src/unwind/unwind_with_dbghelp.cpp b/src/unwind/unwind_with_dbghelp.cpp index 6e9d744..676af22 100644 --- a/src/unwind/unwind_with_dbghelp.cpp +++ b/src/unwind/unwind_with_dbghelp.cpp @@ -110,7 +110,7 @@ namespace detail { get_syminit_manager().init(proc); } else { if(!SymInitialize(proc, NULL, TRUE)) { - throw internal_error("Cpptrace SymInitialize failed"); + throw internal_error("SymInitialize failed"); } } while(trace.size() < max_depth) { @@ -147,7 +147,7 @@ namespace detail { } if(get_cache_mode() != cache_mode::prioritize_speed) { if(!SymCleanup(proc)) { - throw internal_error("Cpptrace SymCleanup failed"); + throw internal_error("SymCleanup failed"); } } return trace; diff --git a/src/utils/dwarf.hpp b/src/utils/dwarf.hpp index 9f065c3..98bf587 100644 --- a/src/utils/dwarf.hpp +++ b/src/utils/dwarf.hpp @@ -28,7 +28,7 @@ namespace libdwarf { char* msg = dwarf_errmsg(error); (void)dbg; // dwarf_dealloc_error(dbg, error); - throw internal_error("Cpptrace dwarf error {} {}", ev, msg); + throw internal_error("dwarf error {} {}", ev, msg); } struct die_object {