`Hide' strlcpy and strlcat (using the namespace.h / __weak_reference

technique) so that we don't wind up calling into an application's
version if the application defines them.

Inspired by:	qpopper's interfering and buggy version of strlcpy
This commit is contained in:
Jacques Vidrine 2003-04-29 21:13:50 +00:00
parent a8b5a9cd12
commit 5723e501ab
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=114256
20 changed files with 71 additions and 51 deletions

View File

@ -30,15 +30,12 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* I din't use "namespace.h" here because none of the relevant utilities
* are threaded, so I'm not concerned about cancellation points or other
* niceties.
*/
#include "namespace.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
#ifndef LINE_MAX
#define LINE_MAX _POSIX2_LINE_MAX
@ -55,7 +52,7 @@ check_utility_compat(const char *utility)
int len;
if ((p = getenv(_ENV_UTIL_COMPAT)) != NULL) {
strlcpy(buf, p, sizeof buf);
_strlcpy(buf, p, sizeof buf);
} else {
if ((len = readlink(_PATH_UTIL_COMPAT, buf, sizeof buf)) < 0)
return 0;

View File

@ -37,13 +37,14 @@ static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/param.h>
#include <errno.h>
#include <limits.h>
#include <paths.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
size_t
@ -114,7 +115,7 @@ confstr(int name, char *buf, size_t len)
docopy:
if (len != 0 && buf != NULL)
strlcpy(buf, p, len);
_strlcpy(buf, p, len);
return (strlen(p) + 1);
default:

View File

@ -27,10 +27,12 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <fmtmsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "un-namespace.h"
/* Default value for MSGVERB. */
#define DFLT_MSGVERB "label:severity:text:action:tag"
@ -101,13 +103,13 @@ fmtmsg(long class, const char *label, int sev, const char *text,
#define INSERT_COLON \
if (*output != '\0') \
strlcat(output, ": ", size)
_strlcat(output, ": ", size)
#define INSERT_NEWLINE \
if (*output != '\0') \
strlcat(output, "\n", size)
_strlcat(output, "\n", size)
#define INSERT_SPACE \
if (*output != '\0') \
strlcat(output, " ", size)
_strlcat(output, " ", size)
/*
* Returns NULL on memory allocation failure, otherwise returns a pointer to
@ -139,20 +141,20 @@ printfmt(char *msgverb, long class, const char *label, int sev,
while ((comp = nextcomp(msgverb)) != NULL) {
if (strcmp(comp, "label") == 0 && label != MM_NULLLBL) {
INSERT_COLON;
strlcat(output, label, size);
_strlcat(output, label, size);
} else if (strcmp(comp, "severity") == 0 && sevname != NULL) {
INSERT_COLON;
strlcat(output, sevinfo(sev), size);
_strlcat(output, sevinfo(sev), size);
} else if (strcmp(comp, "text") == 0 && text != MM_NULLTXT) {
INSERT_COLON;
strlcat(output, text, size);
_strlcat(output, text, size);
} else if (strcmp(comp, "action") == 0 && act != MM_NULLACT) {
INSERT_NEWLINE;
strlcat(output, "TO FIX: ", size);
strlcat(output, act, size);
_strlcat(output, "TO FIX: ", size);
_strlcat(output, act, size);
} else if (strcmp(comp, "tag") == 0 && tag != MM_NULLTAG) {
INSERT_SPACE;
strlcat(output, tag, size);
_strlcat(output, tag, size);
}
}
INSERT_NEWLINE;
@ -171,7 +173,7 @@ nextcomp(const char *msgverb)
char *retval;
if (*lmsgverb == '\0') {
strlcpy(lmsgverb, msgverb, sizeof(lmsgverb));
_strlcpy(lmsgverb, msgverb, sizeof(lmsgverb));
retval = strtok_r(lmsgverb, ":", &state);
} else {
retval = strtok_r(NULL, ":", &state);

View File

@ -620,7 +620,7 @@ dns_group(void *retval, void *mdata, va_list ap)
* pointer for the member list terminator.
*/
adjsize = bufsize - _ALIGNBYTES - sizeof(char *);
linesize = strlcpy(buffer, hes[0], adjsize);
linesize = _strlcpy(buffer, hes[0], adjsize);
if (linesize >= adjsize) {
*errnop = ERANGE;
rv = NS_RETURN;
@ -721,7 +721,7 @@ nis_group(void *retval, void *mdata, va_list ap)
rv = NS_NOTFOUND;
switch (how) {
case nss_lt_name:
if (strlcpy(buffer, name, bufsize) >= bufsize)
if (_strlcpy(buffer, name, bufsize) >= bufsize)
goto erange;
break;
case nss_lt_id:

View File

@ -584,7 +584,7 @@ files_passwd(void *retval, void *mdata, va_list ap)
/* MAXLOGNAME includes NUL byte, but we do not
* include the NUL byte in the key.
*/
namesize = strlcpy(&keybuf[1], name, sizeof(keybuf)-1);
namesize = _strlcpy(&keybuf[1], name, sizeof(keybuf)-1);
if (namesize >= sizeof(keybuf)-1) {
*errnop = EINVAL;
rv = NS_NOTFOUND;
@ -897,7 +897,7 @@ dns_passwd(void *retval, void *mdata, va_list ap)
hes = NULL;
continue;
}
linesize = strlcpy(buffer, hes[0], bufsize);
linesize = _strlcpy(buffer, hes[0], bufsize);
if (linesize >= bufsize) {
*errnop = ERANGE;
rv = NS_RETURN;
@ -1055,7 +1055,7 @@ nis_passwd(void *retval, void *mdata, va_list ap)
rv = NS_NOTFOUND;
switch (how) {
case nss_lt_name:
if (strlcpy(buffer, name, bufsize) >= bufsize)
if (_strlcpy(buffer, name, bufsize) >= bufsize)
goto erange;
break;
case nss_lt_id:
@ -1215,7 +1215,7 @@ compat_use_template(struct passwd *pwd, struct passwd *template, char *buffer,
hold.field = NULL; \
else { \
hold.field = p; \
p += strlcpy(p, pwd->field, eob-p) + 1; \
p += _strlcpy(p, pwd->field, eob-p) + 1; \
} \
} while (0)
COPY(pw_name);
@ -1233,7 +1233,7 @@ compat_use_template(struct passwd *pwd, struct passwd *template, char *buffer,
pwd->field = NULL; \
else { \
pwd->field = p; \
if ((n = strlcpy(p, q, eob-p)) >= eob-p) { \
if ((n = _strlcpy(p, q, eob-p)) >= eob-p) { \
free(copy); \
return (ERANGE); \
} \

View File

@ -43,6 +43,8 @@
#define err _err
#define warn _warn
#define nsdispatch _nsdispatch
#define strlcat _strlcat
#define strlcpy _strlcpy
/*
* Prototypes for syscalls/functions that need to be overridden

View File

@ -149,5 +149,7 @@ int _flock(int, int);
#undef err
#undef warn
#undef nsdispatch
#undef strlcat
#undef strlcpy
#endif /* _UN_NAMESPACE_H_ */

View File

@ -41,6 +41,7 @@ static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
@ -50,6 +51,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
#include "collate.h"
#include "lmonetary.h" /* for __monetary_load_locale() */
#include "lnumeric.h" /* for __numeric_load_locale() */
@ -180,7 +182,7 @@ setlocale(category, locale)
errno = EINVAL;
return (NULL);
}
(void)strlcpy(new_categories[i], locale,
(void)_strlcpy(new_categories[i], locale,
len + 1);
i++;
locale = r;

View File

@ -853,7 +853,7 @@ get_canonname(pai, ai, str)
ai->ai_canonname = (char *)malloc(strlen(str) + 1);
if (ai->ai_canonname == NULL)
return EAI_MEMORY;
strlcpy(ai->ai_canonname, str, strlen(str) + 1);
_strlcpy(ai->ai_canonname, str, strlen(str) + 1);
}
return 0;
}
@ -1305,7 +1305,7 @@ getanswer(answer, anslen, qname, qtype, pai)
had_error++;
continue;
}
strlcpy(bp, tbuf, ep - bp);
_strlcpy(bp, tbuf, ep - bp);
canonname = bp;
bp += n;
continue;

View File

@ -60,6 +60,7 @@ static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vi
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
@ -78,6 +79,7 @@ __FBSDID("$FreeBSD$");
#include <syslog.h>
#include <stdarg.h>
#include <nsswitch.h>
#include "un-namespace.h"
#include "res_config.h"
@ -674,7 +676,7 @@ _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
uaddr[n] & 0xf,
(uaddr[n] >> 4) & 0xf));
}
strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
_strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
break;
default:
abort();
@ -686,7 +688,7 @@ _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf);
if (n < 0 && af == AF_INET6) {
*qp = '\0';
strlcat(qbuf, "ip6.int", sizeof(qbuf));
_strlcat(qbuf, "ip6.int", sizeof(qbuf));
n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf,
sizeof buf->buf);
}

View File

@ -46,6 +46,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
@ -57,6 +58,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <stddef.h>
#include <errno.h>
#include "un-namespace.h"
static const struct afd {
int a_af;
@ -145,12 +147,12 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
if (sp) {
if (strlen(sp->s_name) + 1 > servlen)
return EAI_MEMORY;
strlcpy(serv, sp->s_name, servlen);
_strlcpy(serv, sp->s_name, servlen);
} else {
snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
if (strlen(numserv) + 1 > servlen)
return EAI_MEMORY;
strlcpy(serv, numserv, servlen);
_strlcpy(serv, numserv, servlen);
}
}
@ -223,7 +225,7 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
numaddrlen = strlen(numaddr);
if (numaddrlen + 1 > hostlen) /* don't forget terminator */
return EAI_MEMORY;
strlcpy(host, numaddr, hostlen);
_strlcpy(host, numaddr, hostlen);
break;
}
} else {
@ -246,7 +248,7 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
freehostent(hp);
return EAI_MEMORY;
}
strlcpy(host, hp->h_name, hostlen);
_strlcpy(host, hp->h_name, hostlen);
freehostent(hp);
} else {
if (flags & NI_NAMEREQD)
@ -293,7 +295,7 @@ ip6_parsenumeric(sa, addr, host, hostlen, flags)
numaddrlen = strlen(numaddr);
if (numaddrlen + 1 > hostlen) /* don't forget terminator */
return EAI_MEMORY;
strlcpy(host, numaddr, hostlen);
_strlcpy(host, numaddr, hostlen);
if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
char zonebuf[MAXHOSTNAMELEN];

View File

@ -51,6 +51,7 @@ static char *orig_rcsid = "$NetBSD: hesiod.c,v 1.9 1999/02/11 06:16:38 simonb Ex
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <sys/param.h>
#include <netinet/in.h>
@ -64,6 +65,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
struct hesiod_p {
char *lhs; /* normally ".ns" */
@ -163,7 +165,7 @@ hesiod_to_bind(void *context, const char *name, const char *type)
const char *rhs;
int len;
if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
if (_strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) {
errno = EMSGSIZE;
return NULL;
}

View File

@ -70,7 +70,7 @@ if_nametoindex(const char *ifname)
s = _socket(AF_INET, SOCK_DGRAM, 0);
if (s != -1) {
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
_close(s);
return (ifr.ifr_index);

View File

@ -160,7 +160,7 @@ catopen(name, type)
put_tmpptr:
spcleft = sizeof(path) -
(pathP - path) - 1;
if (strlcpy(pathP, tmpptr, spcleft) >=
if (_strlcpy(pathP, tmpptr, spcleft) >=
spcleft) {
too_long:
free(plang);

View File

@ -535,7 +535,7 @@ svcunix_create(sock, sendsize, recvsize, path)
memset(&sun, 0, sizeof sun);
sun.sun_family = AF_LOCAL;
if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
if (_strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
sizeof(sun.sun_path))
goto done;
sun.sun_len = SUN_LEN(&sun);

View File

@ -67,14 +67,14 @@ realpath(const char *path, char resolved[PATH_MAX])
if (path[1] == '\0')
return (resolved);
resolved_len = 1;
left_len = strlcpy(left, path + 1, sizeof(left));
left_len = _strlcpy(left, path + 1, sizeof(left));
} else {
if (getcwd(resolved, PATH_MAX) == NULL) {
strlcpy(resolved, ".", PATH_MAX);
_strlcpy(resolved, ".", PATH_MAX);
return (NULL);
}
resolved_len = strlen(resolved);
left_len = strlcpy(left, path, sizeof(left));
left_len = _strlcpy(left, path, sizeof(left));
}
if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
@ -131,7 +131,7 @@ realpath(const char *path, char resolved[PATH_MAX])
* lstat() fails we still can return successfully if
* there are no more path components left.
*/
resolved_len = strlcat(resolved, next_token, PATH_MAX);
resolved_len = _strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
return (NULL);
@ -177,13 +177,13 @@ realpath(const char *path, char resolved[PATH_MAX])
symlink[slen] = '/';
symlink[slen + 1] = 0;
}
left_len = strlcat(symlink, left, sizeof(left));
left_len = _strlcat(symlink, left, sizeof(left));
if (left_len >= sizeof(left)) {
errno = ENAMETOOLONG;
return (NULL);
}
}
left_len = strlcpy(left, symlink, sizeof(left));
left_len = _strlcpy(left, symlink, sizeof(left));
}
}

View File

@ -37,9 +37,11 @@ static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "un-namespace.h"
#define UPREFIX "Unknown error: "
@ -69,8 +71,8 @@ errstr(int num, char *buf, size_t len)
} while (uerr /= 10);
if (num < 0)
*--t = '-';
strlcpy(buf, UPREFIX, len);
strlcat(buf, t, len);
_strlcpy(buf, UPREFIX, len);
_strlcat(buf, t, len);
}
int
@ -81,7 +83,7 @@ strerror_r(int errnum, char *strerrbuf, size_t buflen)
errstr(errnum, strerrbuf, buflen);
return (EINVAL);
}
if (strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen)
if (_strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen)
return (ERANGE);
return (0);
}

View File

@ -33,9 +33,12 @@ static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <string.h>
#include "un-namespace.h"
__weak_reference(_strlcat, strlcat);
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
@ -44,7 +47,7 @@ __FBSDID("$FreeBSD$");
* If retval >= siz, truncation occurred.
*/
size_t
strlcat(dst, src, siz)
_strlcat(dst, src, siz)
char *dst;
const char *src;
size_t siz;

View File

@ -33,15 +33,18 @@ static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <string.h>
#include "un-namespace.h"
__weak_reference(_strlcpy, strlcpy);
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t strlcpy(dst, src, siz)
size_t _strlcpy(dst, src, siz)
char *dst;
const char *src;
size_t siz;

View File

@ -509,7 +509,7 @@ _yp_dobind(char *dom, struct dom_binding **ypdb)
*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
gotit:
ysd->dom_vers = YPVERS;
strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
_strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
}
/* Don't rebuild the connection to the server unless we have to. */