Skip to Content.
Sympa Menu

freetds - Re: [freetds] dbstring_free memory leak

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: "James K. Lowden" <jklowden AT schemamania.org>
  • To: FreeTDS Development Group <freetds AT lists.ibiblio.org>
  • Subject: Re: [freetds] dbstring_free memory leak
  • Date: Sat, 31 Jan 2004 15:07:23 -0500

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.

--jkl




Archive powered by MHonArc 2.6.24.

Top of Page