Upgrade to NetBSD: tic.c,v 1.19

The upgrade of tic to v 1.19 fixes a memory corruptions that  was
visible when cross building (specially when using  long  path
names in the output file argument).
This commit is contained in:
Kees Jongenburger 2012-07-23 17:13:18 +00:00
parent 0b4c154160
commit 86344bb535
3 changed files with 68 additions and 54 deletions

View file

@ -43,7 +43,7 @@
2011/01/12 06:17:52,usr.bin/indent 2011/01/12 06:17:52,usr.bin/indent
2010/02/19 16:35:27,usr.bin/sed 2010/02/19 16:35:27,usr.bin/sed
2011/01/15 22:54:10,usr.bin/stat 2011/01/15 22:54:10,usr.bin/stat
2010/02/22 23:05:39,usr.bin/tic 2012/06/01 12:08:40,usr.bin/tic
2010/10/15 05:46:48,usr.bin/mkdep 2010/10/15 05:46:48,usr.bin/mkdep
2010/05/14 17:28:23,usr.bin/newgrp 2010/05/14 17:28:23,usr.bin/newgrp
2010/10/06 07:59:18,usr.bin/uniq 2010/10/06 07:59:18,usr.bin/uniq

View file

@ -6,7 +6,7 @@ WARNS= 4
CPPFLAGS+= -I${.CURDIR}/../../lib/libterminfo CPPFLAGS+= -I${.CURDIR}/../../lib/libterminfo
.ifndef HOSTPROG .ifndef HOSTPROG
LDADD+= -lterminfo LDADD+= -lterminfo -lutil
DPADD+= ${LIBTERMINFO} DPADD+= ${LIBTERMINFO}
.endif .endif

View file

@ -1,4 +1,4 @@
/* $NetBSD: tic.c,v 1.10 2010/02/22 23:05:39 roy Exp $ */ /* $NetBSD: tic.c,v 1.19 2012/06/01 12:08:40 joerg Exp $ */
/* /*
* Copyright (c) 2009, 2010 The NetBSD Foundation, Inc. * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
@ -32,9 +32,10 @@
#endif #endif
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: tic.c,v 1.10 2010/02/22 23:05:39 roy Exp $"); __RCSID("$NetBSD: tic.c,v 1.19 2012/06/01 12:08:40 joerg Exp $");
#include <sys/types.h> #include <sys/types.h>
#include <sys/queue.h>
#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
#include <sys/endian.h> #include <sys/endian.h>
@ -47,27 +48,32 @@ __RCSID("$NetBSD: tic.c,v 1.10 2010/02/22 23:05:39 roy Exp $");
#include <limits.h> #include <limits.h>
#include <fcntl.h> #include <fcntl.h>
#include <ndbm.h> #include <ndbm.h>
#include <search.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <term_private.h> #include <term_private.h>
#include <term.h> #include <term.h>
#include <util.h>
#define HASH_SIZE 16384 /* 2012-06-01: 3600 entries */
/* We store the full list of terminals we have instead of iterating /* We store the full list of terminals we have instead of iterating
through the database as the sequential iterator doesn't work through the database as the sequential iterator doesn't work
the the data size stored changes N amount which ours will. */ the the data size stored changes N amount which ours will. */
typedef struct term { typedef struct term {
struct term *next; SLIST_ENTRY(term) next;
char *name; char *name;
char type; char type;
TIC *tic; TIC *tic;
} TERM; } TERM;
static TERM *terms; static SLIST_HEAD(, term) terms = SLIST_HEAD_INITIALIZER(terms);
static int error_exit; static int error_exit;
static int Sflag; static int Sflag;
static char *dbname; static char *dbname;
static size_t nterm, nalias;
static void static void
do_unlink(void) do_unlink(void)
@ -77,7 +83,7 @@ do_unlink(void)
unlink(dbname); unlink(dbname);
} }
static void __attribute__((__format__(__printf__, 1, 2))) static void __printflike(1, 2)
dowarn(const char *fmt, ...) dowarn(const char *fmt, ...)
{ {
va_list va; va_list va;
@ -123,28 +129,33 @@ save_term(DBM *db, TERM *term)
static TERM * static TERM *
find_term(const char *name) find_term(const char *name)
{ {
TERM *term; ENTRY elem, *elemp;
for (term = terms; term != NULL; term = term->next) elem.key = __UNCONST(name);
if (strcmp(term->name, name) == 0) elem.data = NULL;
return term; elemp = hsearch(elem, FIND);
return NULL; return elemp ? (TERM *)elemp->data : NULL;
} }
static TERM * static TERM *
store_term(const char *name, char type) store_term(const char *name, char type)
{ {
TERM *term; TERM *term;
ENTRY elem;
term = calloc(1, sizeof(*term)); term = ecalloc(1, sizeof(*term));
if (term == NULL) term->name = estrdup(name);
errx(1, "malloc");
term->name = strdup(name);
term->type = type; term->type = type;
if (term->name == NULL) SLIST_INSERT_HEAD(&terms, term, next);
errx(1, "malloc"); elem.key = estrdup(name);
term->next = terms; elem.data = term;
terms = term; hsearch(elem, ENTER);
if (type == 'a')
nalias++;
else
nterm++;
return term; return term;
} }
@ -179,7 +190,7 @@ process_entry(TBUF *buf, int flags)
/* Create aliased terms */ /* Create aliased terms */
if (tic->alias != NULL) { if (tic->alias != NULL) {
alias = p = strdup(tic->alias); alias = p = estrdup(tic->alias);
while (p != NULL && *p != '\0') { while (p != NULL && *p != '\0') {
e = strchr(p, '|'); e = strchr(p, '|');
if (e != NULL) if (e != NULL)
@ -189,15 +200,12 @@ process_entry(TBUF *buf, int flags)
" term %s", tic->name, p); " term %s", tic->name, p);
} else { } else {
term = store_term(p, 'a'); term = store_term(p, 'a');
term->tic = calloc(sizeof(*term->tic), 1); term->tic = ecalloc(sizeof(*term->tic), 1);
if (term->tic == NULL) term->tic->name = estrdup(tic->name);
err(1, "malloc");
term->tic->name = strdup(tic->name);
if (term->tic->name == NULL)
err(1, "malloc");
} }
p = e; p = e;
} }
free(alias);
} }
return 0; return 0;
@ -311,7 +319,7 @@ merge_use(int flags)
TERM *term, *uterm;; TERM *term, *uterm;;
skipped = merged = 0; skipped = merged = 0;
for (term = terms; term != NULL; term = term->next) { SLIST_FOREACH(term, &terms, next) {
if (term->type == 'a') if (term->type == 'a')
continue; continue;
rtic = term->tic; rtic = term->tic;
@ -357,7 +365,7 @@ merge_use(int flags)
cap += sizeof(uint16_t) + num; cap += sizeof(uint16_t) + num;
memn = rtic->extras.bufpos - memn = rtic->extras.bufpos -
(cap - rtic->extras.buf); (cap - rtic->extras.buf);
memcpy(scap, cap, memn); memmove(scap, cap, memn);
rtic->extras.bufpos -= cap - scap; rtic->extras.bufpos -= cap - scap;
cap = scap; cap = scap;
rtic->extras.entries--; rtic->extras.entries--;
@ -443,7 +451,8 @@ main(int argc, char **argv)
char *source, *p, *buf, *ofile; char *source, *p, *buf, *ofile;
FILE *f; FILE *f;
DBM *db; DBM *db;
size_t len, buflen, nterm, nalias; size_t buflen;
ssize_t len;
TBUF tbuf; TBUF tbuf;
TERM *term; TERM *term;
@ -490,9 +499,7 @@ main(int argc, char **argv)
if (ofile == NULL) if (ofile == NULL)
ofile = source; ofile = source;
len = strlen(ofile) + 9; len = strlen(ofile) + 9;
dbname = malloc(len + 4); /* For adding .db after open */ dbname = emalloc(len + 4); /* For adding .db after open */
if (dbname == NULL)
err(1, "malloc");
snprintf(dbname, len, "%s.tmp", ofile); snprintf(dbname, len, "%s.tmp", ofile);
db = dbm_open(dbname, O_CREAT | O_RDWR | O_TRUNC, DEFFILEMODE); db = dbm_open(dbname, O_CREAT | O_RDWR | O_TRUNC, DEFFILEMODE);
if (db == NULL) if (db == NULL)
@ -506,12 +513,15 @@ main(int argc, char **argv)
} else } else
db = NULL; /* satisfy gcc warning */ db = NULL; /* satisfy gcc warning */
tbuf.buflen = tbuf.bufpos = 0; hcreate(HASH_SIZE);
while ((buf = fgetln(f, &buflen)) != NULL) {
buf = tbuf.buf = NULL;
buflen = tbuf.buflen = tbuf.bufpos = 0;
while ((len = getline(&buf, &buflen, f)) != -1) {
/* Skip comments */ /* Skip comments */
if (*buf == '#') if (*buf == '#')
continue; continue;
if (buf[buflen - 1] != '\n') { if (buf[len - 1] != '\n') {
process_entry(&tbuf, flags); process_entry(&tbuf, flags);
dowarn("last line is not a comment" dowarn("last line is not a comment"
" and does not end with a newline"); " and does not end with a newline");
@ -525,13 +535,15 @@ main(int argc, char **argv)
process_entry(&tbuf, flags); process_entry(&tbuf, flags);
/* Grow the buffer if needed */ /* Grow the buffer if needed */
grow_tbuf(&tbuf, buflen); grow_tbuf(&tbuf, len);
/* Append the string */ /* Append the string */
memcpy(tbuf.buf + tbuf.bufpos, buf, buflen); memcpy(tbuf.buf + tbuf.bufpos, buf, len);
tbuf.bufpos += buflen; tbuf.bufpos += len;
} }
free(buf);
/* Process the last entry if not done already */ /* Process the last entry if not done already */
process_entry(&tbuf, flags); process_entry(&tbuf, flags);
free(tbuf.buf);
/* Merge use entries until we have merged all we can */ /* Merge use entries until we have merged all we can */
while (merge_use(flags) != 0) while (merge_use(flags) != 0)
@ -546,24 +558,14 @@ main(int argc, char **argv)
return error_exit; return error_exit;
/* Save the terms */ /* Save the terms */
nterm = nalias = 0; SLIST_FOREACH(term, &terms, next)
for (term = terms; term != NULL; term = term->next) {
save_term(db, term); save_term(db, term);
if (term->type == 'a')
nalias++;
else
nterm++;
}
/* done! */ /* done! */
dbm_close(db); dbm_close(db);
/* Rename the tmp db to the real one now */ /* Rename the tmp db to the real one now */
len = strlen(ofile) + 4; easprintf(&p, "%s.db", ofile);
p = malloc(len);
if (p == NULL)
err(1, "malloc");
snprintf(p, len, "%s.db", ofile);
if (rename(dbname, p) == -1) if (rename(dbname, p) == -1)
err(1, "rename"); err(1, "rename");
free(dbname); free(dbname);
@ -573,5 +575,17 @@ main(int argc, char **argv)
fprintf(stderr, "%zu entries and %zu aliases written to %s\n", fprintf(stderr, "%zu entries and %zu aliases written to %s\n",
nterm, nalias, p); nterm, nalias, p);
#ifdef __VALGRIND__
free(p);
while ((term = SLIST_FIRST(&terms)) != NULL) {
SLIST_REMOVE_HEAD(&terms, next);
_ti_freetic(term->tic);
free(term->name);
free(term);
}
hdestroy();
#endif
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }