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.23 retrieving revision 1.33.4.3 diff -u -p -r1.23 -r1.33.4.3 --- src/lib/libc/gen/getcap.c 1998/08/26 00:38:40 1.23 +++ src/lib/libc/gen/getcap.c 2002/08/13 00:59:50 1.33.4.3 @@ -1,4 +1,4 @@ -/* $NetBSD: getcap.c,v 1.23 1998/08/26 00:38:40 perry Exp $ */ +/* $NetBSD: getcap.c,v 1.33.4.3 2002/08/13 00:59:50 nathanw Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -41,12 +41,15 @@ #if 0 static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; #else -__RCSID("$NetBSD: getcap.c,v 1.23 1998/08/26 00:38:40 perry Exp $"); +__RCSID("$NetBSD: getcap.c,v 1.33.4.3 2002/08/13 00:59:50 nathanw Exp $"); #endif #endif /* LIBC_SCCS and not lint */ #include "namespace.h" #include +#include + +#include #include #include #include @@ -58,16 +61,16 @@ __RCSID("$NetBSD: getcap.c,v 1.23 1998/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 @@ -97,6 +100,9 @@ int cgetset(ent) const char *ent; { + const char *source, *check; + char *dest; + if (ent == NULL) { if (toprec) free(toprec); @@ -110,7 +116,25 @@ cgetset(ent) return (-1); } gottoprec = 0; - (void)strcpy(toprec, ent); /* XXX: strcpy is safe */ + + source=ent; + dest=toprec; + while (*source) { /* Strip whitespace */ + *dest++ = *source++; /* Do not check first field */ + while (*source == ':') { + check=source+1; + while (*check && (isspace((unsigned char)*check) || + (*check=='\\' && isspace((unsigned char)check[1])))) + ++check; + if( *check == ':' ) + source=check; + else + break; + + } + } + *dest=0; + return (0); } @@ -135,6 +159,9 @@ cgetcap(buf, cap, type) char *bp; const char *cp; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + bp = buf; for (;;) { /* @@ -187,6 +214,10 @@ cgetent(buf, db_array, name) { size_t dummy; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + _DIAGASSERT(name != NULL); + return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); } @@ -219,10 +250,17 @@ getent(cap, len, db_array, fd, name, dep char *r_end, *rp = NULL, **db_p; /* pacify gcc */ int myfd = 0, eof, foundit, retval; size_t clen; - char *record, *cbuf; + char *record, *cbuf, *newrecord; int tc_not_resolved; - char pbuf[_POSIX_PATH_MAX]; + char pbuf[MAXPATHLEN]; + _DIAGASSERT(cap != NULL); + _DIAGASSERT(len != NULL); + _DIAGASSERT(db_array != NULL); + /* fd may be -1 */ + _DIAGASSERT(name != NULL); + /* nfield may be NULL */ + /* * Return with ``loop detected'' error if we've recursed more than * MAX_RECURSION times. @@ -282,7 +320,10 @@ getent(cap, len, db_array, fd, name, dep cbuf = malloc(clen + 1); memmove(cbuf, record, clen + 1); if (capdbp->close(capdbp) < 0) { + int serrno = errno; + free(cbuf); + errno = serrno; return (-2); } *len = clen; @@ -334,7 +375,10 @@ getent(cap, len, db_array, fd, name, dep if (myfd) (void)close(fd); if (n < 0) { + int serrno = errno; + free(record); + errno = serrno; return (-2); } else { fd = -1; @@ -392,13 +436,15 @@ getent(cap, len, db_array, fd, name, dep pos = rp - record; newsize = r_end - record + BFRAG; - record = realloc(record, newsize); - if (record == NULL) { - errno = ENOMEM; + 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; } @@ -534,14 +580,16 @@ tc_exp: { newsize = r_end - record + diff + BFRAG; tcpos = tcstart - record; tcposend = tcend - record; - record = realloc(record, newsize); - if (record == NULL) { - errno = ENOMEM; + 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; @@ -572,12 +620,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) @@ -591,8 +642,14 @@ cdbget(capdbp, bp, name) char **bp; const char *name; { - DBT key, data; + DBT key; + DBT data; + + _DIAGASSERT(capdbp != NULL); + _DIAGASSERT(bp != NULL); + _DIAGASSERT(name != NULL); + /* LINTED key is not modified */ key.data = (char *)name; key.size = strlen(name); @@ -627,6 +684,9 @@ cgetmatch(buf, name) { const char *np, *bp; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(name != NULL); + /* * Start search at beginning of record. */ @@ -637,19 +697,22 @@ cgetmatch(buf, name) */ np = name; for (;;) - if (*np == '\0') + if (*np == '\0') { if (*bp == '|' || *bp == ':' || *bp == '\0') return (0); else break; - else + } else if (*bp++ != *np++) break; /* * 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 */ @@ -663,6 +726,10 @@ int cgetfirst(buf, db_array) char **buf, **db_array; { + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + (void)cgetclose(); return (cgetnext(buf, db_array)); } @@ -699,6 +766,9 @@ cgetnext(bp, db_array) char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; size_t dummy; + _DIAGASSERT(bp != NULL); + _DIAGASSERT(db_array != NULL); + if (dbp == NULL) dbp = db_array; @@ -735,7 +805,7 @@ cgetnext(bp, db_array) slash = 0; continue; } - if (isspace(*line) || + if (isspace((unsigned char)*line) || *line == ':' || *line == '#' || slash) { if (line[len - 2] == '\\') slash = 1; @@ -753,6 +823,8 @@ cgetnext(bp, db_array) /* * Line points to a name line. */ + if (len > sizeof(nbuf)) + return -1; done = 0; np = nbuf; for (;;) { @@ -784,6 +856,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 == ':') @@ -828,7 +902,11 @@ cgetstr(buf, cap, str) const char *bp; char *mp; int len; - char *mem; + char *mem, *newmem; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); /* * Find string capability cap @@ -915,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; } @@ -928,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); } @@ -955,7 +1040,11 @@ cgetustr(buf, cap, str) const char *bp; char *mp; int len; - char *mem; + char *mem, *newmem; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); /* * Find string capability cap @@ -991,8 +1080,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; } @@ -1004,9 +1096,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); } @@ -1027,6 +1123,10 @@ cgetnum(buf, cap, num) int base, digit; const char *bp; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(num != NULL); + /* * Find numeric capability cap */ @@ -1088,7 +1188,10 @@ nfcmp(nf, rec) { char *cp, tmp; int ret; - + + _DIAGASSERT(nf != NULL); + _DIAGASSERT(rec != NULL); + for (cp = rec; *cp != ':'; cp++) ;