[PATCH] sim: Be sure of calling freeargv() after successfully call buildargv().

Chen Gang S
2015-01-28T11:37:46Z

buildargv() and freeargv() are pairs, so need be sure of them always
paired to avoid memory leak.

2015-01-28 Chen Gang <gang.chen.5i5j@gmail.com>

* common/sim-options.c (sim_args_command): Call freeargv() when
failure occurs.
* mcore/interp.c (sim_do_command): Call freeargv() before return.
* microblaze/interp.c (sim_do_command): Call freeargv() before
return.
---
sim/ChangeLog | 8 ++++++++
sim/common/sim-options.c | 5 ++++-
sim/mcore/interp.c | 3 +++
sim/microblaze/interp.c | 3 +++
4 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/sim/ChangeLog b/sim/ChangeLog
index 03c244b..a5c3f5d 100644
--- a/sim/ChangeLog
+++ b/sim/ChangeLog
@@ -1,3 +1,11 @@
+2015-01-28 Chen Gang <gang.chen.5i5j@gmail.com>
+
+ * common/sim-options.c (sim_args_command): Call freeargv() when
+ failure occurs.
+ * mcore/interp.c (sim_do_command): Call freeargv() before return.
+ * microblaze/interp.c (sim_do_command): Call freeargv() before
+ return.
+
2014-07-01 Chen Gang <gang.chen.5i5j@gmail.com>

* sim/microblaze/interp.c: Use long int format instead of int
diff --git a/sim/common/sim-options.c b/sim/common/sim-options.c
index c49220e..814edcf 100644
--- a/sim/common/sim-options.c
+++ b/sim/common/sim-options.c
@@ -993,7 +993,10 @@ sim_args_command (SIM_DESC sd, const char *cmd)
sim_cpu *cpu;

if (argv [0] == NULL)
- return SIM_RC_OK; /* FIXME - perhaps help would be better */
+ {
+ freeargv (argv);
+ return SIM_RC_OK; /* FIXME - perhaps help would be better */
+ }

/* First check for a cpu selector. */
{
diff --git a/sim/mcore/interp.c b/sim/mcore/interp.c
index d2edd12..dfaa6aa 100644
--- a/sim/mcore/interp.c
+++ b/sim/mcore/interp.c
@@ -2143,6 +2143,7 @@ sim_do_command (sd, cmd)
if ((simargv[1] == NULL) || (simargv[2] == NULL))
{
fprintf (stderr, "Error: missing argument to watch cmd.\n");
+ freeargv (simargv);
return;
}

@@ -2187,6 +2188,8 @@ sim_do_command (sd, cmd)
fprintf (stderr,"Error: \"%s\" is not a valid M.CORE simulator command.\n",
cmd);
}
+
+ freeargv (simargv);
}
else
{
diff --git a/sim/microblaze/interp.c b/sim/microblaze/interp.c
index 1c8a22d..4fc4595 100644
--- a/sim/microblaze/interp.c
+++ b/sim/microblaze/interp.c
@@ -1019,6 +1019,7 @@ sim_do_command (SIM_DESC sd, const char *cmd)
if ((simargv[1] == NULL) || (simargv[2] == NULL))
{
fprintf (stderr, "Error: missing argument to watch cmd.\n");
+ freeargv (simargv);
return;
}

@@ -1062,6 +1063,8 @@ sim_do_command (SIM_DESC sd, const char *cmd)
fprintf (stderr,"Error: \"%s\" is not a valid M.CORE simulator command.\n",
cmd);
}
+
+ freeargv (simargv);
}
else
{
--
1.9.3 (Apple Git-50)