[PATCH 1.1] Properties: Fixed Memory Leaks in Properties


John Gray <jgray@...>
 

Free memory and fail if realloc fails in propertiesImpl_addKey(). When we

replace a value in the hash table in propertiesImpl_addProperty(), free the old

value.

 

Index: c_cpp/src/c/property.c

===================================================================

RCS file: /cvsroot/products/common/c_cpp/src/c/property.c,v

retrieving revision 1.22.2.2.2.1.2.4

diff -u -r1.22.2.2.2.1.2.4 property.c

--- c_cpp/src/c/property.c  7 Sep 2011 09:45:08 -0000   1.22.2.2.2.1.2.4

+++ c_cpp/src/c/property.c  18 Jan 2012 02:56:09 -0000

@@ -326,7 +326,7 @@

    propertiesImpl_ *this = (propertiesImpl_ *)handle;

    const char*      rval = NULL;

 

-   if( name == NULL || strlen( name ) == 0 )

+   if( name == NULL || NULL == this || strlen( name ) == 0 )

    {

        return NULL;

    }

@@ -554,9 +554,19 @@

         }

         else

         {

-            this->mKeys = (const char* *)realloc(

-                (void* )this->mKeys,

-                allocSize * sizeof( const char* ) );

+            void* reallocBlock =

+                realloc((void* )this->mKeys, (allocSize * sizeof(const char*)));

+

+            if(NULL != reallocBlock)

+            {

+                this->mKeys = (const char**)reallocBlock;

+            }

+            else

+            {

+                free((void* )this->mKeys);

+                this->mKeys = NULL;

+                return 0;

+            }

         }

     }

 

@@ -571,28 +581,36 @@

                             const char* value )

{

     propertiesImpl_ *this = (propertiesImpl_*)properties;

+    void* data = NULL;

+    int ret = 0;

 

-    if( gPropertyDebug )

-    {

-        fprintf (stderr,

-                 "\nAddProperty KEY: %s, VALUE: %s\n",

-                 name,

-                 value);

-    }

+    if( NULL == this )

+        return 0;

 

-    if ( NULL == wtable_lookup( this->mTable, (char* )name ))

+    if ( NULL == (data = wtable_lookup(this->mTable, (char*)name)))

     {

         if( !propertiesImpl_AddKey( this, name ))

             return 0;

     }

 

-    if (-1==wtable_insert( this->mTable, (char* )name, (caddr_t)value ))

+    if(-1 == (ret = wtable_insert( this->mTable, (char* )name, (caddr_t)value)))

     {

         return 0;

     }

 

+    if(0 == ret) /* If 0 is returned then data has been replaced. */

+    {

+        /* If existing data in the table has now been replaced then the old data must be freed. */

+        if(NULL != data)

+        {

+            free(data);

+        }

+

+       if(gPropertyDebug)

+        {

+           fprintf(stderr, "\nAddProperty KEY: %s, VALUE: %s\n", name, value);

+       }

+    }

 

     return 1;

}

-

-

 

Signed-off-by: John Gray <jgray@...>