From ce5c34f4dad99e2374c510d57f43faa6e4040f41 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 31 Jul 2023 18:10:49 -0700 Subject: [PATCH 1/3] Fix stacktrace's usage of snprintf. --- stl/src/stacktrace.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stl/src/stacktrace.cpp b/stl/src/stacktrace.cpp index 7dfae0d0b5..cb0557a4fe 100644 --- a/stl/src/stacktrace.cpp +++ b/stl/src/stacktrace.cpp @@ -148,7 +148,7 @@ namespace { } if (displacement != 0) { - constexpr size_t max_disp_num = sizeof("+0x1122334455667788") - 1; // maximum possible offset + constexpr size_t max_disp_num = sizeof("+0x1122334455667788"); // maximum possible offset off = string_fill(fill, off + max_disp_num, str, [displacement, off](char* s, size_t) { const int ret = std::snprintf(s + off, max_disp_num, "+0x%llX", displacement); @@ -214,7 +214,7 @@ namespace { off = source_file(address, str, off, &line, fill); if (line != 0) { - constexpr size_t max_line_num = sizeof("(4294967295): ") - 1; // maximum possible line number + constexpr size_t max_line_num = sizeof("(4294967295): "); // maximum possible line number off = string_fill(fill, off + max_line_num, str, [line, off](char* s, size_t) { const int ret = std::snprintf(s + off, max_line_num, "(%u): ", line); @@ -317,7 +317,7 @@ void __stdcall __std_stacktrace_to_string(const void* const* const _Addresses, c }); } - constexpr size_t max_entry_num = sizeof("65536> ") - 1; // maximum possible entry number + constexpr size_t max_entry_num = sizeof("65536> "); // maximum possible entry number off = string_fill(_Fill, off + max_entry_num, _Str, [off, i](char* s, size_t) { const int ret = std::snprintf(s + off, max_entry_num, "%u> ", static_cast(i)); From f8b8ee79b7bbfb356d0fc55f033db3502e3cf940 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 1 Aug 2023 17:07:55 -0700 Subject: [PATCH 2/3] Revert "Fix stacktrace's usage of snprintf." This reverts commit ce5c34f4dad99e2374c510d57f43faa6e4040f41. --- stl/src/stacktrace.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stl/src/stacktrace.cpp b/stl/src/stacktrace.cpp index cb0557a4fe..7dfae0d0b5 100644 --- a/stl/src/stacktrace.cpp +++ b/stl/src/stacktrace.cpp @@ -148,7 +148,7 @@ namespace { } if (displacement != 0) { - constexpr size_t max_disp_num = sizeof("+0x1122334455667788"); // maximum possible offset + constexpr size_t max_disp_num = sizeof("+0x1122334455667788") - 1; // maximum possible offset off = string_fill(fill, off + max_disp_num, str, [displacement, off](char* s, size_t) { const int ret = std::snprintf(s + off, max_disp_num, "+0x%llX", displacement); @@ -214,7 +214,7 @@ namespace { off = source_file(address, str, off, &line, fill); if (line != 0) { - constexpr size_t max_line_num = sizeof("(4294967295): "); // maximum possible line number + constexpr size_t max_line_num = sizeof("(4294967295): ") - 1; // maximum possible line number off = string_fill(fill, off + max_line_num, str, [line, off](char* s, size_t) { const int ret = std::snprintf(s + off, max_line_num, "(%u): ", line); @@ -317,7 +317,7 @@ void __stdcall __std_stacktrace_to_string(const void* const* const _Addresses, c }); } - constexpr size_t max_entry_num = sizeof("65536> "); // maximum possible entry number + constexpr size_t max_entry_num = sizeof("65536> ") - 1; // maximum possible entry number off = string_fill(_Fill, off + max_entry_num, _Str, [off, i](char* s, size_t) { const int ret = std::snprintf(s + off, max_entry_num, "%u> ", static_cast(i)); From ee57862335caa4d4fa472c891f31a0023cb4e0f1 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 1 Aug 2023 17:11:53 -0700 Subject: [PATCH 3/3] Properly handle `snprintf` and `resize_and_overwrite`. --- stl/src/stacktrace.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stl/src/stacktrace.cpp b/stl/src/stacktrace.cpp index 7dfae0d0b5..67f6dcf01e 100644 --- a/stl/src/stacktrace.cpp +++ b/stl/src/stacktrace.cpp @@ -151,7 +151,7 @@ namespace { constexpr size_t max_disp_num = sizeof("+0x1122334455667788") - 1; // maximum possible offset off = string_fill(fill, off + max_disp_num, str, [displacement, off](char* s, size_t) { - const int ret = std::snprintf(s + off, max_disp_num, "+0x%llX", displacement); + const int ret = std::snprintf(s + off, max_disp_num + 1, "+0x%llX", displacement); if (ret <= 0) { std::abort(); // formatting error } @@ -217,7 +217,7 @@ namespace { constexpr size_t max_line_num = sizeof("(4294967295): ") - 1; // maximum possible line number off = string_fill(fill, off + max_line_num, str, [line, off](char* s, size_t) { - const int ret = std::snprintf(s + off, max_line_num, "(%u): ", line); + const int ret = std::snprintf(s + off, max_line_num + 1, "(%u): ", line); if (ret <= 0) { std::abort(); // formatting error } @@ -320,7 +320,7 @@ void __stdcall __std_stacktrace_to_string(const void* const* const _Addresses, c constexpr size_t max_entry_num = sizeof("65536> ") - 1; // maximum possible entry number off = string_fill(_Fill, off + max_entry_num, _Str, [off, i](char* s, size_t) { - const int ret = std::snprintf(s + off, max_entry_num, "%u> ", static_cast(i)); + const int ret = std::snprintf(s + off, max_entry_num + 1, "%u> ", static_cast(i)); if (ret <= 0) { std::abort(); // formatting error }