Skip to content

Commit 3791b47

Browse files
committed
[Backtracing][Linux] Add a comment about __cxa_demangle.
`__cxa_demangle()` is a rather unusual API; one of its "features" is that the pointer you pass in must either be `nullptr`, in which case it will call `malloc()` itself, _or_ it has to be a pointer to a block of memory allocated with `malloc()`, because `__cxa_demangle()` may `realloc()` it for you. This seems to me to be something of a non-obvious footgun, so we never pass the caller's pointer through to `__cxa_demangle()`, which lets them decide how they want to allocate space. rdar://110261712
1 parent 1d8fe3b commit 3791b47

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

stdlib/public/runtime/Backtrace.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,10 @@ _swift_backtrace_demangle(const char *mangledName,
886886
return outputBuffer;
887887
#ifndef _WIN32
888888
} else if (name.startswith("_Z")) {
889-
// Try C++
889+
// Try C++; note that we don't want to force callers to use malloc() to
890+
// allocate their buffer, which is a requirement for __cxa_demangle
891+
// because it may call realloc() on the incoming pointer. As a result,
892+
// we never pass the caller's buffer to __cxa_demangle.
890893
size_t resultLen;
891894
int status = 0;
892895
char *result = abi::__cxa_demangle(mangledName, nullptr, &resultLen, &status);

0 commit comments

Comments
 (0)