Thu Mar 9 11:39:41 2017 UTC ()
Remove redundant null check before free. it is safe to free(NULL)
While here, clear up odd whitespace issue. NFC


(maya)
diff -r1.4 -r1.5 src/lib/libc/gen/extattr.c

cvs diff -r1.4 -r1.5 src/lib/libc/gen/extattr.c (expand / switch to unified diff)

--- src/lib/libc/gen/extattr.c 2012/03/13 21:13:34 1.4
+++ src/lib/libc/gen/extattr.c 2017/03/09 11:39:41 1.5
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: extattr.c,v 1.4 2012/03/13 21:13:34 christos Exp $ */ 1/* $NetBSD: extattr.c,v 1.5 2017/03/09 11:39:41 maya Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2001 Robert N. M. Watson 4 * Copyright (c) 2001 Robert N. M. Watson
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * TrustedBSD: Utility functions for extended attributes. 30 * TrustedBSD: Utility functions for extended attributes.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35__RCSID("$NetBSD: extattr.c,v 1.4 2012/03/13 21:13:34 christos Exp $"); 35__RCSID("$NetBSD: extattr.c,v 1.5 2017/03/09 11:39:41 maya Exp $");
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include "namespace.h" 38#include "namespace.h"
39#include <sys/types.h> 39#include <sys/types.h>
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/extattr.h> 41#include <sys/extattr.h>
42 42
43#include <errno.h> 43#include <errno.h>
44#include <unistd.h> 44#include <unistd.h>
45#include <stdlib.h> 45#include <stdlib.h>
46#include <string.h> 46#include <string.h>
47 47
48const int extattr_namespaces[] = { 48const int extattr_namespaces[] = {
@@ -140,42 +140,39 @@ extattr_copy_fd(int from_fd, int to_fd,  @@ -140,42 +140,39 @@ extattr_copy_fd(int from_fd, int to_fd,
140 aname[alen] = '\0'; 140 aname[alen] = '\0';
141 141
142 vlen = extattr_get_fd(from_fd, namespace, aname, NULL, 0); 142 vlen = extattr_get_fd(from_fd, namespace, aname, NULL, 0);
143 if (vlen == -1) 143 if (vlen == -1)
144 goto out; 144 goto out;
145 145
146 if (vlen > maxvlen) { 146 if (vlen > maxvlen) {
147 if ((aval = realloc(aval, (size_t)vlen)) == NULL) 147 if ((aval = realloc(aval, (size_t)vlen)) == NULL)
148 goto out; 148 goto out;
149 maxvlen = vlen; 149 maxvlen = vlen;
150 } 150 }
151 151
152 if ((vlen = extattr_get_fd(from_fd, namespace, aname, 152 if ((vlen = extattr_get_fd(from_fd, namespace, aname,
153 aval, (size_t)vlen)) == -1) 153 aval, (size_t)vlen)) == -1)
154 goto out; 154 goto out;
155  155
156 if (extattr_set_fd(to_fd, namespace, aname, 156 if (extattr_set_fd(to_fd, namespace, aname,
157 aval, (size_t)vlen) != vlen) 157 aval, (size_t)vlen) != vlen)
158 goto out; 158 goto out;
159 } 159 }
160 160
161 error = 0; 161 error = 0;
162out: 162out:
163 if (aval != NULL) 163 free(aval);
164 free(aval); 164 free(alist);
165  165
166 if (alist != NULL) 
167 free(alist); 
168  
169 return error; 166 return error;
170} 167}
171 168
172int 169int
173extattr_copy_file(const char *from, const char *to, int namespace) 170extattr_copy_file(const char *from, const char *to, int namespace)
174{ 171{
175 ssize_t llen, vlen, maxvlen; 172 ssize_t llen, vlen, maxvlen;
176 size_t alen; 173 size_t alen;
177 void *alist = NULL; 174 void *alist = NULL;
178 void *aval = NULL; 175 void *aval = NULL;
179 size_t i; 176 size_t i;
180 int error = -1; 177 int error = -1;
181 178
@@ -212,42 +209,40 @@ extattr_copy_file(const char *from, cons @@ -212,42 +209,40 @@ extattr_copy_file(const char *from, cons
212 (void)memcpy(aname, ap, alen); 209 (void)memcpy(aname, ap, alen);
213 aname[alen] = '\0'; 210 aname[alen] = '\0';
214 211
215 vlen = extattr_get_file(from, namespace, aname, NULL, 0); 212 vlen = extattr_get_file(from, namespace, aname, NULL, 0);
216 if (vlen == -1) 213 if (vlen == -1)
217 goto out; 214 goto out;
218 215
219 if (vlen > maxvlen) { 216 if (vlen > maxvlen) {
220 if ((aval = realloc(aval, (size_t)vlen)) == NULL) 217 if ((aval = realloc(aval, (size_t)vlen)) == NULL)
221 goto out; 218 goto out;
222 maxvlen = vlen; 219 maxvlen = vlen;
223 } 220 }
224 221
225 if ((vlen = extattr_get_file(from, namespace, aname, aval, (size_t)vlen)) == -1) 222 if ((vlen = extattr_get_file(from, namespace, aname,
 223 aval, (size_t)vlen)) == -1)
226 goto out; 224 goto out;
227  225
228 if (extattr_set_file(to, namespace, aname, 226 if (extattr_set_file(to, namespace, aname,
229 aval, (size_t)vlen) != vlen) 227 aval, (size_t)vlen) != vlen)
230 goto out; 228 goto out;
231 } 229 }
232 230
233 error = 0; 231 error = 0;
234out: 232out:
235 if (aval != NULL) 233 free(aval);
236 free(aval); 234 free(alist);
237  235
238 if (alist != NULL) 
239 free(alist); 
240  
241 return error; 236 return error;
242} 237}
243 238
244int 239int
245extattr_copy_link(const char *from, const char *to, int namespace) 240extattr_copy_link(const char *from, const char *to, int namespace)
246{ 241{
247 ssize_t llen, vlen, maxvlen; 242 ssize_t llen, vlen, maxvlen;
248 size_t alen; 243 size_t alen;
249 void *alist = NULL; 244 void *alist = NULL;
250 void *aval = NULL; 245 void *aval = NULL;
251 size_t i; 246 size_t i;
252 int error = -1; 247 int error = -1;
253 248
@@ -295,32 +290,29 @@ extattr_copy_link(const char *from, cons @@ -295,32 +290,29 @@ extattr_copy_link(const char *from, cons
295 } 290 }
296 291
297 if ((vlen = extattr_get_link(from, namespace, aname, 292 if ((vlen = extattr_get_link(from, namespace, aname,
298 aval, (size_t)vlen)) == -1) 293 aval, (size_t)vlen)) == -1)
299 goto out; 294 goto out;
300  295
301 if (extattr_set_link(to, namespace, aname, 296 if (extattr_set_link(to, namespace, aname,
302 aval, (size_t)vlen) != vlen) 297 aval, (size_t)vlen) != vlen)
303 goto out; 298 goto out;
304 } 299 }
305 300
306 error = 0; 301 error = 0;
307out: 302out:
308 if (aval != NULL) 303 free(aval);
309 free(aval); 304 free(alist);
310  305
311 if (alist != NULL) 
312 free(alist); 
313  
314 return error; 306 return error;
315} 307}
316 308
317static int 309static int
318extattr_namespace_access(int namespace, int mode) 310extattr_namespace_access(int namespace, int mode)
319{ 311{
320 switch (namespace) { 312 switch (namespace) {
321 case EXTATTR_NAMESPACE_SYSTEM: 313 case EXTATTR_NAMESPACE_SYSTEM:
322 if ((mode & (R_OK|W_OK)) && getuid() != 0) 314 if ((mode & (R_OK|W_OK)) && getuid() != 0)
323 return -1; 315 return -1;
324 break; 316 break;
325 default: 317 default:
326 break; 318 break;