Skip to content

Commit

Permalink
test: fix memory leaks in malloc cctests
Browse files Browse the repository at this point in the history
Make cctest valgrind-clean again by freeing heap-allocated memory.
Overlooked in commit ea94086 ("src: provide allocation + nullptr check
shortcuts.")

PR-URL: #9667
Refs: #8482
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
bnoordhuis authored and addaleax committed Nov 22, 2016
1 parent 425a864 commit a29be52
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions test/cctest/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(0));
EXPECT_NE(nullptr, Malloc<char>(1));
EXPECT_NE(nullptr, Malloc(0));
EXPECT_NE(nullptr, Malloc(1));
TEST_AND_FREE(Malloc<char>(0));
TEST_AND_FREE(Malloc<char>(1));
TEST_AND_FREE(Malloc(0));
TEST_AND_FREE(Malloc(1));
}

TEST(UtilTest, Calloc) {
using node::Calloc;
EXPECT_NE(nullptr, Calloc<char>(0));
EXPECT_NE(nullptr, Calloc<char>(1));
EXPECT_NE(nullptr, Calloc(0));
EXPECT_NE(nullptr, Calloc(1));
TEST_AND_FREE(Calloc<char>(0));
TEST_AND_FREE(Calloc<char>(1));
TEST_AND_FREE(Calloc(0));
TEST_AND_FREE(Calloc(1));
}

TEST(UtilTest, UncheckedMalloc) {
using node::UncheckedMalloc;
EXPECT_NE(nullptr, UncheckedMalloc<char>(0));
EXPECT_NE(nullptr, UncheckedMalloc<char>(1));
EXPECT_NE(nullptr, UncheckedMalloc(0));
EXPECT_NE(nullptr, UncheckedMalloc(1));
TEST_AND_FREE(UncheckedMalloc<char>(0));
TEST_AND_FREE(UncheckedMalloc<char>(1));
TEST_AND_FREE(UncheckedMalloc(0));
TEST_AND_FREE(UncheckedMalloc(1));
}

TEST(UtilTest, UncheckedCalloc) {
using node::UncheckedCalloc;
EXPECT_NE(nullptr, UncheckedCalloc<char>(0));
EXPECT_NE(nullptr, UncheckedCalloc<char>(1));
EXPECT_NE(nullptr, UncheckedCalloc(0));
EXPECT_NE(nullptr, UncheckedCalloc(1));
TEST_AND_FREE(UncheckedCalloc<char>(0));
TEST_AND_FREE(UncheckedCalloc<char>(1));
TEST_AND_FREE(UncheckedCalloc(0));
TEST_AND_FREE(UncheckedCalloc(1));
}

0 comments on commit a29be52

Please sign in to comment.