From b80f05efaafdb8b4f0ef6afc92bfb31762904e02 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 17 Nov 2016 22:00:00 +0100 Subject: [PATCH] test: fix memory leaks in malloc cctests Make cctest valgrind-clean again by freeing heap-allocated memory. Overlooked in commit ea94086 ("src: provide allocation + nullptr check shortcuts.") PR-URL: https://github.com/nodejs/node/pull/9667 Refs: https://github.com/nodejs/node/pull/8482 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Santiago Gimeno --- test/cctest/util.cc | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/test/cctest/util.cc b/test/cctest/util.cc index 434eeba96f..f99a46cdf5 100644 --- a/test/cctest/util.cc +++ b/test/cctest/util.cc @@ -94,34 +94,41 @@ namespace node { void LowMemoryNotification() {} } +#define TEST_AND_FREE(expression) \ + do { \ + auto pointer = expression; \ + EXPECT_NE(nullptr, pointer); \ + free(pointer); \ + } while (0) + TEST(UtilTest, Malloc) { using node::Malloc; - EXPECT_NE(nullptr, Malloc(0)); - EXPECT_NE(nullptr, Malloc(1)); - EXPECT_NE(nullptr, Malloc(0)); - EXPECT_NE(nullptr, Malloc(1)); + TEST_AND_FREE(Malloc(0)); + TEST_AND_FREE(Malloc(1)); + TEST_AND_FREE(Malloc(0)); + TEST_AND_FREE(Malloc(1)); } TEST(UtilTest, Calloc) { using node::Calloc; - EXPECT_NE(nullptr, Calloc(0)); - EXPECT_NE(nullptr, Calloc(1)); - EXPECT_NE(nullptr, Calloc(0)); - EXPECT_NE(nullptr, Calloc(1)); + TEST_AND_FREE(Calloc(0)); + TEST_AND_FREE(Calloc(1)); + TEST_AND_FREE(Calloc(0)); + TEST_AND_FREE(Calloc(1)); } TEST(UtilTest, UncheckedMalloc) { using node::UncheckedMalloc; - EXPECT_NE(nullptr, UncheckedMalloc(0)); - EXPECT_NE(nullptr, UncheckedMalloc(1)); - EXPECT_NE(nullptr, UncheckedMalloc(0)); - EXPECT_NE(nullptr, UncheckedMalloc(1)); + TEST_AND_FREE(UncheckedMalloc(0)); + TEST_AND_FREE(UncheckedMalloc(1)); + TEST_AND_FREE(UncheckedMalloc(0)); + TEST_AND_FREE(UncheckedMalloc(1)); } TEST(UtilTest, UncheckedCalloc) { using node::UncheckedCalloc; - EXPECT_NE(nullptr, UncheckedCalloc(0)); - EXPECT_NE(nullptr, UncheckedCalloc(1)); - EXPECT_NE(nullptr, UncheckedCalloc(0)); - EXPECT_NE(nullptr, UncheckedCalloc(1)); + TEST_AND_FREE(UncheckedCalloc(0)); + TEST_AND_FREE(UncheckedCalloc(1)); + TEST_AND_FREE(UncheckedCalloc(0)); + TEST_AND_FREE(UncheckedCalloc(1)); }