changeset 25347:22e5eb039c83

Fix memory leak. I thought asx_get_attrib() return a const char *, but indeed it return string by strdup.
author ulion
date Wed, 12 Dec 2007 06:32:54 +0000
parents 46736c300ae2
children 98598befcdd7
files libmenu/menu.c
diffstat 1 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/libmenu/menu.c	Wed Dec 12 05:39:17 2007 +0000
+++ b/libmenu/menu.c	Wed Dec 12 06:32:54 2007 +0000
@@ -109,7 +109,7 @@
 
     if (!strcasecmp(element, "keybindings")) {
       menu_cmd_bindings_t *bindings = cmd_bindings;
-      const char *parent_bindings;
+      char *parent_bindings;
       cmd_bindings = realloc(cmd_bindings,
                              (cmd_bindings_num+1)*sizeof(menu_cmd_bindings_t));
       for (i = 0; i < cmd_bindings_num; ++i)
@@ -117,10 +117,12 @@
           cmd_bindings[i].parent = cmd_bindings[i].parent-bindings+cmd_bindings;
       bindings = &cmd_bindings[cmd_bindings_num];
       memset(bindings, 0, sizeof(menu_cmd_bindings_t));
-      bindings->name = strdup(name);
+      bindings->name = name;
       parent_bindings = asx_get_attrib("parent",attribs);
-      if (parent_bindings)
+      if (parent_bindings) {
         bindings->parent = get_cmd_bindings(parent_bindings);
+        free(parent_bindings);
+      }
       free(element);
       asx_free_attribs(attribs);
       if (body) {
@@ -147,10 +149,12 @@
             bindings->bindings = realloc(bindings->bindings,
                                    (bindings->binding_num+1)*sizeof(key_cmd_t));
             bindings->bindings[bindings->binding_num].key = keycode;
-            bindings->bindings[bindings->binding_num].cmd = cmd ? strdup(cmd)
-                                                                : NULL;
+            bindings->bindings[bindings->binding_num].cmd = cmd;
             ++bindings->binding_num;
           }
+          else
+            free(cmd);
+          free(key);
           free(element);
           asx_free_attribs(attribs);
           free(b);