f976bb9e1a6093d78f524d2ba0471d16615deba1
[deliverable/binutils-gdb.git] / gnulib / patches / 0003-Fix-glob-c-Coverity-issues.patch
1 Coverity discovered a number of resource leaks in Gnulib.
2 The solution is to backport the following Gnulib commits:
3
4 commit 0eee3ccaae5bb3d0016a0da8b8e5108767c02748
5 Author: Bruno Haible <bruno@clisp.org>
6 Date: Fri Mar 31 22:41:38 2017 +0200
7
8 glob: Fix memory leaks.
9
10 * lib/glob.c (glob): Free allocated memory before returning.
11 Reported by Coverity via Tim Rühsen.
12
13 commit b1d7f3165ba1c7a44a29017eb80491094aa240ba
14 Author: Bruno Haible <bruno@clisp.org>
15 Date: Fri Mar 31 22:43:35 2017 +0200
16
17 glob: Fix invalid free() call.
18
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.
22
23 commit 1540f3441555f756558f3a18e5f68914c0b72227
24 Author: Bruno Haible <bruno@clisp.org>
25 Date: Sat Apr 1 15:15:18 2017 +0200
26
27 glob: Fix more memory leaks.
28
29 * lib/glob.c (glob): Free allocated memory before returning.
30 Reported by Coverity via Tim Rühsen.
31
32 commit b19cb256c9a4d3a138c27181cffee5513edb0e81
33 Author: Bruno Haible <bruno@clisp.org>
34 Date: Thu Jul 6 23:21:49 2017 +0200
35
36 glob: Fix more memory leaks.
37
38 * lib/glob.c (glob): Free dirname before returning.
39 Reported by Coverity and Tim Rühsen.
40
41 commit 8cb994d1fc4a957359780e1a4187b4f250c1cea5
42 Author: Tim Rühsen <tim.ruehsen@gmx.de>
43 Date: Mon Jul 10 19:02:19 2017 +0200
44
45 glob: Fix more memory leaks.
46
47 * lib/glob.c (glob): Use 'goto out' in order to free dirname before
48 returning.
49 Reported by Tim Rühsen.
50
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);
57 if (pwtmpbuf == NULL)
58 {
59 + if (__glibc_unlikely (malloc_name))
60 + free (name);
61 retval = GLOB_NOSPACE;
62 goto out;
63 }
64 @@ -762,6 +764,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
65 if (newp == NULL)
66 {
67 free (malloc_pwtmpbuf);
68 + if (__glibc_unlikely (malloc_name))
69 + free (name);
70 retval = GLOB_NOSPACE;
71 goto out;
72 }
73 @@ -797,23 +801,30 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
74 malloc_home_dir = 1;
75 }
76 memcpy (home_dir, p->pw_dir, home_dir_len);
77 -
78 - free (pwtmpbuf);
79 }
80 }
81 + free (malloc_pwtmpbuf);
82 + }
83 + else
84 + {
85 + if (__glibc_unlikely (malloc_name))
86 + free (name);
87 }
88 }
89 if (home_dir == NULL || home_dir[0] == '\0')
90 {
91 + if (__glibc_unlikely (malloc_home_dir))
92 + free (home_dir);
93 if (flags & GLOB_TILDE_CHECK)
94 {
95 - if (__glibc_unlikely (malloc_home_dir))
96 - free (home_dir);
97 retval = GLOB_NOMATCH;
98 goto out;
99 }
100 else
101 - home_dir = (char *) "~"; /* No luck. */
102 + {
103 + home_dir = (char *) "~"; /* No luck. */
104 + malloc_home_dir = 0;
105 + }
106 }
107 # endif /* WINDOWS32 */
108 # endif
109 @@ -855,6 +866,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
110 dirname = newp;
111 dirlen += home_len - 1;
112 malloc_dirname = !use_alloca;
113 +
114 + if (__glibc_unlikely (malloc_home_dir))
115 + free (home_dir);
116 }
117 dirname_modified = 1;
118 }
119 @@ -1027,9 +1041,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
120 free (malloc_pwtmpbuf);
121
122 if (flags & GLOB_TILDE_CHECK)
123 - /* We have to regard it as an error if we cannot find the
124 - home directory. */
125 - return GLOB_NOMATCH;
126 + {
127 + /* We have to regard it as an error if we cannot find the
128 + home directory. */
129 + retval = GLOB_NOMATCH;
130 + goto out;
131 + }
132 }
133 }
134 }
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;
138 pglob->gl_pathc = 0;
139 - return GLOB_NOSPACE;
140 + retval = GLOB_NOSPACE;
141 + goto out;
142 }
143
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);
147 p[0] = '/';
148 p[1] = '\0';
149 + if (__glibc_unlikely (malloc_dirname))
150 + free (dirname);
151 }
152 else
153 {
154 - pglob->gl_pathv[newcount] = strdup (dirname);
155 - if (pglob->gl_pathv[newcount] == NULL)
156 - goto nospace;
157 + if (__glibc_unlikely (malloc_dirname))
158 + pglob->gl_pathv[newcount] = dirname;
159 + else
160 + {
161 + pglob->gl_pathv[newcount] = strdup (dirname);
162 + if (pglob->gl_pathv[newcount] == NULL)
163 + goto nospace;
164 + }
165 }
166 pglob->gl_pathv[++newcount] = NULL;
167 ++pglob->gl_pathc;
168 @@ -1092,7 +1117,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
169 }
170
171 /* Not found. */
172 - return GLOB_NOMATCH;
173 + retval = GLOB_NOMATCH;
174 + goto out;
175 }
176
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),
179 if (status != 0)
180 {
181 if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH)
182 - return status;
183 + {
184 + retval = status;
185 + goto out;
186 + }
187 goto no_matches;
188 }
189
190 @@ -1157,7 +1186,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
191 if (interrupt_state)
192 {
193 globfree (&dirs);
194 - return GLOB_ABORTED;
195 + retval = GLOB_ABORTED;
196 + goto out;
197 }
198 }
199 #endif /* SHELL. */
200 @@ -1176,7 +1206,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
201 globfree (&dirs);
202 globfree (pglob);
203 pglob->gl_pathc = 0;
204 - return status;
205 + retval = status;
206 + goto out;
207 }
208
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),
211 globfree (&dirs);
212 globfree (pglob);
213 pglob->gl_pathc = 0;
214 - return GLOB_NOSPACE;
215 + retval = GLOB_NOSPACE;
216 + goto out;
217 }
218 }
219
220 @@ -1209,7 +1241,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
221 {
222 nospace2:
223 globfree (&dirs);
224 - return GLOB_NOSPACE;
225 + retval = GLOB_NOSPACE;
226 + goto out;
227 }
228
229 new_gl_pathv = realloc (pglob->gl_pathv,
230 @@ -1224,7 +1257,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
231 globfree (&dirs);
232 globfree (pglob);
233 pglob->gl_pathc = 0;
234 - return GLOB_NOSPACE;
235 + retval = GLOB_NOSPACE;
236 + goto out;
237 }
238
239 ++pglob->gl_pathc;
240 @@ -1236,7 +1270,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
241 else
242 {
243 globfree (&dirs);
244 - return GLOB_NOMATCH;
245 + retval = GLOB_NOMATCH;
246 + goto out;
247 }
248 }
249
250 @@ -1282,7 +1317,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
251 flags = orig_flags;
252 goto no_matches;
253 }
254 - return status;
255 + retval = status;
256 + goto out;
257 }
258
259 if (dirlen > 0)
260 @@ -1294,7 +1330,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
261 {
262 globfree (pglob);
263 pglob->gl_pathc = 0;
264 - return GLOB_NOSPACE;
265 + retval = GLOB_NOSPACE;
266 + goto out;
267 }
268 }
269 }
270 @@ -1319,7 +1356,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
271 {
272 globfree (pglob);
273 pglob->gl_pathc = 0;
274 - return GLOB_NOSPACE;
275 + retval = GLOB_NOSPACE;
276 + goto out;
277 }
278 strcpy (&new[len - 2], "/");
279 pglob->gl_pathv[i] = new;
This page took 0.03546 seconds and 3 git commands to generate.