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