Tue Jan 4 23:34:06 2011 UTC ()
Fix file descriptor leak. Found by cppcheck.


(wiz)
diff -r1.47 -r1.48 src/sbin/ldconfig/ldconfig.c

cvs diff -r1.47 -r1.48 src/sbin/ldconfig/ldconfig.c (expand / switch to unified diff)

--- src/sbin/ldconfig/ldconfig.c 2010/05/13 17:52:12 1.47
+++ src/sbin/ldconfig/ldconfig.c 2011/01/04 23:34:06 1.48
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ldconfig.c,v 1.47 2010/05/13 17:52:12 tnozaki Exp $ */ 1/* $NetBSD: ldconfig.c,v 1.48 2011/01/04 23:34:06 wiz Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg. 8 * by Paul Kranenburg.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32 32
33#ifndef lint 33#ifndef lint
34__RCSID("$NetBSD: ldconfig.c,v 1.47 2010/05/13 17:52:12 tnozaki Exp $"); 34__RCSID("$NetBSD: ldconfig.c,v 1.48 2011/01/04 23:34:06 wiz Exp $");
35#endif 35#endif
36 36
37 37
38#include <sys/param.h> 38#include <sys/param.h>
39#include <sys/types.h> 39#include <sys/types.h>
40#include <sys/stat.h> 40#include <sys/stat.h>
41#include <sys/file.h> 41#include <sys/file.h>
42#include <sys/time.h> 42#include <sys/time.h>
43#include <sys/mman.h> 43#include <sys/mman.h>
44#include <sys/exec_aout.h> 44#include <sys/exec_aout.h>
45#include <a.out.h> 45#include <a.out.h>
46#include <ctype.h> 46#include <ctype.h>
47#include <dirent.h> 47#include <dirent.h>
@@ -457,60 +457,62 @@ buildhints(void) @@ -457,60 +457,62 @@ buildhints(void)
457 if (str_index != strtab_sz) { 457 if (str_index != strtab_sz) {
458 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 458 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
459 } 459 }
460 460
461 tempfile = concat(_PATH_LD_HINTS, ".XXXXXX", ""); 461 tempfile = concat(_PATH_LD_HINTS, ".XXXXXX", "");
462 if ((fd = mkstemp(tempfile)) == -1) { 462 if ((fd = mkstemp(tempfile)) == -1) {
463 warn("%s", tempfile); 463 warn("%s", tempfile);
464 goto out; 464 goto out;
465 } 465 }
466 466
467 if (write(fd, &hdr, sizeof(struct hints_header)) != 467 if (write(fd, &hdr, sizeof(struct hints_header)) !=
468 sizeof(struct hints_header)) { 468 sizeof(struct hints_header)) {
469 warn("%s", _PATH_LD_HINTS); 469 warn("%s", _PATH_LD_HINTS);
470 goto out; 470 goto fdout;
471 } 471 }
472 if ((size_t)write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 472 if ((size_t)write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
473 hdr.hh_nbucket * sizeof(struct hints_bucket)) { 473 hdr.hh_nbucket * sizeof(struct hints_bucket)) {
474 warn("%s", _PATH_LD_HINTS); 474 warn("%s", _PATH_LD_HINTS);
475 goto out; 475 goto fdout;
476 } 476 }
477 if (write(fd, strtab, strtab_sz) != strtab_sz) { 477 if (write(fd, strtab, strtab_sz) != strtab_sz) {
478 warn("%s", _PATH_LD_HINTS); 478 warn("%s", _PATH_LD_HINTS);
479 goto out; 479 goto fdout;
480 } 480 }
481 if (fchmod(fd, 0444) == -1) { 481 if (fchmod(fd, 0444) == -1) {
482 warn("%s", _PATH_LD_HINTS); 482 warn("%s", _PATH_LD_HINTS);
483 goto out; 483 goto fdout;
484 } 484 }
485 if (close(fd) != 0) { 485 if (close(fd) != 0) {
486 warn("%s", _PATH_LD_HINTS); 486 warn("%s", _PATH_LD_HINTS);
487 goto out; 487 goto out;
488 } 488 }
489 489
490 /* Install it */ 490 /* Install it */
491 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) { 491 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
492 warn("%s", _PATH_LD_HINTS); 492 warn("%s", _PATH_LD_HINTS);
493 goto out; 493 goto out;
494 } 494 }
495 495
496 if (rename(tempfile, _PATH_LD_HINTS) != 0) { 496 if (rename(tempfile, _PATH_LD_HINTS) != 0) {
497 warn("%s", _PATH_LD_HINTS); 497 warn("%s", _PATH_LD_HINTS);
498 goto out; 498 goto out;
499 } 499 }
500 500
501 free(blist); 501 free(blist);
502 free(strtab); 502 free(strtab);
503 return 0; 503 return 0;
 504fdout:
 505 (void)close(fd);
504out: 506out:
505 free(blist); 507 free(blist);
506 free(strtab); 508 free(strtab);
507 return -1; 509 return -1;
508} 510}
509 511
510static int 512static int
511readhints(void) 513readhints(void)
512{ 514{
513 int fd; 515 int fd;
514 void *addr = (void *) -1; 516 void *addr = (void *) -1;
515 size_t msize = 0; 517 size_t msize = 0;
516 struct hints_header *hdr; 518 struct hints_header *hdr;