From 3b5064f1ff6104e4082f1f26534a5967bd043f24 Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Thu, 5 Oct 2023 10:58:03 -0400 Subject: [PATCH 1/5] Fix forwarding issue --- src/platform/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/utils.hpp b/src/platform/utils.hpp index 7b5a7fc..8fd3e25 100644 --- a/src/platform/utils.hpp +++ b/src/platform/utils.hpp @@ -230,7 +230,7 @@ namespace detail { typename std::enable_if::type, optional>::value, int>::type = 0 > optional& operator=(U&& value) { - optional o(std::move(value)); + optional o(std::forward(value)); swap(o); return *this; } From 12b80f7af1e1765f8516504bb524b4c3ccf8c2af Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:01:03 -0400 Subject: [PATCH 2/5] Some fixes --- src/cpptrace.cpp | 32 +++++++++++------------ src/symbols/symbols_with_addr2line.cpp | 2 +- src/symbols/symbols_with_dbghelp.cpp | 2 +- src/symbols/symbols_with_libbacktrace.cpp | 2 +- src/symbols/symbols_with_libdwarf.cpp | 4 +-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/cpptrace.cpp b/src/cpptrace.cpp index 635da81..124c4fa 100644 --- a/src/cpptrace.cpp +++ b/src/cpptrace.cpp @@ -16,14 +16,14 @@ #include "platform/utils.hpp" #include "platform/object.hpp" -#define ESC "\033[" -#define RESET ESC "0m" -#define RED ESC "31m" -#define GREEN ESC "32m" -#define YELLOW ESC "33m" -#define BLUE ESC "34m" -#define MAGENTA ESC "35m" -#define CYAN ESC "36m" +#define ESC "\033[" // NOSONAR +#define RESET ESC "0m" // NOSONAR +#define RED ESC "31m" // NOSONAR +#define GREEN ESC "32m" // NOSONAR +#define YELLOW ESC "33m" // NOSONAR +#define BLUE ESC "34m" // NOSONAR +#define MAGENTA ESC "35m" // NOSONAR +#define CYAN ESC "36m" // NOSONAR namespace cpptrace { CPPTRACE_FORCE_NO_INLINE CPPTRACE_API @@ -40,7 +40,7 @@ namespace cpptrace { object_trace raw_trace::resolve_object_trace() const { try { return object_trace(detail::get_frames_object_info(frames)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -56,7 +56,7 @@ namespace cpptrace { frame.symbol = detail::demangle(frame.symbol); } return stacktrace(std::move(trace)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -88,7 +88,7 @@ namespace cpptrace { stacktrace object_trace::resolve() const { try { return stacktrace(detail::resolve_frames(frames)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -241,7 +241,7 @@ namespace cpptrace { raw_trace generate_raw_trace(std::uint_least32_t skip) { try { return raw_trace(detail::capture_frames(skip + 1, UINT_LEAST32_MAX)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -253,7 +253,7 @@ namespace cpptrace { raw_trace generate_raw_trace(std::uint_least32_t skip, std::uint_least32_t max_depth) { try { return raw_trace(detail::capture_frames(skip + 1, max_depth)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -265,7 +265,7 @@ namespace cpptrace { object_trace generate_object_trace(std::uint_least32_t skip) { try { return object_trace(detail::get_frames_object_info(detail::capture_frames(skip + 1, UINT_LEAST32_MAX))); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -277,7 +277,7 @@ namespace cpptrace { object_trace generate_object_trace(std::uint_least32_t skip, std::uint_least32_t max_depth) { try { return object_trace(detail::get_frames_object_info(detail::capture_frames(skip + 1, max_depth))); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } @@ -299,7 +299,7 @@ namespace cpptrace { frame.symbol = detail::demangle(frame.symbol); } return stacktrace(std::move(trace)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } diff --git a/src/symbols/symbols_with_addr2line.cpp b/src/symbols/symbols_with_addr2line.cpp index 145385a..3e6f945 100644 --- a/src/symbols/symbols_with_addr2line.cpp +++ b/src/symbols/symbols_with_addr2line.cpp @@ -301,7 +301,7 @@ namespace addr2line { for(size_t i = 0; i < output.size(); i++) { update_trace(output[i], i, entries_vec); } - } catch(...) { + } catch(...) { // NOSONAR if(!should_absorb_trace_exceptions()) { throw; } diff --git a/src/symbols/symbols_with_dbghelp.cpp b/src/symbols/symbols_with_dbghelp.cpp index 7e16f32..9c0604b 100644 --- a/src/symbols/symbols_with_dbghelp.cpp +++ b/src/symbols/symbols_with_dbghelp.cpp @@ -408,7 +408,7 @@ namespace dbghelp { for(const auto frame : frames) { try { trace.push_back(resolve_frame(proc, frame)); - } catch(...) { + } catch(...) { // NOSONAR if(!detail::should_absorb_trace_exceptions()) { throw; } diff --git a/src/symbols/symbols_with_libbacktrace.cpp b/src/symbols/symbols_with_libbacktrace.cpp index c57e4d7..df969b3 100644 --- a/src/symbols/symbols_with_libbacktrace.cpp +++ b/src/symbols/symbols_with_libbacktrace.cpp @@ -77,7 +77,7 @@ namespace libbacktrace { ); } return frame; - } catch(...) { + } catch(...) { // NOSONAR if(!should_absorb_trace_exceptions()) { throw; } diff --git a/src/symbols/symbols_with_libdwarf.cpp b/src/symbols/symbols_with_libdwarf.cpp index 314a402..99f9ebc 100644 --- a/src/symbols/symbols_with_libdwarf.cpp +++ b/src/symbols/symbols_with_libdwarf.cpp @@ -667,7 +667,7 @@ namespace libdwarf { const auto& dlframe = entry.first.get(); auto& frame = entry.second.get(); frame = resolver->resolve_frame(dlframe); - } catch(...) { + } catch(...) { // NOSONAR if(!should_absorb_trace_exceptions()) { throw; } @@ -678,7 +678,7 @@ namespace libdwarf { // .emplace needed, for some reason .insert tries to copy <= gcc 7.2 resolver_map.emplace(obj_name, std::move(resolver_object).unwrap()); } - } catch(...) { + } catch(...) { // NOSONAR if(!should_absorb_trace_exceptions()) { throw; } From 1b9e5916f6b7d1cf5cf8a7d3a55e9bcfeb1d0db7 Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:07:46 -0400 Subject: [PATCH 3/5] Some fixes --- .github/workflows/sonarlint.yml | 2 +- src/cpptrace.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/sonarlint.yml b/.github/workflows/sonarlint.yml index 15e7805..d22565d 100644 --- a/.github/workflows/sonarlint.yml +++ b/.github/workflows/sonarlint.yml @@ -21,7 +21,7 @@ jobs: run: | mkdir build cd build - cmake .. -DCMAKE_BUILD_TYPE=Debug -DCPPTRACE_BUILD_DEMO=On -DCPPTRACE_BUILD_TEST=On -DCMAKE_EXPORT_COMPILE_COMMANDS=On + cmake .. -DCMAKE_BUILD_TYPE=Debug -DCPPTRACE_BUILD_DEMO=On -DCPPTRACE_BUILD_TEST=On -DCMAKE_EXPORT_COMPILE_COMMANDS=On -DCMAKE_CXX_STANDARD=11 make -j cd .. - name: Run sonar-scanner diff --git a/src/cpptrace.cpp b/src/cpptrace.cpp index 124c4fa..bb3ca27 100644 --- a/src/cpptrace.cpp +++ b/src/cpptrace.cpp @@ -313,8 +313,8 @@ namespace cpptrace { } namespace detail { - std::atomic_bool absorb_trace_exceptions(true); - std::atomic cache_mode(cache_mode::prioritize_speed); + std::atomic_bool absorb_trace_exceptions(true); // NOSONAR + std::atomic cache_mode(cache_mode::prioritize_speed); // NOSONAR } CPPTRACE_API void absorb_trace_exceptions(bool absorb) { From af181bf981cd1bffe7b127a4d302a0578a3d330b Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:16:55 -0400 Subject: [PATCH 4/5] Disabled on sonar --- src/cpptrace.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cpptrace.cpp b/src/cpptrace.cpp index bb3ca27..51d2d1b 100644 --- a/src/cpptrace.cpp +++ b/src/cpptrace.cpp @@ -16,14 +16,14 @@ #include "platform/utils.hpp" #include "platform/object.hpp" -#define ESC "\033[" // NOSONAR -#define RESET ESC "0m" // NOSONAR -#define RED ESC "31m" // NOSONAR -#define GREEN ESC "32m" // NOSONAR -#define YELLOW ESC "33m" // NOSONAR -#define BLUE ESC "34m" // NOSONAR -#define MAGENTA ESC "35m" // NOSONAR -#define CYAN ESC "36m" // NOSONAR +#define ESC "\033[" +#define RESET ESC "0m" +#define RED ESC "31m" +#define GREEN ESC "32m" +#define YELLOW ESC "33m" +#define BLUE ESC "34m" +#define MAGENTA ESC "35m" +#define CYAN ESC "36m" namespace cpptrace { CPPTRACE_FORCE_NO_INLINE CPPTRACE_API From 53ceb99b4668ec6722b262694d1100c672430b8a Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:08:47 -0400 Subject: [PATCH 5/5] Sonar fixes: More nullptr literal use, take std::fuction by const&, and [[noreturn]] --- src/platform/common.hpp | 2 +- src/platform/dwarf.hpp | 30 +++++++++++++-------------- src/platform/error.hpp | 2 +- src/symbols/symbols_with_libdwarf.cpp | 10 ++++----- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/platform/common.hpp b/src/platform/common.hpp index 659ae25..818500a 100644 --- a/src/platform/common.hpp +++ b/src/platform/common.hpp @@ -51,7 +51,7 @@ namespace cpptrace { namespace detail { // Placed here instead of utils because it's used by error.hpp and utils.hpp template std::string stringf(T... args) { - int length = snprintf(0, 0, args...); + int length = snprintf(nullptr, 0, args...); if(length < 0) { throw std::logic_error("invalid arguments to stringf"); } diff --git a/src/platform/dwarf.hpp b/src/platform/dwarf.hpp index aaa128d..a4593f0 100644 --- a/src/platform/dwarf.hpp +++ b/src/platform/dwarf.hpp @@ -18,7 +18,7 @@ namespace libdwarf { static_assert(std::is_pointer::value, "Dwarf_Die not a pointer"); static_assert(std::is_pointer::value, "Dwarf_Debug not a pointer"); - void handle_dwarf_error(Dwarf_Debug dbg, Dwarf_Error error) { + [[noreturn]] void handle_dwarf_error(Dwarf_Debug dbg, Dwarf_Error error) { int ev = dwarf_errno(error); char* msg = dwarf_errmsg(error); dwarf_dealloc_error(dbg, error); @@ -46,7 +46,7 @@ namespace libdwarf { >::type = 0 > int wrap(int (*f)(Args...), Args2&&... args) const { - Dwarf_Error error = 0; + Dwarf_Error error = nullptr; int ret = f(std::forward(args)..., &error); if(ret == DW_DLV_ERROR) { handle_dwarf_error(dbg, error); @@ -85,7 +85,7 @@ namespace libdwarf { die_object clone() const { Dwarf_Off global_offset = get_global_offset(); Dwarf_Bool is_info = dwarf_get_die_infotypes_flag(die); - Dwarf_Die die_copy = 0; + Dwarf_Die die_copy = nullptr; VERIFY(wrap(dwarf_offdie_b, dbg, global_offset, is_info, &die_copy) == DW_DLV_OK); return {dbg, die_copy}; } @@ -96,19 +96,19 @@ namespace libdwarf { if(ret == DW_DLV_OK) { return die_object(dbg, child); } else if(ret == DW_DLV_NO_ENTRY) { - return die_object(dbg, 0); + return die_object(dbg, nullptr); } else { PANIC(); } } die_object get_sibling() const { - Dwarf_Die sibling = 0; + Dwarf_Die sibling = nullptr; int ret = wrap(dwarf_siblingof_b, dbg, die, true, &sibling); if(ret == DW_DLV_OK) { return die_object(dbg, sibling); } else if(ret == DW_DLV_NO_ENTRY) { - return die_object(dbg, 0); + return die_object(dbg, nullptr); } else { PANIC(); } @@ -193,7 +193,7 @@ namespace libdwarf { VERIFY(wrap(dwarf_formref, attr, &off, &is_info) == DW_DLV_OK); Dwarf_Off global_offset = 0; VERIFY(wrap(dwarf_convert_to_global_offset, attr, off, &global_offset) == DW_DLV_OK); - Dwarf_Die target = 0; + Dwarf_Die target = nullptr; VERIFY(wrap(dwarf_offdie_b, dbg, global_offset, is_info, &target) == DW_DLV_OK); return die_object(dbg, target); } @@ -202,7 +202,7 @@ namespace libdwarf { Dwarf_Off off; VERIFY(wrap(dwarf_global_formref, attr, &off) == DW_DLV_OK); int is_info = dwarf_get_die_infotypes_flag(die); - Dwarf_Die target = 0; + Dwarf_Die target = nullptr; VERIFY(wrap(dwarf_offdie_b, dbg, off, is_info, &target) == DW_DLV_OK); return die_object(dbg, target); } @@ -210,7 +210,7 @@ namespace libdwarf { { Dwarf_Sig8 signature; VERIFY(wrap(dwarf_formsig8, attr, &signature) == DW_DLV_OK); - Dwarf_Die target = 0; + Dwarf_Die target = nullptr; Dwarf_Bool targ_is_info = false; VERIFY(wrap(dwarf_find_die_given_sig8, dbg, &signature, &target, &targ_is_info) == DW_DLV_OK); return die_object(dbg, target); @@ -234,7 +234,7 @@ namespace libdwarf { template void dwarf5_ranges(F callback) const { - Dwarf_Attribute attr = 0; + Dwarf_Attribute attr = nullptr; if(wrap(dwarf_attr, die, DW_AT_ranges, &attr) != DW_DLV_OK) { return; } @@ -243,7 +243,7 @@ namespace libdwarf { Dwarf_Half form = 0; VERIFY(wrap(dwarf_whatform, attr, &form) == DW_DLV_OK); // get .debug_rnglists info - Dwarf_Rnglists_Head head = 0; + Dwarf_Rnglists_Head head = nullptr; Dwarf_Unsigned rnglists_entries = 0; Dwarf_Unsigned dw_global_offset_of_rle_set = 0; int res = wrap( @@ -312,7 +312,7 @@ namespace libdwarf { template void dwarf4_ranges(Dwarf_Addr lowpc, F callback) const { - Dwarf_Attribute attr = 0; + Dwarf_Attribute attr = nullptr; if(wrap(dwarf_attr, die, DW_AT_ranges, &attr) != DW_DLV_OK) { return; } @@ -325,7 +325,7 @@ namespace libdwarf { if(lowpc != std::numeric_limits::max()) { baseaddr = lowpc; } - Dwarf_Ranges* ranges = 0; + Dwarf_Ranges* ranges = nullptr; Dwarf_Signed count = 0; VERIFY( wrap( @@ -430,7 +430,7 @@ namespace libdwarf { // returns true if traversal should continue bool walk_die_list( const die_object& die, - std::function fn + const std::function& fn ) { // TODO: Refactor so there is only one fn call bool continue_traversal = true; @@ -453,7 +453,7 @@ namespace libdwarf { // returns true if traversal should continue bool walk_die_list_recursive( const die_object& die, - std::function fn + const std::function& fn ) { return walk_die_list( die, diff --git a/src/platform/error.hpp b/src/platform/error.hpp index 6bec118..978c240 100644 --- a/src/platform/error.hpp +++ b/src/platform/error.hpp @@ -47,7 +47,7 @@ namespace detail { constexpr const char* assert_actions[] = {"assertion", "verification", "panic"}; constexpr const char* assert_names[] = {"ASSERT", "VERIFY", "PANIC"}; - inline void assert_fail( + [[noreturn]] inline void assert_fail( assert_type type, const char* expression, const char* signature, diff --git a/src/symbols/symbols_with_libdwarf.cpp b/src/symbols/symbols_with_libdwarf.cpp index 99f9ebc..8e6bcd2 100644 --- a/src/symbols/symbols_with_libdwarf.cpp +++ b/src/symbols/symbols_with_libdwarf.cpp @@ -95,7 +95,7 @@ namespace libdwarf { >::type = 0 > int wrap(int (*f)(Args...), Args2&&... args) const { - Dwarf_Error error = 0; + Dwarf_Error error = nullptr; int ret = f(std::forward(args)..., &error); if(ret == DW_DLV_ERROR) { handle_dwarf_error(dbg, error); @@ -211,7 +211,7 @@ namespace libdwarf { // walk all CU's in a dbg, callback is called on each die and should return true to // continue traversal - void walk_compilation_units(std::function fn) { + void walk_compilation_units(const std::function& fn) { // libdwarf keeps track of where it is in the file, dwarf_next_cu_header_d is statefull Dwarf_Unsigned next_cu_header; Dwarf_Half header_cu_type; @@ -467,7 +467,7 @@ namespace libdwarf { VERIFY(ret == DW_DLV_OK); line_contexts.insert({off, {version, line_context}}); } - Dwarf_Line* line_buffer = 0; + Dwarf_Line* line_buffer = nullptr; Dwarf_Signed line_count = 0; Dwarf_Line* linebuf_actuals = nullptr; Dwarf_Signed linecount_actuals = 0; @@ -482,7 +482,7 @@ namespace libdwarf { ) == DW_DLV_OK ); Dwarf_Addr last_lineaddr = 0; - Dwarf_Line last_line = 0; + Dwarf_Line last_line = nullptr; for(int i = 0; i < line_count; i++) { Dwarf_Line line = line_buffer[i]; Dwarf_Addr lineaddr = 0; @@ -519,7 +519,7 @@ namespace libdwarf { VERIFY(wrap(dwarf_lineendsequence, line, &is_line_end) == DW_DLV_OK); if(is_line_end) { last_lineaddr = 0; - last_line = 0; + last_line = nullptr; } else { last_lineaddr = lineaddr; last_line = line;