1 Coverity discovered a number of resource leaks in Gnulib.
2 The solution is to backport the following Gnulib commits:
4 commit 0eee3ccaae5bb3d0016a0da8b8e5108767c02748
5 Author: Bruno Haible <bruno@clisp.org>
6 Date: Fri Mar 31 22:41:38 2017 +0200
8 glob: Fix memory leaks.
10 * lib/glob.c (glob): Free allocated memory before returning.
11 Reported by Coverity via Tim Rühsen.
13 commit b1d7f3165ba1c7a44a29017eb80491094aa240ba
14 Author: Bruno Haible <bruno@clisp.org>
15 Date: Fri Mar 31 22:43:35 2017 +0200
17 glob: Fix invalid free() call.
19 * lib/glob.c (glob): Reset malloc_home_dir when assigning a pointer to
20 static storage to home_dir.
21 Reported by Coverity via Tim Rühsen.
23 commit 1540f3441555f756558f3a18e5f68914c0b72227
24 Author: Bruno Haible <bruno@clisp.org>
25 Date: Sat Apr 1 15:15:18 2017 +0200
27 glob: Fix more memory leaks.
29 * lib/glob.c (glob): Free allocated memory before returning.
30 Reported by Coverity via Tim Rühsen.
32 commit b19cb256c9a4d3a138c27181cffee5513edb0e81
33 Author: Bruno Haible <bruno@clisp.org>
34 Date: Thu Jul 6 23:21:49 2017 +0200
36 glob: Fix more memory leaks.
38 * lib/glob.c (glob): Free dirname before returning.
39 Reported by Coverity and Tim Rühsen.
41 commit 8cb994d1fc4a957359780e1a4187b4f250c1cea5
42 Author: Tim Rühsen <tim.ruehsen@gmx.de>
43 Date: Mon Jul 10 19:02:19 2017 +0200
45 glob: Fix more memory leaks.
47 * lib/glob.c (glob): Use 'goto out' in order to free dirname before
49 Reported by Tim Rühsen.
51 diff --git a/gnulib/import/glob.c b/gnulib/import/glob.c
52 index 4b04b90..416d210 100644
53 --- a/gdb/gnulib/import/glob.c
54 +++ b/gdb/gnulib/import/glob.c
55 @@ -734,6 +734,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
56 pwtmpbuf = malloc (pwbuflen);
59 + if (__glibc_unlikely (malloc_name))
61 retval = GLOB_NOSPACE;
64 @@ -762,6 +764,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
67 free (malloc_pwtmpbuf);
68 + if (__glibc_unlikely (malloc_name))
70 retval = GLOB_NOSPACE;
73 @@ -797,23 +801,30 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
76 memcpy (home_dir, p->pw_dir, home_dir_len);
81 + free (malloc_pwtmpbuf);
85 + if (__glibc_unlikely (malloc_name))
89 if (home_dir == NULL || home_dir[0] == '\0')
91 + if (__glibc_unlikely (malloc_home_dir))
93 if (flags & GLOB_TILDE_CHECK)
95 - if (__glibc_unlikely (malloc_home_dir))
97 retval = GLOB_NOMATCH;
101 - home_dir = (char *) "~"; /* No luck. */
103 + home_dir = (char *) "~"; /* No luck. */
104 + malloc_home_dir = 0;
107 # endif /* WINDOWS32 */
109 @@ -855,6 +866,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
111 dirlen += home_len - 1;
112 malloc_dirname = !use_alloca;
114 + if (__glibc_unlikely (malloc_home_dir))
117 dirname_modified = 1;
119 @@ -1027,9 +1041,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
120 free (malloc_pwtmpbuf);
122 if (flags & GLOB_TILDE_CHECK)
123 - /* We have to regard it as an error if we cannot find the
125 - return GLOB_NOMATCH;
127 + /* We have to regard it as an error if we cannot find the
129 + retval = GLOB_NOMATCH;
135 @@ -1059,7 +1076,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
136 free (pglob->gl_pathv);
137 pglob->gl_pathv = NULL;
139 - return GLOB_NOSPACE;
140 + retval = GLOB_NOSPACE;
144 new_gl_pathv = realloc (pglob->gl_pathv,
145 @@ -1077,12 +1095,19 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
146 p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen);
149 + if (__glibc_unlikely (malloc_dirname))
154 - pglob->gl_pathv[newcount] = strdup (dirname);
155 - if (pglob->gl_pathv[newcount] == NULL)
157 + if (__glibc_unlikely (malloc_dirname))
158 + pglob->gl_pathv[newcount] = dirname;
161 + pglob->gl_pathv[newcount] = strdup (dirname);
162 + if (pglob->gl_pathv[newcount] == NULL)
166 pglob->gl_pathv[++newcount] = NULL;
168 @@ -1092,7 +1117,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
172 - return GLOB_NOMATCH;
173 + retval = GLOB_NOMATCH;
177 meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE));
178 @@ -1138,7 +1164,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
181 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
190 @@ -1157,7 +1186,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
194 - return GLOB_ABORTED;
195 + retval = GLOB_ABORTED;
200 @@ -1176,7 +1206,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
209 /* Stick the directory on the front of each name. */
210 @@ -1187,7 +1218,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
214 - return GLOB_NOSPACE;
215 + retval = GLOB_NOSPACE;
220 @@ -1209,7 +1241,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
224 - return GLOB_NOSPACE;
225 + retval = GLOB_NOSPACE;
229 new_gl_pathv = realloc (pglob->gl_pathv,
230 @@ -1224,7 +1257,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
234 - return GLOB_NOSPACE;
235 + retval = GLOB_NOSPACE;
240 @@ -1236,7 +1270,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
244 - return GLOB_NOMATCH;
245 + retval = GLOB_NOMATCH;
250 @@ -1282,7 +1317,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
260 @@ -1294,7 +1330,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
264 - return GLOB_NOSPACE;
265 + retval = GLOB_NOSPACE;
270 @@ -1319,7 +1356,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
274 - return GLOB_NOSPACE;
275 + retval = GLOB_NOSPACE;
278 strcpy (&new[len - 2], "/");
279 pglob->gl_pathv[i] = new;