diff options
author | Florian Weimer <fweimer@redhat.com> | 2015-04-27 15:41:03 +0200 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2015-04-27 16:19:55 +0200 |
commit | cc8dcf96e71dd643f929e32150904cd6ad69efa8 (patch) | |
tree | 0711c094ad29df752a51ffff7120f7b48a945dfd /test-skeleton.c | |
parent | 2dd6ee79b19ccfdd7f68cd534b8b71f77479132b (diff) | |
download | glibc-cc8dcf96e71dd643f929e32150904cd6ad69efa8.tar.gz glibc-cc8dcf96e71dd643f929e32150904cd6ad69efa8.tar.xz glibc-cc8dcf96e71dd643f929e32150904cd6ad69efa8.zip |
test-skeleton: Support temporary files without memory leaks [BZ#18333]
add_temp_file now makes a copy which is freed by delete_temp_files. Callers to create_temp_file can now free the returned file name to avoid the memory leak. These changes do not affect the leak behavior of existing code. Also address a NULL pointer derefence in tzset after a memoru allocation failure, found during testing.
Diffstat (limited to 'test-skeleton.c')
-rw-r--r-- | test-skeleton.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/test-skeleton.c b/test-skeleton.c index 7a8ddfa55b..43fc236fc3 100644 --- a/test-skeleton.c +++ b/test-skeleton.c @@ -73,7 +73,7 @@ static const char *test_dir; struct temp_name_list { struct qelem q; - const char *name; + char *name; } *temp_name_list; /* Add temporary files in list. */ @@ -83,14 +83,17 @@ add_temp_file (const char *name) { struct temp_name_list *newp = (struct temp_name_list *) calloc (sizeof (*newp), 1); - if (newp != NULL) + char *newname = strdup (name); + if (newp != NULL && newname != NULL) { - newp->name = name; + newp->name = newname; if (temp_name_list == NULL) temp_name_list = (struct temp_name_list *) &newp->q; else insque (newp, temp_name_list); } + else + free (newp); } /* Delete all temporary files. */ @@ -100,11 +103,19 @@ delete_temp_files (void) while (temp_name_list != NULL) { remove (temp_name_list->name); - temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw; + free (temp_name_list->name); + + struct temp_name_list *next + = (struct temp_name_list *) temp_name_list->q.q_forw; + free (temp_name_list); + temp_name_list = next; } } -/* Create a temporary file. */ +/* Create a temporary file. Return the opened file descriptor on + success, or -1 on failure. Write the file name to *FILENAME if + FILENAME is not NULL. In this case, the caller is expected to free + *FILENAME. */ static int __attribute__ ((unused)) create_temp_file (const char *base, char **filename) @@ -132,6 +143,8 @@ create_temp_file (const char *base, char **filename) add_temp_file (fname); if (filename != NULL) *filename = fname; + else + free (fname); return fd; } |