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.1 retrieving revision 1.46 diff -u -p -r1.1 -r1.46 --- src/lib/libc/gen/getcap.c 1993/05/18 07:37:54 1.1 +++ src/lib/libc/gen/getcap.c 2006/05/29 21:55:41 1.46 @@ -1,6 +1,8 @@ +/* $NetBSD: getcap.c,v 1.46 2006/05/29 21:55:41 jnemeth Exp $ */ + /*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Casey Leedom of Lawrence Livermore National Laboratory. @@ -13,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. * @@ -34,14 +32,30 @@ * SUCH DAMAGE. */ +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)getcap.c 5.15 (Berkeley) 3/19/93"; +#if 0 +static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; +#else +__RCSID("$NetBSD: getcap.c,v 1.46 2006/05/29 21:55:41 jnemeth 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 @@ -50,6 +64,19 @@ static char sccsid[] = "@(#)getcap.c 5.1 #include #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) +#endif + #define BFRAG 1024 #define BSIZE 1024 #define ESC ('[' & 037) /* ASCII ESC */ @@ -64,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 **, char *)); -static int getent __P((char **, u_int *, char **, int, 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 @@ -74,9 +104,11 @@ static int nfcmp __P((char *, char *)); * virtual database. 0 is returned on success, -1 on failure. */ int -cgetset(ent) - char *ent; +cgetset(const char *ent) { + const char *source, *check; + char *dest; + if (ent == NULL) { if (toprec) free(toprec); @@ -90,7 +122,25 @@ cgetset(ent) return (-1); } gottoprec = 0; - (void)strcpy(toprec, ent); + + 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); } @@ -108,10 +158,15 @@ cgetset(ent) */ char * cgetcap(buf, cap, type) - char *buf, *cap; + char *buf; + const char *cap; int type; { - register char *bp, *cp; + char *bp; + const char *cp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); bp = buf; for (;;) { @@ -158,11 +213,15 @@ cgetcap(buf, cap, type) * encountered (couldn't open/read a file, etc.), and -3 if a potential * reference loop is detected. */ +/* coverity[+alloc : arg-*0] */ int -cgetent(buf, db_array, name) - char **buf, **db_array, *name; +cgetent(char **buf, const char * const *db_array, const char *name) { - u_int dummy; + size_t dummy; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + _DIAGASSERT(name != NULL); return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); } @@ -185,20 +244,31 @@ cgetent(buf, db_array, name) * names interpolated, a name can't be found, or depth exceeds * MAX_RECURSION. */ +/* coverity[+alloc : arg-*0] */ static int -getent(cap, len, db_array, fd, name, depth, nfield) - char **cap, **db_array, *name, *nfield; - u_int *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; - DBT key, data; - register char *r_end, *rp, **db_p; - int myfd, eof, foundit, retval; - char *record; + char pbuf[MAXPATHLEN]; + char *cbuf; + int retval; + size_t clen; +#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); + _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. @@ -214,8 +284,7 @@ getent(cap, len, db_array, fd, name, dep errno = ENOMEM; return (-2); } - (void)strcpy(record, toprec); - myfd = 0; + (void)strcpy(record, toprec); /* XXX: strcpy is safe */ db_p = db_array; rp = record + topreclen + 1; r_end = rp + BFRAG; @@ -242,28 +311,44 @@ getent(cap, len, db_array, fd, name, dep */ if (fd >= 0) { - (void)lseek(fd, (off_t)0, L_SET); - myfd = 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) { free(record); retval = cdbget(capdbp, &record, name); - if (capdbp->close(capdbp) < 0) + if (retval < 0) { + /* no record available */ + (void)capdbp->close(capdbp); + return (retval); + } + /* save the data; close frees it */ + clen = strlen(record); + if ((cbuf = malloc(clen + 1)) == NULL) { + (void)capdbp->close(capdbp); + errno = ENOMEM; + return (-2); + } + memmove(cbuf, record, clen + 1); + if (capdbp->close(capdbp) < 0) { + int serrno = errno; + + free(cbuf); + errno = serrno; return (-2); - *len = strlen(record); - *cap = malloc(*len + 1); - memmove(*cap, record, *len + 1); + } + *len = clen; + *cap = cbuf; return (retval); - } else { + } else +#endif + { fd = open(*db_p, O_RDONLY, 0); if (fd < 0) { /* No error on unfound file. */ - if (errno == ENOENT) - continue; - free(record); - return (-2); + continue; } myfd = 1; } @@ -273,8 +358,8 @@ getent(cap, len, db_array, fd, name, dep */ { char buf[BUFSIZ]; - register char *b_end, *bp; - register int c; + char *b_end, *bp, *cp; + int c, slash; /* * Loop invariants: @@ -283,9 +368,12 @@ getent(cap, len, db_array, fd, name, dep * Rp always points just past last character in record. * B_end always points just past last character in buf. * Bp always points at next character in buf. + * Cp remembers where the last colon was. */ b_end = buf; bp = buf; + cp = 0; + slash = 0; for (;;) { /* @@ -302,7 +390,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; @@ -316,12 +407,37 @@ getent(cap, len, db_array, fd, name, dep c = *bp++; if (c == '\n') { - if (rp > record && *(rp-1) == '\\') { + if (slash) { + slash = 0; rp--; continue; } else break; } + if (slash) { + slash = 0; + cp = 0; + } + if (c == ':') { + /* + * If the field was `empty' (i.e. + * contained only white space), back up + * to the colon (eliminating the + * field). + */ + if (cp) + rp = cp; + else + cp = rp; + } else if (c == '\\') { + slash = 1; + } else if (c != ' ' && c != '\t') { + /* + * Forget where the colon was, as this + * is not an empty field. + */ + cp = 0; + } *rp++ = c; /* @@ -335,18 +451,23 @@ 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; } } - /* loop invariant let's us do this */ + /* Eliminate any white space after the last colon. */ + if (cp) + rp = cp + 1; + /* Loop invariant lets us do this. */ *rp++ = '\0'; /* @@ -384,9 +505,8 @@ getent(cap, len, db_array, fd, name, dep * references in it ... */ tc_exp: { - register char *newicap, *s; - register int newilen; - u_int ilen; + char *newicap, *s; + size_t ilen, newilen; int diff, iret, tclen; char *icap, *scan, *tc, *tcstart, *tcend; @@ -475,14 +595,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; @@ -493,8 +615,8 @@ tc_exp: { * Insert tc'ed record into our record. */ s = tcstart + newilen; - bcopy(tcend, s, rp - tcend); - bcopy(newicap, tcstart, newilen); + memmove(s, tcend, (size_t)(rp - tcend)); + memmove(tcstart, newicap, newilen); rp += diff; free(icap); @@ -513,12 +635,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) @@ -526,16 +651,18 @@ tc_exp: { return (0); } +#ifndef SMALL static int -cdbget(capdbp, bp, name) - DB *capdbp; - char **bp, *name; +cdbget(DB *capdbp, char **bp, const char *name) { - DBT key, data; - char *buf; - int st; + DBT key; + DBT data; - key.data = name; + _DIAGASSERT(capdbp != NULL); + _DIAGASSERT(bp != NULL); + _DIAGASSERT(name != NULL); + + key.data = __UNCONST(name); key.size = strlen(name); for (;;) { @@ -558,16 +685,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) - char *buf, *name; +cgetmatch(const char *buf, const char *name) { - register char *np, *bp; + const char *np, *bp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(name != NULL); /* * Start search at beginning of record. @@ -579,19 +709,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 */ @@ -601,24 +734,23 @@ 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); + (void)cgetclose(); return (cgetnext(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); @@ -635,15 +767,17 @@ cgetclose() * specified by db_array. It returns 0 upon completion of the database, 1 * upon returning an entry with more remaining, and -1 if an error occurs. */ +/* coverity[+alloc : arg-*0] */ int -cgetnext(bp, db_array) - register char **bp; - char **db_array; +cgetnext(char **bp, const char * const *db_array) { - size_t len; - int status, i, done; + size_t len = 0; + int status, done; char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; - u_int dummy; + size_t dummy; + + _DIAGASSERT(bp != NULL); + _DIAGASSERT(db_array != NULL); if (dbp == NULL) dbp = db_array; @@ -657,13 +791,16 @@ cgetnext(bp, db_array) gottoprec = 1; line = toprec; } else { - line = fgetline(pfp, &len); - if (line == NULL && pfp) { - (void)fclose(pfp); + line = fgetln(pfp, &len); + if (line == NULL) { + if (pfp == NULL) + return -1; if (ferror(pfp)) { (void)cgetclose(); return (-1); } else { + (void)fclose(pfp); + pfp = NULL; if (*++dbp == NULL) { (void)cgetclose(); return (0); @@ -680,7 +817,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; @@ -698,7 +835,8 @@ cgetnext(bp, db_array) /* * Line points to a name line. */ - i = 0; + if (len > sizeof(nbuf)) + return -1; done = 0; np = nbuf; for (;;) { @@ -716,19 +854,24 @@ cgetnext(bp, db_array) *np = '\0'; break; } else { /* name field extends beyond the line */ - line = fgetline(pfp, &len); + line = fgetln(pfp, &len); if (line == NULL && pfp) { - (void)fclose(pfp); if (ferror(pfp)) { (void)cgetclose(); return (-1); } + (void)fclose(pfp); + pfp = NULL; + *np = '\0'; + break; } else line[len - 1] = '\0'; } } + if (len > sizeof(buf)) + return -1; rp = buf; - for(cp = nbuf; *cp != NULL; cp++) + for(cp = nbuf; *cp != '\0'; cp++) if (*cp == '|' || *cp == ':') break; else @@ -762,14 +905,17 @@ cgetnext(bp, db_array) * allocation failure). */ int -cgetstr(buf, cap, str) - char *buf, *cap; - char **str; +cgetstr(char *buf, const char *cap, char **str) { - register u_int m_room; - register char *bp, *mp; + 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); /* * Find string capability cap @@ -806,7 +952,7 @@ cgetstr(buf, cap, str) if (*bp == ':' || *bp == '\0') break; /* drop unfinished escape */ if ('0' <= *bp && *bp <= '7') { - register int n, i; + int n, i; n = 0; i = 3; /* maximum of three octal digits */ @@ -856,8 +1002,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; } @@ -869,9 +1018,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); } @@ -887,13 +1040,17 @@ cgetstr(buf, cap, str) * error was encountered (storage allocation failure). */ int -cgetustr(buf, cap, str) - char *buf, *cap, **str; +cgetustr(char *buf, const char *cap, char **str) { - register u_int m_room; - register char *bp, *mp; + 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); /* * Find string capability cap @@ -929,8 +1086,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; } @@ -942,9 +1102,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); } @@ -956,13 +1120,15 @@ cgetustr(buf, cap, str) * numeric capability couldn't be found. */ int -cgetnum(buf, cap, num) - char *buf, *cap; - long *num; +cgetnum(char *buf, const char *cap, long *num) { - register long n; - register int base, digit; - register char *bp; + long n; + int base, digit; + const char *bp; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(num != NULL); /* * Find numeric capability cap @@ -1020,12 +1186,14 @@ 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; - + + _DIAGASSERT(nf != NULL); + _DIAGASSERT(rec != NULL); + for (cp = rec; *cp != ':'; cp++) ;