[freetds] dbstring_free memory leak

Frediano Ziglio freddyz77 at tin.it
Sun Feb 1 03:13:02 EST 2004


Il sab, 2004-01-31 alle 21:07, James K. Lowden ha scritto:
> On 31 Jan 2004 16:22:16 +0100, Frediano Ziglio <freddyz77 at tin.it> wrote:
> > Il gio, 2004-01-29 alle 22:28, Edward Quackenbush ha scritto:
> > > I encountered a memory leak when repeatedly opening and closing a 
> > > conection using the DB-lib interface.  It appears that the
> > > dbstring_free function in dblib.c does not free the string.  The
> > > following patch should correct this issue. 
> > > eq 
> > > 
> > 
> > Applied
> 
> I don't see why this is needed.  I think the pre-patched code is right. 
> Can someone explain why not?  
> 
> struct dbstring
> {
> 	BYTE *strtext;
> 	DBINT strtotlen;
> 	struct dbstring *strnext;
> };
> typedef struct dbstring DBSTRING;
> 
>  1  dbstring_free(DBSTRING ** dbstrp)
>  2  {
>  3          if ((dbstrp != NULL) && (*dbstrp != NULL)) {
>  4                  if ((*dbstrp)->strnext != NULL) {
>  5                          dbstring_free(&((*dbstrp)->strnext));
>  6                  }
>  7                  free(*dbstrp);
>  8                  *dbstrp = NULL;
>  9          }
> 10  }
> 
> So, if I have:
> 
> DBSTRING a, b;
> 
> a.strtext = malloc(10);
> a.strtotlen = 10;
> a.strnext = &b;
> 
> b.strtext = malloc(10);
> b.strtotlen = 10;
> b.strnext = NULL;
> 
> Then the call dbstring_free(&a) should:
> 
> == nest level 0, examining a ==
> #3  dbstrp != NULL) && (*dbstrp != NULL), so
> #4  (*dbstrp)->strnext != NULL // because it's b, so
> #5  dbstring_free(&((*dbstrp)->strnext));
> == nest level 1, examining b ==
> #3  dbstrp != NULL) && (*dbstrp != NULL), so
> #4  (*dbstrp)->strnext == NULL, so
> #7  free(*dbstrp); // frees b
> #8  *dbstrp = NULL;
> #10 return
> == nest level 0, continuing with a ==
> #7  free(*dbstrp); // frees a
> #8  *dbstrp = NULL;
> #10 return
> 
> Thus, both a and b are freed.  The effect of the patch should be nil,
> calling free(3) on a NULL pointer, set by the recursive call.  
> 

However a.strtext and b.strtext was not freed. Just strnext and strtext
are very similar :)

freddy77





More information about the FreeTDS mailing list