Hello,

I was looking through svn code and found several memory leaks. Not sure if
this is the preferred fix, but it should show you what I was looking at.

Signed-off-by: Steve Grubb <[email protected]>
---

diff -urp openocd.orig/src/helper/command.c openocd/src/helper/command.c
--- openocd.orig/src/helper/command.c   2009-08-23 11:29:44.000000000 -0400
+++ openocd/src/helper/command.c        2009-08-23 11:37:03.000000000 -0400
@@ -117,6 +117,10 @@ static int script_command(Jim_Interp *in
                words[i] = strdup(w);
                if (words[i] == NULL)
                {
+                       int j;
+                       for (j = 0; j < i; j++)
+                               free(words[j]);
+                       free(words);
                        return JIM_ERR;
                }
        }
diff -urp openocd.orig/src/jtag/tcl.c openocd/src/jtag/tcl.c
--- openocd.orig/src/jtag/tcl.c 2009-08-23 11:29:44.000000000 -0400
+++ openocd/src/jtag/tcl.c      2009-08-23 11:39:54.000000000 -0400
@@ -220,6 +220,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo
         * */
        if (goi->argc < 3) {
                Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS 
....");
+               free(pTap);
                return JIM_ERR;
        }
        Jim_GetOpt_String(goi, &cp, NULL);
@@ -249,6 +250,8 @@ static int jim_newtap_cmd(Jim_GetOptInfo
                e = Jim_GetOpt_Nvp(goi, opts, &n);
                if (e != JIM_OK) {
                        Jim_GetOpt_NvpUnknown(goi, opts, 0);
+                       free(pTap->dotted_name);
+                       free(pTap);
                        return e;
                }
                LOG_DEBUG("Processing option: %s", n->name);
@@ -266,12 +269,16 @@ static int jim_newtap_cmd(Jim_GetOptInfo
                        e = Jim_GetOpt_Wide(goi, &w);
                        if (e != JIM_OK) {
                                Jim_SetResult_sprintf(goi->interp, "option: %s 
bad parameter", n->name);
+                               free(pTap->dotted_name);
+                               free(pTap);
                                return e;
                        }
 
                        new_expected_ids = malloc(sizeof(uint32_t) * 
(pTap->expected_ids_cnt + 1));
                        if (new_expected_ids == NULL) {
                                Jim_SetResult_sprintf(goi->interp, "no memory");
+                               free(pTap->dotted_name);
+                               free(pTap);
                                return JIM_ERR;
                        }
 
@@ -290,6 +297,8 @@ static int jim_newtap_cmd(Jim_GetOptInfo
                        e = Jim_GetOpt_Wide(goi, &w);
                        if (e != JIM_OK) {
                                Jim_SetResult_sprintf(goi->interp, "option: %s 
bad parameter", n->name);
+                               free(pTap->dotted_name);
+                               free(pTap);
                                return e;
                        }
                        switch (n->value) {
@@ -303,6 +312,8 @@ static int jim_newtap_cmd(Jim_GetOptInfo
                                if (is_bad_irval(pTap->ir_length, w)) {
                                        LOG_ERROR("IR mask %x too big",
                                                        (int) w);
+                                       free(pTap->dotted_name);
+                                       free(pTap);
                                        return ERROR_FAIL;
                                }
                                pTap->ir_capture_mask = w;
@@ -312,6 +323,8 @@ static int jim_newtap_cmd(Jim_GetOptInfo
                                if (is_bad_irval(pTap->ir_length, w)) {
                                        LOG_ERROR("IR capture %x too big",
                                                        (int) w);
+                                       free(pTap->dotted_name);
+                                       free(pTap);
                                        return ERROR_FAIL;
                                }
                                pTap->ir_capture_value = w;
@@ -1144,7 +1157,12 @@ static int handle_irscan_command(struct 
                tap = jtag_tap_by_string(args[i*2]);
                if (tap == NULL)
                {
+                       int j;
+                       for (j = 0; j < i; j++)
+                               free(fields[j].out_value);
+                        free(fields);
                        command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
+
                        return ERROR_FAIL;
                }
                int field_size = tap->ir_length;
diff -urp openocd.orig/src/jtag/usbprog.c openocd/src/jtag/usbprog.c
--- openocd.orig/src/jtag/usbprog.c     2009-08-23 11:29:44.000000000 -0400
+++ openocd/src/jtag/usbprog.c  2009-08-23 11:42:16.000000000 -0400
@@ -435,6 +435,7 @@ struct usbprog_jtag* usbprog_jtag_open(v
                        }
                }
        }
+       free(tmp);
        return 0;
 }
 
diff -urp openocd.orig/src/server/telnet_server.c 
openocd/src/server/telnet_server.c
--- openocd.orig/src/server/telnet_server.c     2009-08-23 11:29:44.000000000 
-0400
+++ openocd/src/server/telnet_server.c  2009-08-23 12:08:42.000000000 -0400
@@ -597,6 +597,7 @@ int telnet_init(char *banner)
        if (telnet_port == 0)
        {
                LOG_INFO("telnet port disabled");
+               free(telnet_service);
                return ERROR_OK;
        }
 
diff -urp openocd.orig/src/svf/svf.c openocd/src/svf/svf.c
--- openocd.orig/src/svf/svf.c  2009-08-23 11:29:44.000000000 -0400
+++ openocd/src/svf/svf.c       2009-08-23 11:44:50.000000000 -0400
@@ -1322,6 +1322,7 @@ static int svf_run_command(struct comman
                                if (!svf_tap_state_is_valid(path[i]))
                                {
                                        LOG_ERROR("%s is not valid state", 
svf_tap_state_name[path[i]]);
+                                       free(path);
                                        return ERROR_FAIL;
                                }
                                if (TAP_RESET == path[i])
@@ -1348,6 +1349,7 @@ static int svf_run_command(struct comman
                                else
                                {
                                        LOG_ERROR("%s is not valid state", 
svf_tap_state_name[path[num_of_argu - 1]]);
+                                       free(path);
                                        return ERROR_FAIL;
                                }
                        }
diff -urp openocd.orig/src/target/embeddedice.c openocd/src/target/embeddedice.c
--- openocd.orig/src/target/embeddedice.c       2009-08-23 11:29:44.000000000 
-0400
+++ openocd/src/target/embeddedice.c    2009-08-23 11:48:21.000000000 -0400
@@ -197,6 +197,7 @@ reg_cache_t* embeddedice_build_reg_cache
                        free(reg_list[i].value);
                }
                free(reg_list);
+               free(reg_cache);
                free(arch_info);
                return NULL;
        }
diff -urp openocd.orig/src/target/etm.c openocd/src/target/etm.c
--- openocd.orig/src/target/etm.c       2009-08-23 11:29:44.000000000 -0400
+++ openocd/src/target/etm.c    2009-08-23 11:53:21.000000000 -0400
@@ -246,6 +246,12 @@ reg_cache_t* etm_build_reg_cache(target_
                if (!etb)
                {
                        LOG_ERROR("etb selected as etm capture driver, but no 
ETB configured");
+                       for (i = 0; i < num_regs; i++)
+                       {
+                               free(reg_list[i].value);
+                       }
+                       free(reg_cache);
+                       free(arch_info);
                        return ERROR_OK;
                }
 
@@ -1202,6 +1208,7 @@ static int handle_etm_config_command(str
 
        if (argc != 5)
        {
+               free(etm_ctx);
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
@@ -1209,12 +1216,14 @@ static int handle_etm_config_command(str
        if (!target)
        {
                LOG_ERROR("target '%s' not defined", args[0]);
+               free(etm_ctx);
                return ERROR_FAIL;
        }
 
        if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
        {
                command_print(cmd_ctx, "current target isn't an ARM7/ARM9 
target");
+               free(etm_ctx);
                return ERROR_FAIL;
        }
 
@@ -1231,6 +1240,7 @@ static int handle_etm_config_command(str
                        break;
                default:
                        command_print(cmd_ctx, "unsupported ETM port width 
'%s', must be 4, 8 or 16", args[1]);
+                       free(etm_ctx);
                        return ERROR_FAIL;
        }
 
@@ -1249,6 +1259,7 @@ static int handle_etm_config_command(str
        else
        {
                command_print(cmd_ctx, "unsupported ETM port mode '%s', must be 
'normal', 'multiplexed' or 'demultiplexed'", args[2]);
+               free(etm_ctx);
                return ERROR_FAIL;
        }
 
@@ -1263,6 +1274,7 @@ static int handle_etm_config_command(str
        else
        {
                command_print(cmd_ctx, "unsupported ETM port clocking '%s', 
must be 'full' or 'half'", args[3]);
+               free(etm_ctx);
                return ERROR_FAIL;
        }
 

_______________________________________________
Openocd-development mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to