Skip to content

Commit

Permalink
Fix incorrect use of abi::__cxa_demangle
Browse files Browse the repository at this point in the history
The buffer, if passed, should be allocated with malloc, not statically. In this
case we don't need to allocate one first, we can use the default behaviour
where abi::__cxa_demangle allocates one for us.
  • Loading branch information
qris committed Feb 18, 2018
1 parent 0569184 commit 6e17b14
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions lib/common/Utils.cpp
Expand Up @@ -161,7 +161,6 @@ const Log::Category BACKTRACE("Backtrace");
static std::string demangle(const std::string& mangled_name)
{
std::string demangled_name = mangled_name;
char buffer[1024];

#if defined WIN32
if(UnDecorateSymbolName(mangled_name.c_str(), buffer, sizeof(buffer),
Expand All @@ -175,41 +174,34 @@ static std::string demangle(const std::string& mangled_name)
}
#elif defined HAVE_CXXABI_H
int status;
size_t length = sizeof(buffer);

char* result = abi::__cxa_demangle(mangled_name.c_str(),
buffer, &length, &status);
char* result = abi::__cxa_demangle(mangled_name.c_str(), NULL, NULL, &status);
if(result != NULL)
{
demangled_name = result;
free(result);
}

if (status == 0)
{
demangled_name = result;
return demangled_name;
}
else if (status == -1)
{
BOX_WARNING("Demangle failed with "
"memory allocation error: " <<
mangled_name);
BOX_WARNING("Demangle failed with memory allocation error: " << mangled_name);
}
else if (status == -2)
{
// Probably non-C++ name, don't demangle
/*
BOX_WARNING("Demangle failed with "
"with invalid name: " <<
mangled_name);
*/
}
else if (status == -3)
{
BOX_WARNING("Demangle failed with "
"with invalid argument: " <<
mangled_name);
BOX_WARNING("Demangle failed with with invalid argument: " << mangled_name);
}
else
{
BOX_WARNING("Demangle failed with "
"with unknown error " << status <<
": " << mangled_name);
BOX_WARNING("Demangle failed with with unknown error " << status << ": " <<
mangled_name);
}
#endif // HAVE_CXXABI_H

Expand Down

0 comments on commit 6e17b14

Please sign in to comment.