Remove memory leaks in isolationtester.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 27 Jun 2021 16:45:04 +0000 (12:45 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 27 Jun 2021 16:45:04 +0000 (12:45 -0400)
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
src/test/isolation/specscanner.l

index 7258dfa8b9cc64cfdb9682e325f7588c28df8bbb..cba371fe6ee98bed3a72801c3c1e03bc922df116 100644 (file)
@@ -54,8 +54,8 @@ static void exit_nicely(void) pg_attribute_noreturn();
 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);
@@ -366,9 +366,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
@@ -393,6 +393,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;
@@ -418,11 +419,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;
@@ -444,7 +450,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]--;
 
index 2e8f7f58694e6d7c2b6f57f8fde519e4dd589f3d..6c18460942d3269168d312da08a83eaeb7fe04c4 100644 (file)
@@ -52,8 +52,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!) */