Check that names passed to cpptrace::detail::demangle look like mangled names before passing to abi::__cxa_demangle
This commit is contained in:
parent
a28cc3a3a0
commit
c5c785db89
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "demangle/demangle.hpp"
|
#include "demangle/demangle.hpp"
|
||||||
|
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
|
||||||
#include <cxxabi.h>
|
#include <cxxabi.h>
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@ -11,6 +13,11 @@ namespace cpptrace {
|
|||||||
namespace detail {
|
namespace detail {
|
||||||
std::string demangle(const std::string& name) {
|
std::string demangle(const std::string& name) {
|
||||||
int status;
|
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
|
// 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
|
// 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
|
// want to rely on it
|
||||||
|
|||||||
@ -83,6 +83,10 @@ namespace detail {
|
|||||||
return str.substr(left, right - left);
|
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() {
|
inline bool is_little_endian() {
|
||||||
std::uint16_t num = 0x1;
|
std::uint16_t num = 0x1;
|
||||||
const auto* ptr = (std::uint8_t*)#
|
const auto* ptr = (std::uint8_t*)#
|
||||||
|
|||||||
@ -9,6 +9,7 @@ using testing::ElementsAre;
|
|||||||
using cpptrace::detail::split;
|
using cpptrace::detail::split;
|
||||||
using cpptrace::detail::join;
|
using cpptrace::detail::join;
|
||||||
using cpptrace::detail::trim;
|
using cpptrace::detail::trim;
|
||||||
|
using cpptrace::detail::starts_with;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -77,4 +78,15 @@ TEST(TrimTest, Basic) {
|
|||||||
EXPECT_EQ(trim("\t test\n "), "test");
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user