Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vdr] Re: vdr 1.3.19: memory leak in AddDirectory



Stefan Huelswitt wrote:
Hi,
I noticed that there is a memory leak in AddDirectory().
The functions uses asprint() to malloc the new directory string,
but on exit this string is strdup'ed by cString and the malloc'ed
buffer is lost.

The attached patch fixes the problem. If a static sized buffer is
not acceptable, we would have to use something like:

char buf[strlen(DirName)+strlen(FileName)+3];

But it seems that gcc 2.95.x cannot handle this correctly, at
least not with -O2.

There is a similiar memory leak in strescape(), but I don't see a
easy way to resolv this.
I guess what we need here is a way to tell cString to take ownership
of the given string. The attached patch should do this.

Klaus
--- tools.h	2005/01/16 11:39:58	1.65
+++ tools.h	2005/02/05 10:00:22
@@ -61,7 +61,7 @@
 private:
   char *s;
 public:
-  cString(const char *S = NULL);
+  cString(const char *S = NULL, bool TakePointer = false);
   virtual ~cString();
   operator const char * () const { return s; } // for use in (const char *) context
   const char * operator*() const { return s; } // for use in (const void *) context (printf() etc.)
--- tools.c	2005/01/16 11:47:44	1.88
+++ tools.c	2005/02/05 10:10:30
@@ -199,7 +199,7 @@
         }
   if (t)
      *t = 0;
-  return s;
+  return cString(s, t != NULL);
 }
 
 bool startswith(const char *s, const char *p)
@@ -250,7 +250,7 @@
 {
   char *buf;
   asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName);
-  return buf;
+  return cString(buf, true);
 }
 
 cString itoa(int n)
@@ -513,9 +513,9 @@
 
 // --- cString ---------------------------------------------------------------
 
-cString::cString(const char *S)
+cString::cString(const char *S, bool TakePointer)
 {
-  s = S ? strdup(S) : NULL;
+  s = TakePointer ? (char *)S : S ? strdup(S) : NULL;
 }
 
 cString::~cString()

Home | Main Index | Thread Index