changeset 9576:8cae100b8cd1

Improve memory cleanup in 'relocatable' module.
author Bruno Haible <bruno@clisp.org>
date Tue, 08 Jan 2008 00:28:30 +0100
parents a30e05c634d8
children 0b6c2edb43b3
files ChangeLog lib/progreloc.c lib/relocatable.c lib/relocatable.h
diffstat 4 files changed, 49 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jan 07 23:29:52 2008 +0100
+++ b/ChangeLog	Tue Jan 08 00:28:30 2008 +0100
@@ -1,3 +1,14 @@
+2008-01-01  Sylvain Beucler  <beuc@gnu.org>
+            Bruno Haible  <bruno@clisp.org>
+
+	Improve memory cleanup in 'relocatable' module.
+	* lib/relocatable.h (compute_curr_prefix): Change return type to
+	'char *'.
+	* lib/relocatable.c (compute_curr_prefix): Change return type to
+	'char *'. Free curr_installdir after use.
+	(relocate): Free curr_prefix_better after use.
+	* lib/progreloc.c (prepare_relocate): Free curr_prefix after use.
+
 2008-01-01  Bruno Haible  <bruno@clisp.org>
 
 	* tests/test-wcwidth.c (main): Relax test of U+2060. Avoids a test
--- a/lib/progreloc.c	Mon Jan 07 23:29:52 2008 +0100
+++ b/lib/progreloc.c	Tue Jan 08 00:28:30 2008 +0100
@@ -1,5 +1,5 @@
 /* Provide relocatable programs.
-   Copyright (C) 2003-2007 Free Software Foundation, Inc.
+   Copyright (C) 2003-2008 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software: you can redistribute it and/or modify
@@ -281,7 +281,7 @@
 prepare_relocate (const char *orig_installprefix, const char *orig_installdir,
 		  const char *argv0)
 {
-  const char *curr_prefix;
+  char *curr_prefix;
 
   /* Determine the full pathname of the current executable.  */
   executable_fullname = find_executable (argv0);
@@ -290,8 +290,12 @@
   curr_prefix = compute_curr_prefix (orig_installprefix, orig_installdir,
 				     executable_fullname);
   if (curr_prefix != NULL)
-    /* Now pass this prefix to all copies of the relocate.c source file.  */
-    set_relocation_prefix (orig_installprefix, curr_prefix);
+    {
+      /* Now pass this prefix to all copies of the relocate.c source file.  */
+      set_relocation_prefix (orig_installprefix, curr_prefix);
+
+      free (curr_prefix);
+    }
 }
 
 /* Set program_name, based on argv[0], and original installation prefix and
--- a/lib/relocatable.c	Mon Jan 07 23:29:52 2008 +0100
+++ b/lib/relocatable.c	Tue Jan 08 00:28:30 2008 +0100
@@ -1,5 +1,5 @@
 /* Provide relocatable packages.
-   Copyright (C) 2003-2006 Free Software Foundation, Inc.
+   Copyright (C) 2003-2006, 2008 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify it
@@ -160,17 +160,18 @@
 /* Convenience function:
    Computes the current installation prefix, based on the original
    installation prefix, the original installation directory of a particular
-   file, and the current pathname of this file.  Returns NULL upon failure.  */
+   file, and the current pathname of this file.
+   Returns it, freshly allocated.  Returns NULL upon failure.  */
 #ifdef IN_LIBRARY
 #define compute_curr_prefix local_compute_curr_prefix
 static
 #endif
-const char *
+char *
 compute_curr_prefix (const char *orig_installprefix,
 		     const char *orig_installdir,
 		     const char *curr_pathname)
 {
-  const char *curr_installdir;
+  char *curr_installdir;
   const char *rel_installdir;
 
   if (curr_pathname == NULL)
@@ -254,8 +255,11 @@
       }
 
     if (rp > rel_installdir)
-      /* Unexpected: The curr_installdir does not end with rel_installdir.  */
-      return NULL;
+      {
+	/* Unexpected: The curr_installdir does not end with rel_installdir.  */
+	free (curr_installdir);
+	return NULL;
+      }
 
     {
       size_t curr_prefix_len = cp - curr_installdir;
@@ -264,11 +268,16 @@
       curr_prefix = (char *) xmalloc (curr_prefix_len + 1);
 #ifdef NO_XMALLOC
       if (curr_prefix == NULL)
-	return NULL;
+	{
+	  free (curr_installdir);
+	  return NULL;
+	}
 #endif
       memcpy (curr_prefix, curr_installdir, curr_prefix_len);
       curr_prefix[curr_prefix_len] = '\0';
 
+      free (curr_installdir);
+
       return curr_prefix;
     }
   }
@@ -420,15 +429,19 @@
 	 orig_prefix.  */
       const char *orig_installprefix = INSTALLPREFIX;
       const char *orig_installdir = INSTALLDIR;
-      const char *curr_prefix_better;
+      char *curr_prefix_better;
 
       curr_prefix_better =
 	compute_curr_prefix (orig_installprefix, orig_installdir,
 			     get_shared_library_fullname ());
-      if (curr_prefix_better == NULL)
-	curr_prefix_better = curr_prefix;
 
-      set_relocation_prefix (orig_installprefix, curr_prefix_better);
+      set_relocation_prefix (orig_installprefix,
+			     curr_prefix_better != NULL
+			     ? curr_prefix_better
+			     : curr_prefix);
+
+      if (curr_prefix_better != NULL)
+	free (curr_prefix_better);
 
       initialized = 1;
     }
--- a/lib/relocatable.h	Mon Jan 07 23:29:52 2008 +0100
+++ b/lib/relocatable.h	Tue Jan 08 00:28:30 2008 +0100
@@ -1,5 +1,5 @@
 /* Provide relocatable packages.
-   Copyright (C) 2003, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2005, 2008 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify it
@@ -59,10 +59,11 @@
 /* Convenience function:
    Computes the current installation prefix, based on the original
    installation prefix, the original installation directory of a particular
-   file, and the current pathname of this file.  Returns NULL upon failure.  */
-extern const char * compute_curr_prefix (const char *orig_installprefix,
-					 const char *orig_installdir,
-					 const char *curr_pathname);
+   file, and the current pathname of this file.
+   Returns it, freshly allocated.  Returns NULL upon failure.  */
+extern char * compute_curr_prefix (const char *orig_installprefix,
+				   const char *orig_installdir,
+				   const char *curr_pathname);
 
 #else