From 7676d95949b1e33c1d20325c0231fc774364136e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 27 Jun 2021 12:45:04 -0400 Subject: [PATCH] Remove memory leaks in isolationtester. specscanner.l leaked a kilobyte of memory per token of the spec file. Apparently somebody thought that the introductory code block would be executed once; but it's once per yylex() call. A couple of functions in isolationtester.c leaked small amounts of memory due to not bothering to free one-time allocations. Might as well improve these so that valgrind gives this program a clean bill of health. Also get rid of an ugly static variable. Coverity complained about one of the one-time leaks, which led me to try valgrind'ing isolationtester, which led to discovery of the larger leak. --- src/test/isolation/isolationtester.c | 20 +++++++++++++------- src/test/isolation/specscanner.l | 8 ++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c index 978873726e8..f25c3127463 100644 --- a/src/test/isolation/isolationtester.c +++ b/src/test/isolation/isolationtester.c @@ -53,8 +53,8 @@ static int64 max_step_wait = 300 * USECS_PER_SEC; static void check_testspec(TestSpec *testspec); static void run_testspec(TestSpec *testspec); static void run_all_permutations(TestSpec *testspec); -static void run_all_permutations_recurse(TestSpec *testspec, int nsteps, - PermutationStep **steps); +static void run_all_permutations_recurse(TestSpec *testspec, int *piles, + int nsteps, PermutationStep **steps); static void run_named_permutations(TestSpec *testspec); static void run_permutation(TestSpec *testspec, int nsteps, PermutationStep **steps); @@ -365,9 +365,9 @@ check_testspec(TestSpec *testspec) fprintf(stderr, "unused step name: %s\n", allsteps[i]->name); } } -} -static int *piles; + free(allsteps); +} /* * Run the permutations specified in the spec, or all if none were @@ -392,6 +392,7 @@ run_all_permutations(TestSpec *testspec) int i; PermutationStep *steps; PermutationStep **stepptrs; + int *piles; /* Count the total number of steps in all sessions */ nsteps = 0; @@ -417,11 +418,16 @@ run_all_permutations(TestSpec *testspec) for (i = 0; i < testspec->nsessions; i++) piles[i] = 0; - run_all_permutations_recurse(testspec, 0, stepptrs); + run_all_permutations_recurse(testspec, piles, 0, stepptrs); + + free(steps); + free(stepptrs); + free(piles); } static void -run_all_permutations_recurse(TestSpec *testspec, int nsteps, PermutationStep **steps) +run_all_permutations_recurse(TestSpec *testspec, int *piles, + int nsteps, PermutationStep **steps) { int i; bool found = false; @@ -443,7 +449,7 @@ run_all_permutations_recurse(TestSpec *testspec, int nsteps, PermutationStep **s piles[i]++; - run_all_permutations_recurse(testspec, nsteps + 1, steps); + run_all_permutations_recurse(testspec, piles, nsteps + 1, steps); piles[i]--; diff --git a/src/test/isolation/specscanner.l b/src/test/isolation/specscanner.l index c4c6266d7fd..4f94714ecb8 100644 --- a/src/test/isolation/specscanner.l +++ b/src/test/isolation/specscanner.l @@ -50,8 +50,12 @@ self [,()*] %% %{ - litbuf = pg_malloc(LITBUF_INIT); - litbufsize = LITBUF_INIT; + /* Allocate litbuf in first call of yylex() */ + if (litbuf == NULL) + { + litbuf = pg_malloc(LITBUF_INIT); + litbufsize = LITBUF_INIT; + } %} /* Keywords (must appear before the {identifier} rule!) */ -- 2.39.5