From: Jonathan Wakely Date: Thu, 6 Jul 2017 12:42:46 +0000 (+0100) Subject: Fix memory leaks in libstdc++ ABI tests X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=4f7c2c7ff2374cb39386f627fa97e2363dd2f94d;p=gcc.git Fix memory leaks in libstdc++ ABI tests * testsuite/abi/pr42230.cc: Free memory. * testsuite/util/testsuite_abi.cc (demangle): Return std::string instead of pointer that might need freeing. * testsuite/util/testsuite_abi.h (demangle): Likewise. * testsuite/util/testsuite_hooks.cc (verify_demangle): Free memory. From-SVN: r250020 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1c50a3b2b4c..be9813a08b1 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,11 @@ 2017-07-06 Jonathan Wakely + * testsuite/abi/pr42230.cc: Free memory. + * testsuite/util/testsuite_abi.cc (demangle): Return std::string + instead of pointer that might need freeing. + * testsuite/util/testsuite_abi.h (demangle): Likewise. + * testsuite/util/testsuite_hooks.cc (verify_demangle): Free memory. + * include/bits/uses_allocator.h (__use_alloc(const _Alloc&&)): Add deleted overload to prevent dangling references to rvalues. * include/experimental/memory_resource diff --git a/libstdc++-v3/testsuite/abi/pr42230.cc b/libstdc++-v3/testsuite/abi/pr42230.cc index 2a338995336..3b5a1f6c242 100644 --- a/libstdc++-v3/testsuite/abi/pr42230.cc +++ b/libstdc++-v3/testsuite/abi/pr42230.cc @@ -12,5 +12,6 @@ int main() char* ret = abi::__cxa_demangle("e", 0, &length, &cc); assert( (cc < 0 && !ret) || (ret && length) ); + std::free(ret); return 0; } diff --git a/libstdc++-v3/testsuite/util/testsuite_abi.cc b/libstdc++-v3/testsuite/util/testsuite_abi.cc index 4d7f4ca3a65..d18429a55b2 100644 --- a/libstdc++-v3/testsuite/util/testsuite_abi.cc +++ b/libstdc++-v3/testsuite/util/testsuite_abi.cc @@ -590,21 +590,26 @@ create_symbols(const char* file) } -const char* +std::string demangle(const std::string& mangled) { - const char* name; + std::string name; if (mangled[0] != '_' || mangled[1] != 'Z') { // This is not a mangled symbol, thus has "C" linkage. - name = mangled.c_str(); + name = mangled; } else { // Use __cxa_demangle to demangle. int status = 0; - name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status); - if (!name) + char* ptr = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status); + if (ptr) + { + name = ptr; + free(ptr); + } + else { switch (status) { diff --git a/libstdc++-v3/testsuite/util/testsuite_abi.h b/libstdc++-v3/testsuite/util/testsuite_abi.h index 8275b23e162..77c5656177f 100644 --- a/libstdc++-v3/testsuite/util/testsuite_abi.h +++ b/libstdc++-v3/testsuite/util/testsuite_abi.h @@ -94,5 +94,5 @@ compare_symbols(const char* baseline_file, const char* test_file, bool verb); symbols create_symbols(const char* file); -const char* +std::string demangle(const std::string& mangled); diff --git a/libstdc++-v3/testsuite/util/testsuite_hooks.cc b/libstdc++-v3/testsuite/util/testsuite_hooks.cc index d1063e3d8d7..74e755dfe28 100644 --- a/libstdc++-v3/testsuite/util/testsuite_hooks.cc +++ b/libstdc++-v3/testsuite/util/testsuite_hooks.cc @@ -131,8 +131,11 @@ namespace __gnu_test verify_demangle(const char* mangled, const char* wanted) { int status = 0; - const char* s = abi::__cxa_demangle(mangled, 0, 0, &status); - if (!s) + const char* s = 0; + char* demangled = abi::__cxa_demangle(mangled, 0, 0, &status); + if (demangled) + s = demangled; + else { switch (status) { @@ -156,6 +159,7 @@ namespace __gnu_test std::string w(wanted); if (w != s) std::__throw_runtime_error(s); + free(demangled); } void