Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/getcap.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libc/gen/getcap.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.30 retrieving revision 1.43 diff -u -p -r1.30 -r1.43 --- src/lib/libc/gen/getcap.c 1999/09/16 11:44:57 1.30 +++ src/lib/libc/gen/getcap.c 2006/03/19 02:12:09 1.43 @@ -1,4 +1,4 @@ -/* $NetBSD: getcap.c,v 1.30 1999/09/16 11:44:57 lukem Exp $ */ +/* $NetBSD: getcap.c,v 1.43 2006/03/19 02:12:09 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -36,21 +32,30 @@ * SUCH DAMAGE. */ +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + #include #if defined(LIBC_SCCS) && !defined(lint) #if 0 static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; #else -__RCSID("$NetBSD: getcap.c,v 1.30 1999/09/16 11:44:57 lukem Exp $"); +__RCSID("$NetBSD: getcap.c,v 1.43 2006/03/19 02:12:09 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ +#ifndef SMALL #include "namespace.h" +#endif #include +#include #include #include +#ifndef SMALL #include +#endif #include #include #include @@ -60,16 +65,16 @@ __RCSID("$NetBSD: getcap.c,v 1.30 1999/0 #include #ifdef __weak_alias -__weak_alias(cgetcap,_cgetcap); -__weak_alias(cgetclose,_cgetclose); -__weak_alias(cgetent,_cgetent); -__weak_alias(cgetfirst,_cgetfirst); -__weak_alias(cgetmatch,_cgetmatch); -__weak_alias(cgetnext,_cgetnext); -__weak_alias(cgetnum,_cgetnum); -__weak_alias(cgetset,_cgetset); -__weak_alias(cgetstr,_cgetstr); -__weak_alias(cgetustr,_cgetustr); +__weak_alias(cgetcap,_cgetcap) +__weak_alias(cgetclose,_cgetclose) +__weak_alias(cgetent,_cgetent) +__weak_alias(cgetfirst,_cgetfirst) +__weak_alias(cgetmatch,_cgetmatch) +__weak_alias(cgetnext,_cgetnext) +__weak_alias(cgetnum,_cgetnum) +__weak_alias(cgetset,_cgetset) +__weak_alias(cgetstr,_cgetstr) +__weak_alias(cgetustr,_cgetustr) #endif #define BFRAG 1024 @@ -86,9 +91,12 @@ static size_t topreclen; /* toprec leng static char *toprec; /* Additional record specified by cgetset() */ static int gottoprec; /* Flag indicating retrieval of toprecord */ -static int cdbget __P((DB *, char **, const char *)); -static int getent __P((char **, size_t *, char **, int, const char *, int, char *)); -static int nfcmp __P((char *, char *)); +#ifndef SMALL +static int cdbget(DB *, char **, const char *); +#endif +static int getent(char **, size_t *, const char * const *, int, + const char *, int, char *); +static int nfcmp(char *, char *); /* * Cgetset() allows the addition of a user specified buffer to be added @@ -96,8 +104,7 @@ static int nfcmp __P((char *, char *)); * virtual database. 0 is returned on success, -1 on failure. */ int -cgetset(ent) - const char *ent; +cgetset(const char *ent) { const char *source, *check; char *dest; @@ -160,10 +167,6 @@ cgetcap(buf, cap, type) _DIAGASSERT(buf != NULL); _DIAGASSERT(cap != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || cap == NULL) - return (NULL); -#endif bp = buf; for (;;) { @@ -211,21 +214,13 @@ cgetcap(buf, cap, type) * reference loop is detected. */ int -cgetent(buf, db_array, name) - char **buf, **db_array; - const char *name; +cgetent(char **buf, const char * const *db_array, const char *name) { size_t dummy; _DIAGASSERT(buf != NULL); _DIAGASSERT(db_array != NULL); _DIAGASSERT(name != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || db_array == NULL || name == NULL) { - errno = EFAULT; - return (-2); - } -#endif return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); } @@ -249,19 +244,21 @@ cgetent(buf, db_array, name) * MAX_RECURSION. */ static int -getent(cap, len, db_array, fd, name, depth, nfield) - char **cap, **db_array, *nfield; - const char *name; - size_t *len; - int fd, depth; +getent(char **cap, size_t *len, const char * const *db_array, int fd, + const char *name, int depth, char *nfield) { +#ifndef SMALL DB *capdbp; - char *r_end, *rp = NULL, **db_p; /* pacify gcc */ - int myfd = 0, eof, foundit, retval; + char pbuf[MAXPATHLEN]; + char *cbuf; + int retval; size_t clen; - char *record, *cbuf; +#endif + char *record, *newrecord; + char *r_end, *rp = NULL; /* pacify gcc */ + const char * const *db_p; + int myfd = 0, eof, foundit; int tc_not_resolved; - char pbuf[_POSIX_PATH_MAX]; _DIAGASSERT(cap != NULL); _DIAGASSERT(len != NULL); @@ -314,6 +311,7 @@ getent(cap, len, db_array, fd, name, dep if (fd >= 0) { (void)lseek(fd, (off_t)0, SEEK_SET); } else { +#ifndef SMALL (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) != NULL) { @@ -338,7 +336,9 @@ getent(cap, len, db_array, fd, name, dep *len = clen; *cap = cbuf; return (retval); - } else { + } else +#endif + { fd = open(*db_p, O_RDONLY, 0); if (fd < 0) { /* No error on unfound file. */ @@ -445,13 +445,15 @@ getent(cap, len, db_array, fd, name, dep pos = rp - record; newsize = r_end - record + BFRAG; - record = realloc(record, newsize); - if (record == NULL) { + newrecord = realloc(record, newsize); + if (newrecord == NULL) { + free(record); if (myfd) (void)close(fd); errno = ENOMEM; return (-2); } + record = newrecord; r_end = record + newsize; rp = record + pos; } @@ -587,14 +589,16 @@ tc_exp: { newsize = r_end - record + diff + BFRAG; tcpos = tcstart - record; tcposend = tcend - record; - record = realloc(record, newsize); - if (record == NULL) { + newrecord = realloc(record, newsize); + if (newrecord == NULL) { + free(record); if (myfd) (void)close(fd); free(icap); errno = ENOMEM; return (-2); } + record = newrecord; r_end = record + newsize; rp = record + pos; tcstart = record + tcpos; @@ -625,12 +629,15 @@ tc_exp: { if (myfd) (void)close(fd); *len = rp - record - 1; /* don't count NUL */ - if (r_end > rp) - if ((record = + if (r_end > rp) { + if ((newrecord = realloc(record, (size_t)(rp - record))) == NULL) { + free(record); errno = ENOMEM; return (-2); } + record = newrecord; + } *cap = record; if (tc_not_resolved) @@ -638,11 +645,9 @@ tc_exp: { return (0); } +#ifndef SMALL static int -cdbget(capdbp, bp, name) - DB *capdbp; - char **bp; - const char *name; +cdbget(DB *capdbp, char **bp, const char *name) { DBT key; DBT data; @@ -651,8 +656,7 @@ cdbget(capdbp, bp, name) _DIAGASSERT(bp != NULL); _DIAGASSERT(name != NULL); - /* LINTED key is not modified */ - key.data = (char *)name; + key.data = __UNCONST(name); key.size = strlen(name); for (;;) { @@ -675,23 +679,19 @@ cdbget(capdbp, bp, name) *bp = (char *)data.data + 1; return (((char *)(data.data))[0] == TCERR ? 1 : 0); } +#endif /* * Cgetmatch will return 0 if name is one of the names of the capability * record buf, -1 if not. */ int -cgetmatch(buf, name) - const char *buf, *name; +cgetmatch(const char *buf, const char *name) { const char *np, *bp; _DIAGASSERT(buf != NULL); _DIAGASSERT(name != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || name == NULL) - return (-1); -#endif /* * Start search at beginning of record. @@ -715,7 +715,10 @@ cgetmatch(buf, name) /* * Match failed, skip to next name in record. */ - bp--; /* a '|' or ':' may have stopped the match */ + if (bp > buf) + bp--; /* a '|' or ':' may have stopped the match */ + else + return (-1); for (;;) if (*bp == '\0' || *bp == ':') return (-1); /* match failed totally */ @@ -726,18 +729,11 @@ cgetmatch(buf, name) } int -cgetfirst(buf, db_array) - char **buf, **db_array; +cgetfirst(char **buf, const char * const *db_array) { _DIAGASSERT(buf != NULL); _DIAGASSERT(db_array != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || db_array == NULL) { - errno = EFAULT; - return (-2); - } -#endif (void)cgetclose(); return (cgetnext(buf, db_array)); @@ -745,10 +741,10 @@ cgetfirst(buf, db_array) static FILE *pfp; static int slash; -static char **dbp; +static const char * const *dbp; int -cgetclose() +cgetclose(void) { if (pfp != NULL) { (void)fclose(pfp); @@ -766,23 +762,15 @@ cgetclose() * upon returning an entry with more remaining, and -1 if an error occurs. */ int -cgetnext(bp, db_array) - char **bp; - char **db_array; +cgetnext(char **bp, const char * const *db_array) { - size_t len; + size_t len = 0; int status, done; char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; size_t dummy; _DIAGASSERT(bp != NULL); _DIAGASSERT(db_array != NULL); -#ifdef _DIAGNOSTIC - if (bp == NULL || db_array == NULL) { - errno = EFAULT; - return (-2); - } -#endif if (dbp == NULL) dbp = db_array; @@ -838,6 +826,8 @@ cgetnext(bp, db_array) /* * Line points to a name line. */ + if (len > sizeof(nbuf)) + return -1; done = 0; np = nbuf; for (;;) { @@ -869,6 +859,8 @@ cgetnext(bp, db_array) line[len - 1] = '\0'; } } + if (len > sizeof(buf)) + return -1; rp = buf; for(cp = nbuf; *cp != '\0'; cp++) if (*cp == '|' || *cp == ':') @@ -904,26 +896,17 @@ cgetnext(bp, db_array) * allocation failure). */ int -cgetstr(buf, cap, str) - char *buf; - const char *cap; - char **str; +cgetstr(char *buf, const char *cap, char **str) { u_int m_room; const char *bp; char *mp; int len; - char *mem; + char *mem, *newmem; _DIAGASSERT(buf != NULL); _DIAGASSERT(cap != NULL); _DIAGASSERT(str != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || cap == NULL || str == NULL) { - errno = EFAULT; - return (-2); - } -#endif /* * Find string capability cap @@ -1010,8 +993,11 @@ cgetstr(buf, cap, str) if (m_room == 0) { size_t size = mp - mem; - if ((mem = realloc(mem, size + SFRAG)) == NULL) + if ((newmem = realloc(mem, size + SFRAG)) == NULL) { + free(mem); return (-2); + } + mem = newmem; m_room = SFRAG; mp = mem + size; } @@ -1023,9 +1009,13 @@ cgetstr(buf, cap, str) /* * Give back any extra memory and return value and success. */ - if (m_room != 0) - if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) + if (m_room != 0) { + if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { + free(mem); return (-2); + } + mem = newmem; + } *str = mem; return (len); } @@ -1041,26 +1031,17 @@ cgetstr(buf, cap, str) * error was encountered (storage allocation failure). */ int -cgetustr(buf, cap, str) - char *buf; - const char *cap; - char **str; +cgetustr(char *buf, const char *cap, char **str) { u_int m_room; const char *bp; char *mp; int len; - char *mem; + char *mem, *newmem; _DIAGASSERT(buf != NULL); _DIAGASSERT(cap != NULL); _DIAGASSERT(str != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || cap == NULL || str == NULL) { - errno = EFAULT; - return (-2); - } -#endif /* * Find string capability cap @@ -1096,8 +1077,11 @@ cgetustr(buf, cap, str) if (m_room == 0) { size_t size = mp - mem; - if ((mem = realloc(mem, size + SFRAG)) == NULL) + if ((newmem = realloc(mem, size + SFRAG)) == NULL) { + free(mem); return (-2); + } + mem = newmem; m_room = SFRAG; mp = mem + size; } @@ -1109,9 +1093,13 @@ cgetustr(buf, cap, str) /* * Give back any extra memory and return value and success. */ - if (m_room != 0) - if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) + if (m_room != 0) { + if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { + free(mem); return (-2); + } + mem = newmem; + } *str = mem; return (len); } @@ -1123,10 +1111,7 @@ cgetustr(buf, cap, str) * numeric capability couldn't be found. */ int -cgetnum(buf, cap, num) - char *buf; - const char *cap; - long *num; +cgetnum(char *buf, const char *cap, long *num) { long n; int base, digit; @@ -1135,10 +1120,6 @@ cgetnum(buf, cap, num) _DIAGASSERT(buf != NULL); _DIAGASSERT(cap != NULL); _DIAGASSERT(num != NULL); -#ifdef _DIAGNOSTIC - if (buf == NULL || cap == NULL || num == NULL) - return (-1); -#endif /* * Find numeric capability cap @@ -1196,8 +1177,7 @@ cgetnum(buf, cap, num) * Compare name field of record. */ static int -nfcmp(nf, rec) - char *nf, *rec; +nfcmp(char *nf, char *rec) { char *cp, tmp; int ret;