You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@axis.apache.org by bi...@apache.org on 2018/08/21 16:26:21 UTC

svn commit: r1838567 - in /axis/axis2/c/core/trunk/src/core: engine/conf.c util/core_utils.c

Author: billblough
Date: Tue Aug 21 16:26:21 2018
New Revision: 1838567

URL: http://svn.apache.org/viewvc?rev=1838567&view=rev
Log:
Fix memory leaks

Modified:
    axis/axis2/c/core/trunk/src/core/engine/conf.c
    axis/axis2/c/core/trunk/src/core/util/core_utils.c

Modified: axis/axis2/c/core/trunk/src/core/engine/conf.c
URL: http://svn.apache.org/viewvc/axis/axis2/c/core/trunk/src/core/engine/conf.c?rev=1838567&r1=1838566&r2=1838567&view=diff
==============================================================================
--- axis/axis2/c/core/trunk/src/core/engine/conf.c (original)
+++ axis/axis2/c/core/trunk/src/core/engine/conf.c Tue Aug 21 16:26:21 2018
@@ -374,17 +374,22 @@ axis2_conf_free(
     if(conf->name_to_version_map)
     {
         axutil_hash_index_t *hi = NULL;
+        void *key = NULL;
         void *val = NULL;
         for(hi = axutil_hash_first(conf->name_to_version_map, env); hi; hi = axutil_hash_next(env,
             hi))
         {
             axis2_char_t *module_ver = NULL;
-            axutil_hash_this(hi, NULL, NULL, &val);
+            axutil_hash_this(hi, &key, NULL, &val);
             module_ver = (axis2_char_t *)val;
             if(module_ver)
             {
                 AXIS2_FREE(env->allocator, module_ver);
             }
+            if(key)
+            {
+                AXIS2_FREE(env->allocator, key);
+            }
         }
         axutil_hash_free(conf->name_to_version_map, env);
     }
@@ -1713,7 +1718,14 @@ axis2_conf_add_default_module_version(
         {
             return AXIS2_FAILURE;
         }
-        axutil_hash_set(name_to_ver_map, module_name, AXIS2_HASH_KEY_STRING, new_entry);
+        axis2_char_t *new_name = axutil_strdup(env, module_name);
+        if(!new_entry)
+        {
+            AXIS2_FREE(env->allocator, new_entry);
+            return AXIS2_FAILURE;
+        }
+
+        axutil_hash_set(name_to_ver_map, new_name, AXIS2_HASH_KEY_STRING, new_entry);
         return AXIS2_SUCCESS;
     }
     return AXIS2_FAILURE;

Modified: axis/axis2/c/core/trunk/src/core/util/core_utils.c
URL: http://svn.apache.org/viewvc/axis/axis2/c/core/trunk/src/core/util/core_utils.c?rev=1838567&r1=1838566&r2=1838567&view=diff
==============================================================================
--- axis/axis2/c/core/trunk/src/core/util/core_utils.c (original)
+++ axis/axis2/c/core/trunk/src/core/util/core_utils.c Tue Aug 21 16:26:21 2018
@@ -352,30 +352,44 @@ axis2_core_utils_calculate_default_modul
                         if(module_ver_str && AXIS2_TRUE == axis2_core_utils_is_latest_mod_ver(env,
                             module_ver_str, current_def_ver))
                         {
+                            axis2_char_t *old_ver = NULL;
+                            old_ver = axutil_hash_get(default_modules,
+                                                      module_name_str,
+                                                      AXIS2_HASH_KEY_STRING);
+                            if (old_ver) {
+                                AXIS2_FREE(env->allocator, old_ver);
+                                old_ver = NULL;
+                            }
                             axutil_hash_set(default_modules, module_name_str,
                                 AXIS2_HASH_KEY_STRING, module_ver_str);
+                            AXIS2_FREE(env->allocator, module_name_str);
+                            module_name_str = NULL;
                         }
                         else
                         {
                             if(module_name_str)
                             {
                                 AXIS2_FREE(env->allocator, module_name_str);
+                                module_name_str = NULL;
                             }
                             if(module_ver_str)
                             {
                                 AXIS2_FREE(env->allocator, module_ver_str);
+                                module_ver_str = NULL;
                             }
                         }
                     }
                     else
                     {
-                        axutil_hash_set(default_modules, module_name_str, AXIS2_HASH_KEY_STRING,
-                            module_ver_str);
-                    }
-
-                    if(module_name_str)
-                    {
-                        AXIS2_FREE(env->allocator, module_name_str);
+                        if (module_ver_str)
+                        {
+                            axutil_hash_set(default_modules, module_name_str, AXIS2_HASH_KEY_STRING,
+                                module_ver_str);
+                        }
+                        else
+                        {
+                                AXIS2_FREE(env->allocator, module_name_str);
+                        }
                     }
                 }
             }
@@ -395,6 +409,12 @@ axis2_core_utils_calculate_default_modul
                 (axis2_char_t *)val);
             AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI, "Added default module"
                 " version : %s for module : %s", (axis2_char_t *)val, (axis2_char_t *)key_string);
+            AXIS2_FREE(env->allocator, val);
+            /* the key stored in the hash is the dynamically allocated module_name,
+             * so to avoid a memleak we have to free it before freeing the hash.
+             * TODO: find a better way to deal with this
+             */
+            AXIS2_FREE(env->allocator, key_string);
         }
     }