Check that names passed to cpptrace::detail::demangle look like mangled names before passing to abi::__cxa_demangle

This commit is contained in:
Jeremy Rifkin 2025-01-27 23:48:13 -06:00
parent a28cc3a3a0
commit c5c785db89
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
3 changed files with 23 additions and 0 deletions

View File

@ -2,6 +2,8 @@
#include "demangle/demangle.hpp"
#include "utils/utils.hpp"
#include <cxxabi.h>
#include <cstdlib>
@ -11,6 +13,11 @@ namespace cpptrace {
namespace detail {
std::string demangle(const std::string& name) {
int status;
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#demangler
// check both _Z and __Z, apple prefixes all symbols with an underscore
if(!(starts_with(name, "_Z") || starts_with(name, "__Z"))) {
return name;
}
// presumably thread-safe
// it appears safe to pass nullptr for status however the docs don't explicitly say it's safe so I don't
// want to rely on it

View File

@ -83,6 +83,10 @@ namespace detail {
return str.substr(left, right - left);
}
inline bool starts_with(const std::string& str, const std::string& prefix) {
return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
}
inline bool is_little_endian() {
std::uint16_t num = 0x1;
const auto* ptr = (std::uint8_t*)&num;

View File

@ -9,6 +9,7 @@ using testing::ElementsAre;
using cpptrace::detail::split;
using cpptrace::detail::join;
using cpptrace::detail::trim;
using cpptrace::detail::starts_with;
namespace {
@ -77,4 +78,15 @@ TEST(TrimTest, Basic) {
EXPECT_EQ(trim("\t test\n "), "test");
}
TEST(StartsWith, Basic) {
EXPECT_TRUE(starts_with("", ""));
EXPECT_TRUE(starts_with("abc", ""));
EXPECT_FALSE(starts_with("", "abc"));
EXPECT_FALSE(starts_with("ab", "abc"));
EXPECT_TRUE(starts_with("test", "test"));
EXPECT_TRUE(starts_with("hello_world", "hello"));
EXPECT_FALSE(starts_with("hello_world", "world"));
EXPECT_FALSE(starts_with("abcd", "abce"));
}
}