sim: fix minor --sysroot mem leak
authorMike Frysinger <vapier@gentoo.org>
Fri, 27 May 2011 18:05:16 +0000 (18:05 +0000)
committerMike Frysinger <vapier@gentoo.org>
Fri, 27 May 2011 18:05:16 +0000 (18:05 +0000)
The current --sysroot parsing attempts to keep from leaking memory by
treating the empty string specially (sine this is the initial value),
but it ends up leaking memory when the arg is an empty string.  So if
someone uses --sysroot "", the old value is leaked, as is the new one.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
sim/common/ChangeLog
sim/common/sim-options.c

index b6805db..821248d 100644 (file)
@@ -1,3 +1,9 @@
+2011-05-27  Mike Frysinger  <vapier@gentoo.org>
+
+       * sim-options.c (standard_option_handler): Remove arg[0] check
+       when freeing simulator_sysroot.  Only strdup arg when arg[0] is
+       not an empty string, otherwise assign "" back to it.
+
 2011-05-26  Mike Frysinger  <vapier@gentoo.org>
 
        * nltvals.def: Regenerate to include Blackfin syscalls again.
index ddab83d..1a0c541 100644 (file)
@@ -458,10 +458,14 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
 
     case OPTION_SYSROOT:
       /* Don't leak memory in the odd event that there's lots of
-        --sysroot=... options.  */
-      if (simulator_sysroot[0] != '\0' && arg[0] != '\0')
+        --sysroot=... options.  We treat "" specially since this
+        is the statically initialized value and cannot free it.  */
+      if (simulator_sysroot[0] != '\0')
        free (simulator_sysroot);
-      simulator_sysroot = xstrdup (arg);
+      if (arg[0] != '\0')
+       simulator_sysroot = xstrdup (arg);
+      else
+       simulator_sysroot = "";
       break;
     }