Fri Jun 1 12:08:40 2012 UTC ()
Fix a number of memory leaks. Keep final loop of the cleanup in tic(1)
under #ifdef __VALGRIND__ though.


(joerg)
diff -r1.6 -r1.7 src/lib/libterminfo/compile.c
diff -r1.18 -r1.19 src/usr.bin/tic/tic.c

cvs diff -r1.6 -r1.7 src/lib/libterminfo/compile.c (expand / switch to unified diff)

--- src/lib/libterminfo/compile.c 2011/11/28 12:44:19 1.6
+++ src/lib/libterminfo/compile.c 2012/06/01 12:08:40 1.7
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: compile.c,v 1.6 2011/11/28 12:44:19 joerg Exp $ */ 1/* $NetBSD: compile.c,v 1.7 2012/06/01 12:08:40 joerg Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2009, 2010, 2011 The NetBSD Foundation, Inc. 4 * Copyright (c) 2009, 2010, 2011 The NetBSD Foundation, Inc.
5 * 5 *
6 * This code is derived from software contributed to The NetBSD Foundation 6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples. 7 * by Roy Marples.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30#if HAVE_NBTOOL_CONFIG_H 30#if HAVE_NBTOOL_CONFIG_H
31#include "nbtool_config.h" 31#include "nbtool_config.h"
32#endif 32#endif
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__RCSID("$NetBSD: compile.c,v 1.6 2011/11/28 12:44:19 joerg Exp $"); 35__RCSID("$NetBSD: compile.c,v 1.7 2012/06/01 12:08:40 joerg Exp $");
36 36
37#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H 37#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
38#include <sys/endian.h> 38#include <sys/endian.h>
39#endif 39#endif
40 40
41#include <assert.h> 41#include <assert.h>
42#include <ctype.h> 42#include <ctype.h>
43#include <err.h> 43#include <err.h>
44#include <errno.h> 44#include <errno.h>
45#include <limits.h> 45#include <limits.h>
46#include <stdarg.h> 46#include <stdarg.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <stdint.h> 48#include <stdint.h>
@@ -64,27 +64,27 @@ dowarn(int flags, const char *fmt, ...) @@ -64,27 +64,27 @@ dowarn(int flags, const char *fmt, ...)
64 } 64 }
65} 65}
66 66
67char * 67char *
68_ti_grow_tbuf(TBUF *tbuf, size_t len) 68_ti_grow_tbuf(TBUF *tbuf, size_t len)
69{ 69{
70 char *buf; 70 char *buf;
71 size_t l; 71 size_t l;
72 72
73 _DIAGASSERT(tbuf != NULL); 73 _DIAGASSERT(tbuf != NULL);
74 74
75 l = tbuf->bufpos + len; 75 l = tbuf->bufpos + len;
76 if (l > tbuf->buflen) { 76 if (l > tbuf->buflen) {
77 if (tbuf->bufpos == 0) 77 if (tbuf->buflen == 0)
78 buf = malloc(l); 78 buf = malloc(l);
79 else 79 else
80 buf = realloc(tbuf->buf, l); 80 buf = realloc(tbuf->buf, l);
81 if (buf == NULL) 81 if (buf == NULL)
82 return NULL; 82 return NULL;
83 tbuf->buf = buf; 83 tbuf->buf = buf;
84 tbuf->buflen = l; 84 tbuf->buflen = l;
85 } 85 }
86 return tbuf->buf; 86 return tbuf->buf;
87} 87}
88 88
89char * 89char *
90_ti_find_cap(TBUF *tbuf, char type, short ind) 90_ti_find_cap(TBUF *tbuf, char type, short ind)
@@ -649,19 +649,20 @@ error: @@ -649,19 +649,20 @@ error:
649 free(buf.buf); 649 free(buf.buf);
650 _ti_freetic(tic); 650 _ti_freetic(tic);
651 return NULL; 651 return NULL;
652} 652}
653 653
654void 654void
655_ti_freetic(TIC *tic) 655_ti_freetic(TIC *tic)
656{ 656{
657 657
658 if (tic != NULL) { 658 if (tic != NULL) {
659 free(tic->name); 659 free(tic->name);
660 free(tic->alias); 660 free(tic->alias);
661 free(tic->desc); 661 free(tic->desc);
 662 free(tic->extras.buf);
662 free(tic->flags.buf); 663 free(tic->flags.buf);
663 free(tic->nums.buf); 664 free(tic->nums.buf);
664 free(tic->strs.buf); 665 free(tic->strs.buf);
665 free(tic); 666 free(tic);
666 } 667 }
667} 668}

cvs diff -r1.18 -r1.19 src/usr.bin/tic/tic.c (expand / switch to unified diff)

--- src/usr.bin/tic/tic.c 2012/05/31 21:01:06 1.18
+++ src/usr.bin/tic/tic.c 2012/06/01 12:08:40 1.19
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tic.c,v 1.18 2012/05/31 21:01:06 joerg Exp $ */ 1/* $NetBSD: tic.c,v 1.19 2012/06/01 12:08:40 joerg Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc. 4 * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
5 * 5 *
6 * This code is derived from software contributed to The NetBSD Foundation 6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples. 7 * by Roy Marples.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30#if HAVE_NBTOOL_CONFIG_H 30#if HAVE_NBTOOL_CONFIG_H
31#include "nbtool_config.h" 31#include "nbtool_config.h"
32#endif 32#endif
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__RCSID("$NetBSD: tic.c,v 1.18 2012/05/31 21:01:06 joerg Exp $"); 35__RCSID("$NetBSD: tic.c,v 1.19 2012/06/01 12:08:40 joerg Exp $");
36 36
37#include <sys/types.h> 37#include <sys/types.h>
38#include <sys/queue.h> 38#include <sys/queue.h>
39 39
40#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H 40#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
41#include <sys/endian.h> 41#include <sys/endian.h>
42#endif 42#endif
43 43
44#include <ctype.h> 44#include <ctype.h>
45#include <err.h> 45#include <err.h>
46#include <errno.h> 46#include <errno.h>
47#include <getopt.h> 47#include <getopt.h>
48#include <limits.h> 48#include <limits.h>
@@ -195,26 +195,27 @@ process_entry(TBUF *buf, int flags) @@ -195,26 +195,27 @@ process_entry(TBUF *buf, int flags)
195 e = strchr(p, '|'); 195 e = strchr(p, '|');
196 if (e != NULL) 196 if (e != NULL)
197 *e++ = '\0'; 197 *e++ = '\0';
198 if (find_term(p) != NULL) { 198 if (find_term(p) != NULL) {
199 dowarn("%s: has alias for already assigned" 199 dowarn("%s: has alias for already assigned"
200 " term %s", tic->name, p); 200 " term %s", tic->name, p);
201 } else { 201 } else {
202 term = store_term(p, 'a'); 202 term = store_term(p, 'a');
203 term->tic = ecalloc(sizeof(*term->tic), 1); 203 term->tic = ecalloc(sizeof(*term->tic), 1);
204 term->tic->name = estrdup(tic->name); 204 term->tic->name = estrdup(tic->name);
205 } 205 }
206 p = e; 206 p = e;
207 } 207 }
 208 free(alias);
208 } 209 }
209  210
210 return 0; 211 return 0;
211} 212}
212 213
213static void 214static void
214merge(TIC *rtic, TIC *utic, int flags) 215merge(TIC *rtic, TIC *utic, int flags)
215{ 216{
216 char *cap, flag, *code, type, *str; 217 char *cap, flag, *code, type, *str;
217 short ind, num; 218 short ind, num;
218 size_t n; 219 size_t n;
219 220
220 cap = utic->flags.buf; 221 cap = utic->flags.buf;
@@ -504,53 +505,55 @@ main(int argc, char **argv) @@ -504,53 +505,55 @@ main(int argc, char **argv)
504 if (db == NULL) 505 if (db == NULL)
505 err(1, "dbopen: %s", source); 506 err(1, "dbopen: %s", source);
506 p = dbname + strlen(dbname); 507 p = dbname + strlen(dbname);
507 *p++ = '.'; 508 *p++ = '.';
508 *p++ = 'd'; 509 *p++ = 'd';
509 *p++ = 'b'; 510 *p++ = 'b';
510 *p++ = '\0'; 511 *p++ = '\0';
511 atexit(do_unlink); 512 atexit(do_unlink);
512 } else 513 } else
513 db = NULL; /* satisfy gcc warning */ 514 db = NULL; /* satisfy gcc warning */
514 515
515 hcreate(HASH_SIZE); 516 hcreate(HASH_SIZE);
516 517
517 buf = NULL; 518 buf = tbuf.buf = NULL;
518 buflen = tbuf.buflen = tbuf.bufpos = 0;  519 buflen = tbuf.buflen = tbuf.bufpos = 0;
519 while ((len = getline(&buf, &buflen, f)) != -1) { 520 while ((len = getline(&buf, &buflen, f)) != -1) {
520 /* Skip comments */ 521 /* Skip comments */
521 if (*buf == '#') 522 if (*buf == '#')
522 continue; 523 continue;
523 if (buf[len - 1] != '\n') { 524 if (buf[len - 1] != '\n') {
524 process_entry(&tbuf, flags); 525 process_entry(&tbuf, flags);
525 dowarn("last line is not a comment" 526 dowarn("last line is not a comment"
526 " and does not end with a newline"); 527 " and does not end with a newline");
527 continue; 528 continue;
528 } 529 }
529 /* 530 /*
530 If the first char is space not a space then we have a 531 If the first char is space not a space then we have a
531 new entry, so process it. 532 new entry, so process it.
532 */ 533 */
533 if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0) 534 if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0)
534 process_entry(&tbuf, flags); 535 process_entry(&tbuf, flags);
535  536
536 /* Grow the buffer if needed */ 537 /* Grow the buffer if needed */
537 grow_tbuf(&tbuf, len); 538 grow_tbuf(&tbuf, len);
538 /* Append the string */ 539 /* Append the string */
539 memcpy(tbuf.buf + tbuf.bufpos, buf, len); 540 memcpy(tbuf.buf + tbuf.bufpos, buf, len);
540 tbuf.bufpos += len; 541 tbuf.bufpos += len;
541 } 542 }
 543 free(buf);
542 /* Process the last entry if not done already */ 544 /* Process the last entry if not done already */
543 process_entry(&tbuf, flags); 545 process_entry(&tbuf, flags);
 546 free(tbuf.buf);
544 547
545 /* Merge use entries until we have merged all we can */ 548 /* Merge use entries until we have merged all we can */
546 while (merge_use(flags) != 0) 549 while (merge_use(flags) != 0)
547 ; 550 ;
548 551
549 if (Sflag) { 552 if (Sflag) {
550 print_dump(argc - optind, argv + optind); 553 print_dump(argc - optind, argv + optind);
551 return error_exit; 554 return error_exit;
552 } 555 }
553 556
554 if (cflag) 557 if (cflag)
555 return error_exit; 558 return error_exit;
556 559
@@ -561,19 +564,28 @@ main(int argc, char **argv) @@ -561,19 +564,28 @@ main(int argc, char **argv)
561 /* done! */ 564 /* done! */
562 dbm_close(db); 565 dbm_close(db);
563 566
564 /* Rename the tmp db to the real one now */ 567 /* Rename the tmp db to the real one now */
565 easprintf(&p, "%s.db", ofile); 568 easprintf(&p, "%s.db", ofile);
566 if (rename(dbname, p) == -1) 569 if (rename(dbname, p) == -1)
567 err(1, "rename"); 570 err(1, "rename");
568 free(dbname); 571 free(dbname);
569 dbname = NULL; 572 dbname = NULL;
570 573
571 if (sflag != 0) 574 if (sflag != 0)
572 fprintf(stderr, "%zu entries and %zu aliases written to %s\n", 575 fprintf(stderr, "%zu entries and %zu aliases written to %s\n",
573 nterm, nalias, p); 576 nterm, nalias, p);
574 free(p); 
575 577
 578#ifdef __VALGRIND__
 579 free(p);
 580 while ((term = SLIST_FIRST(&terms)) != NULL) {
 581 SLIST_REMOVE_HEAD(&terms, next);
 582 _ti_freetic(term->tic);
 583 free(term->name);
 584 free(term);
 585 }
576 hdestroy(); 586 hdestroy();
 587#endif
 588
577 589
578 return EXIT_SUCCESS; 590 return EXIT_SUCCESS;
579} 591}