This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[patch 1/4] wordexp: dont leak file streams
- From: Peter Rosin <peda at lysator dot liu dot se>
- To: newlib at sourceware dot org
- Date: Fri, 14 Sep 2012 01:04:55 +0200
- Subject: [patch 1/4] wordexp: dont leak file streams
- References: <20120913230454.392673700@lysator.liu.se>
2012-09-13 Peter Rosin <peda@lysator.liu.se>
* libc/posix/wordexp.c (wordexp): Don't leak file streams.
Index: newlib/libc/posix/wordexp.c
===================================================================
--- newlib.orig/libc/posix/wordexp.c
+++ newlib/libc/posix/wordexp.c
@@ -29,8 +29,8 @@
int
wordexp(const char *words, wordexp_t *pwordexp, int flags)
{
- FILE *f;
- FILE *f_err;
+ FILE *f = NULL;
+ FILE *f_err = NULL;
char tmp[MAXLINELEN];
int i = 0;
int offs = 0;
@@ -143,8 +143,14 @@ wordexp(const char *words, wordexp_t *pw
pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = NULL;
pwordexp->we_wordc += num_words;
- close(fd[0]);
- close(fd_err[0]);
+ if (f)
+ fclose(f);
+ else
+ close(fd[0]);
+ if (f_err)
+ fclose(f_err);
+ else
+ close(fd_err[0]);
/* Wait for child to finish. */
waitpid (pid, NULL, 0);