This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[libgfortran,committed] Fix an invalid free in cleanup function


I've had this in my tree for some time now, it fixes an invalid free
in libgfortran destructor function when the executable is called with
an absolute path (it was introduced by my -fbacktrace patch). It also
forces initialization of a temporary character buffer, used for a
getcwd() call.

Bootstrapped and regtesting on x86_64-linux. I'll commit when testing
is finished.


2007-04-19 Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>


       * runtime/main.c (please_free_exe_path_when_done): New variable.
       (store_exe_path): Initialize character buffer, and mark whether
       exe_path should be free'd by the library destructor function.
       (cleanup): Only free exe_path if needed.
Index: runtime/main.c
===================================================================
--- runtime/main.c      (revision 123968)
+++ runtime/main.c      (working copy)
@@ -97,6 +97,7 @@
 
 
 static const char *exe_path;
+static int please_free_exe_path_when_done;
 
 /* Save the path under which the program was called, for use in the
    backtrace routines.  */
@@ -116,15 +117,18 @@
   if (argv0[0] == '/')
     {
       exe_path = argv0;
+      please_free_exe_path_when_done = 0;
       return;
     }
 
+  memset (buf, 0, sizeof (buf));
   cwd = getcwd (buf, sizeof (buf));
 
   /* exe_path will be cwd + "/" + argv[0] + "\0" */
   path = malloc (strlen (cwd) + 1 + strlen (argv0) + 1);
   st_sprintf (path, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
   exe_path = path;
+  please_free_exe_path_when_done = 1;
 }
 
 /* Return the full path of the executable.  */
@@ -168,4 +172,7 @@
 cleanup (void)
 {
   close_units ();
+  
+  if (please_free_exe_path_when_done)
+    free (exe_path);
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]