← Back to team overview

maria-developers team mailing list archive

[patch 09/11] Fix Valgrind-reported memory leak in top DBUG stack frame.

 

If DbugParse() is called multiple times, the stack->keywords for the
top stack frame could be overwritten without being freed, causing a
memory leak reported by Valgrind.

=== modified file 'dbug/dbug.c'
---
 dbug/dbug.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Index: dbug/dbug.c
===================================================================
--- dbug/dbug.c.orig	2009-04-07 13:40:35.000000000 +0200
+++ dbug/dbug.c	2009-04-08 00:35:07.000000000 +0200
@@ -506,6 +506,9 @@ int DbugParse(CODE_STATE *cs, const char
   rel= control[0] == '+' || control[0] == '-';
   if ((!rel || (!stack->out_file && !stack->next)))
   {
+    /* If overwriting previous state, be sure to free old to avoid leak. */
+    if (stack->out_file)
+      FreeState(cs, stack, 0);
     stack->flags= 0;
     stack->delay= 0;
     stack->maxdepth= 0;
@@ -1648,10 +1651,12 @@ static void FreeState(CODE_STATE *cs, st
     FreeList(state->processes);
   if (!is_shared(state, p_functions))
     FreeList(state->p_functions);
-  if (!is_shared(state, out_file))
+  if (!is_shared(state, out_file) &&
+      state->out_file != stderr && state->out_file != stdout)
     DBUGCloseFile(cs, state->out_file);
   (void) fflush(cs->stack->out_file);
-  if (state->prof_file)
+  if (state->prof_file &&
+      state->prof_file != stderr && state->prof_file != stdout)
     DBUGCloseFile(cs, state->prof_file);
   if (free_state)
     free((void*) state);

-- 



Follow ups

References