diff options
Diffstat (limited to 'cxx/include/aesxx/error.hpp')
-rw-r--r-- | cxx/include/aesxx/error.hpp | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/cxx/include/aesxx/error.hpp b/cxx/include/aesxx/error.hpp index 384a637..21f65d5 100644 --- a/cxx/include/aesxx/error.hpp +++ b/cxx/include/aesxx/error.hpp @@ -27,15 +27,20 @@ namespace aes { public: Error(const AES_ErrorDetails& err_details) - : std::runtime_error(format_error_message(err_details)) + : std::runtime_error{format_error_message(err_details)} { - copy_call_stack(err_details); + fill_call_stack(err_details); } - void for_each_in_call_stack(const std::function<void (void*, const std::string&)>& callback) const + void for_each_addr( + const std::function<void (const void*, const std::string&)>& callback) const { aux::CallStackFormatter formatter; - std::for_each(call_stack, call_stack + call_stack_size, [&formatter, &callback] (void* addr) + + std::for_each( + call_stack, + call_stack + call_stack_len, + [&formatter, &callback] (const void* addr) { callback(addr, formatter.format_address(addr)); }); @@ -47,24 +52,28 @@ namespace aes std::vector<char> buf; buf.resize(aes_format_error(&err_details, NULL, 0)); aes_format_error(&err_details, buf.data(), buf.size()); - return { buf.begin(), buf.end() }; + return {buf.begin(), buf.end()}; } - void copy_call_stack(const AES_ErrorDetails& err_details) + void fill_call_stack(const AES_ErrorDetails& err_details) { - call_stack_size = err_details.call_stack_size; - std::memcpy(call_stack, err_details.call_stack, call_stack_size * sizeof(void*)); + call_stack_len = err_details.call_stack_len; + + if (call_stack_len > AES_MAX_CALL_STACK_LENGTH) + call_stack_len = AES_MAX_CALL_STACK_LENGTH; + + std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*)); } - void* call_stack[AES_MAX_CALL_STACK_LENGTH]; - std::size_t call_stack_size; + std::size_t call_stack_len = 0; + const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; }; - std::ostream& operator<<(std::ostream& os, const Error& e) + std::ostream& operator<<(std::ostream& os, const Error& error) { - os << "AES error: " << e.what() << '\n'; + os << "AES error: " << error.what() << '\n'; os << "Call stack:\n"; - e.for_each_in_call_stack([&os] (void* addr, const std::string& name) + error.for_each_addr([&os] (const void* addr, const std::string& name) { os << '\t' << addr << ' ' << name << '\n'; }); |