This fixes some more ASan errors in the testsuite. The first one is a
simple leak. The second one is mroe complicated, the test created
loads of variables to hold either a strup'd string or "", in order to
restore the original environment. Since the test exits after that
function, we don't care about restoring the environment, so can get
rid of most of the variables.

        * testsuite/20_util/specialized_algorithms/memory_management_tools/
        1.cc: Free memory.
        * testsuite/22_locale/locale/cons/5.cc: Remove redundant restoration
        of original environment and free memory.

Tested powerpc64le-linux, committed to trunk.


commit 5d1ce22784966d603d1fb62e641b8f7d30e281d2
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu Jul 6 13:23:58 2017 +0100

    Fix memory leaks in libstdc++ tests
    
        * testsuite/20_util/specialized_algorithms/memory_management_tools/
        1.cc: Free memory.
        * testsuite/22_locale/locale/cons/5.cc: Remove redundant restoration
        of original environment and free memory.

diff --git 
a/libstdc++-v3/testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc
 
b/libstdc++-v3/testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc
index d80c125..39d3e76 100644
--- 
a/libstdc++-v3/testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc
+++ 
b/libstdc++-v3/testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc
@@ -180,6 +180,7 @@ void test12()
     std::uninitialized_default_construct(target, target+10);
   } catch (...) {
   }
+  free(target);
   VERIFY(ctor_count == 5);
   VERIFY(del_count == 5);
   throw_after = 0;
@@ -198,6 +199,7 @@ void test13()
     std::uninitialized_value_construct(target, target+10);
   } catch (...) {
   }
+  free(target);
   VERIFY(ctor_count == 5);
   VERIFY(del_count == 5);
   throw_after = 0;
@@ -216,6 +218,7 @@ void test14()
     std::uninitialized_default_construct_n(target, 10);
   } catch (...) {
   }
+  free(target);
   VERIFY(ctor_count == 5);
   VERIFY(del_count == 5);
   throw_after = 0;
@@ -234,6 +237,7 @@ void test15()
     std::uninitialized_value_construct_n(target, 10);
   } catch (...) {
   }
+  free(target);
   VERIFY(ctor_count == 5);
   VERIFY(del_count == 5);
   throw_after = 0;
@@ -254,6 +258,7 @@ void test16()
     std::uninitialized_move(source.begin(), source.end(), target);
   } catch (...) {
   }
+  free(target);
   VERIFY(ctor_count == 5);
   VERIFY(del_count == 5);
   throw_after = 0;
@@ -273,6 +278,7 @@ void test17()
     std::uninitialized_move_n(source.begin(), 10, target);
   } catch (...) {
   }
+  free(target);
   VERIFY(ctor_count == 5);
   VERIFY(del_count == 5);
   throw_after = 0;
diff --git a/libstdc++-v3/testsuite/22_locale/locale/cons/5.cc 
b/libstdc++-v3/testsuite/22_locale/locale/cons/5.cc
index 4c23c7f..c0a18ce 100644
--- a/libstdc++-v3/testsuite/22_locale/locale/cons/5.cc
+++ b/libstdc++-v3/testsuite/22_locale/locale/cons/5.cc
@@ -37,34 +37,7 @@ void test04()
 
 #ifdef _GLIBCXX_HAVE_SETENV
 
-  const char* LANG_orig = getenv("LANG") ? strdup(getenv("LANG")) : "";
-  const char* LC_ALL_orig = getenv("LC_ALL") ? strdup(getenv("LC_ALL")) : "";
-  const char* LC_CTYPE_orig = 
-    getenv("LC_CTYPE") ? strdup(getenv("LC_CTYPE")) : "";
-  const char* LC_NUMERIC_orig = 
-    getenv("LC_NUMERIC") ? strdup(getenv("LC_NUMERIC")) : "";
-  const char* LC_TIME_orig = 
-    getenv("LC_TIME") ? strdup(getenv("LC_TIME")) : "";
-  const char* LC_COLLATE_orig =
-    getenv("LC_COLLATE") ? strdup(getenv("LC_COLLATE")) : "";
-  const char* LC_MONETARY_orig = 
-    getenv("LC_MONETARY") ? strdup(getenv("LC_MONETARY")) : "";
-  const char* LC_MESSAGES_orig = 
-    getenv("LC_MESSAGES") ? strdup(getenv("LC_MESSAGES")) : "";
-#if _GLIBCXX_NUM_CATEGORIES
-  const char* LC_PAPER_orig = 
-    getenv("LC_PAPER") ? strdup(getenv("LC_PAPER")) : "";
-  const char* LC_NAME_orig = 
-    getenv("LC_NAME") ? strdup(getenv("LC_NAME")) : "";
-  const char* LC_ADDRESS_orig = 
-    getenv("LC_ADDRESS") ? strdup(getenv("LC_ADDRESS")) : "";
-  const char* LC_TELEPHONE_orig = 
-    getenv("LC_TELEPHONE") ? strdup(getenv("LC_TELEPHONE")) : "";
-  const char* LC_MEASUREMENT_orig = 
-    getenv("LC_MEASUREMENT") ? strdup(getenv("LC_MEASUREMENT")) : "";
-  const char* LC_IDENTIFICATION_orig =
-    getenv("LC_IDENTIFICATION") ? strdup(getenv("LC_IDENTIFICATION")) : "";
-#endif
+  char* LANG_orig = strdup(getenv("LANG") ? getenv("LANG") : "");
 
   // Check that a "POSIX" LC_ALL is equivalent to "C".
   if (!setenv("LC_ALL", "POSIX", 1))
@@ -91,12 +64,11 @@ void test04()
          VERIFY( loc.name() == "en_PH" );
        }
       setenv("LC_ALL", "", 1);
-      setenv("LANG", LANG_orig ? LANG_orig : "", 1);
-      setenv("LC_COLLATE", LC_COLLATE_orig ? LC_COLLATE_orig : "", 1);
+      setenv("LANG", LANG_orig, 1);
     }
 
   // NB: LANG checks all LC_* macro settings. As such, all LC_* macros
-  // must be cleared for these tests, and then restored.
+  // must be cleared for these tests.
   setenv("LC_ALL", "", 1);
   setenv("LC_CTYPE", "", 1);
   setenv("LC_NUMERIC", "", 1);
@@ -173,26 +145,9 @@ void test04()
     }
 #endif
 
-  // Restore the environment.
-  setenv("LANG", LANG_orig ? LANG_orig : "", 1);
-  setenv("LC_ALL", LC_ALL_orig ? LC_ALL_orig : "", 1);
-  setenv("LC_CTYPE", LC_CTYPE_orig ? LC_CTYPE_orig : "", 1);
-  setenv("LC_NUMERIC", LC_NUMERIC_orig ? LC_NUMERIC_orig : "", 1);
-  setenv("LC_TIME", LC_TIME_orig ? LC_TIME_orig : "", 1);
-  setenv("LC_COLLATE", LC_COLLATE_orig ? LC_COLLATE_orig : "", 1);
-  setenv("LC_MONETARY", LC_MONETARY_orig ? LC_MONETARY_orig : "", 1);
-  setenv("LC_MESSAGES", LC_MESSAGES_orig ? LC_MESSAGES_orig : "", 1);
-#if _GLIBCXX_NUM_CATEGORIES
-  setenv("LC_PAPER", LC_PAPER_orig ? LC_PAPER_orig : "", 1);
-  setenv("LC_NAME", LC_NAME_orig ? LC_NAME_orig : "", 1);
-  setenv("LC_ADDRESS", LC_ADDRESS_orig ? LC_ADDRESS_orig : "", 1);
-  setenv("LC_TELEPHONE", LC_TELEPHONE_orig ? LC_TELEPHONE_orig : "", 1);
-  setenv("LC_MEASUREMENT", LC_MEASUREMENT_orig ? LC_MEASUREMENT_orig : "", 1);
-  setenv("LC_IDENTIFICATION", 
-         LC_IDENTIFICATION_orig ? LC_IDENTIFICATION_orig : "", 1);
-#endif
+  free(LANG_orig);
 
-#endif
+#endif // _GLIBCXX_HAVE_SETENV
 }
 
 int main()

Reply via email to