#include <string.h>
#include <folly/Malloc.h>
+#include <folly/portability/Config.h>
#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
# include <cxxabi.h>
#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
fbstring demangle(const char* name) {
+#ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
+ // GCC's __cxa_demangle() uses on-stack data structures for the
+ // parser state which are linear in the number of components of the
+ // symbol. For extremely long symbols, this can cause a stack
+ // overflow. We set an arbitrary symbol length limit above which we
+ // just return the mangled name.
+ size_t mangledLen = strlen(name);
+ if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
+ return fbstring(name, mangledLen);
+ }
+#endif
+
int status;
size_t len = 0;
// malloc() memory for the demangled type name
} // namespace
size_t demangle(const char* name, char* out, size_t outSize) {
+#ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
+ size_t mangledLen = strlen(name);
+ if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
+ if (outSize) {
+ size_t n = std::min(mangledLen, outSize - 1);
+ memcpy(out, name, n);
+ out[n] = '\0';
+ }
+ return mangledLen;
+ }
+#endif
+
DemangleBuf dbuf;
dbuf.dest = out;
dbuf.remaining = outSize ? outSize - 1 : 0; // leave room for null term
EXPECT_STREQ("folly_test", buf);
}
}
-#endif
+
+#if defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
+namespace {
+
+template <int I, class T1, class T2>
+struct Node {};
+
+template <int N, int I = 1>
+struct LongSymbol {
+ using arg1 = typename LongSymbol<N / 2, 2 * I>::type;
+ using arg2 = typename LongSymbol<N / 2, 2 * I + 1>::type;
+ using type = Node<I, arg1, arg2>;
+};
+
+template <int I>
+struct LongSymbol<0, I> {
+ using type = void;
+};
+
+} // namespace
+
+TEST(Demangle, LongSymbolFallback) {
+ // The symbol must be at least FOLLY_DEMANGLE_MAX_SYMBOL_SIZE long.
+ using Symbol = LongSymbol<FOLLY_DEMANGLE_MAX_SYMBOL_SIZE>::type;
+ auto name = typeid(Symbol).name();
+
+ EXPECT_STREQ(name, demangle(name).c_str());
+
+ char buf[16];
+ char expected[16];
+ folly::demangle(name, buf, 16);
+ folly::strlcpy(expected, name, 16);
+ EXPECT_STREQ(expected, buf);
+}
+#endif // defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
+
+#endif // FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
TEST(Demangle, strlcpy) {
char buf[6];