ce426f
commit cc8dcf96e71dd643f929e32150904cd6ad69efa8
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Mon Apr 27 15:41:03 2015 +0200
ce426f
ce426f
    test-skeleton: Support temporary files without memory leaks [BZ#18333]
ce426f
    
ce426f
    add_temp_file now makes a copy which is freed by delete_temp_files.
ce426f
    Callers to create_temp_file can now free the returned file name to
ce426f
    avoid the memory leak.  These changes do not affect the leak behavior
ce426f
    of existing code.
ce426f
ce426f
    Also address a NULL pointer derefence in tzset after a memory allocation
ce426f
    failure, found during testing.
ce426f
ce426f
diff --git a/test-skeleton.c b/test-skeleton.c
ce426f
index 7a8ddfa..43fc236 100644
ce426f
--- a/test-skeleton.c
ce426f
+++ b/test-skeleton.c
ce426f
@@ -73,7 +73,7 @@ static const char *test_dir;
ce426f
 struct temp_name_list
ce426f
 {
ce426f
   struct qelem q;
ce426f
-  const char *name;
ce426f
+  char *name;
ce426f
 } *temp_name_list;
ce426f
 
ce426f
 /* Add temporary files in list.  */
ce426f
@@ -83,14 +83,17 @@ add_temp_file (const char *name)
ce426f
 {
ce426f
   struct temp_name_list *newp
ce426f
     = (struct temp_name_list *) calloc (sizeof (*newp), 1);
ce426f
-  if (newp != NULL)
ce426f
+  char *newname = strdup (name);
ce426f
+  if (newp != NULL && newname != NULL)
ce426f
     {
ce426f
-      newp->name = name;
ce426f
+      newp->name = newname;
ce426f
       if (temp_name_list == NULL)
ce426f
 	temp_name_list = (struct temp_name_list *) &newp->q;
ce426f
       else
ce426f
 	insque (newp, temp_name_list);
ce426f
     }
ce426f
+  else
ce426f
+    free (newp);
ce426f
 }
ce426f
 
ce426f
 /* Delete all temporary files.  */
ce426f
@@ -100,11 +103,19 @@ delete_temp_files (void)
ce426f
   while (temp_name_list != NULL)
ce426f
     {
ce426f
       remove (temp_name_list->name);
ce426f
-      temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
ce426f
+      free (temp_name_list->name);
ce426f
+
ce426f
+      struct temp_name_list *next
ce426f
+	= (struct temp_name_list *) temp_name_list->q.q_forw;
ce426f
+      free (temp_name_list);
ce426f
+      temp_name_list = next;
ce426f
     }
ce426f
 }
ce426f
 
ce426f
-/* Create a temporary file.  */
ce426f
+/* Create a temporary file.  Return the opened file descriptor on
ce426f
+   success, or -1 on failure.  Write the file name to *FILENAME if
ce426f
+   FILENAME is not NULL.  In this case, the caller is expected to free
ce426f
+   *FILENAME.  */
ce426f
 static int
ce426f
 __attribute__ ((unused))
ce426f
 create_temp_file (const char *base, char **filename)
ce426f
@@ -132,6 +143,8 @@ create_temp_file (const char *base, char **filename)
ce426f
   add_temp_file (fname);
ce426f
   if (filename != NULL)
ce426f
     *filename = fname;
ce426f
+  else
ce426f
+    free (fname);
ce426f
 
ce426f
   return fd;
ce426f
 }
ce426f
diff --git a/time/tzset.c b/time/tzset.c
ce426f
--- a/time/tzset.c
ce426f
+++ b/time/tzset.c
ce426f
@@ -202,7 +202,10 @@
ce426f
 	return false;
ce426f
     }
ce426f
 
ce426f
-  tz_rules[whichrule].name = __tzstring_len (start, len);
ce426f
+  const char *name = __tzstring_len (start, len);
ce426f
+  if (name == NULL)
ce426f
+    return false;
ce426f
+  tz_rules[whichrule].name = name;
ce426f
 
ce426f
   *tzp = p;
ce426f
   return true;