Replace strlcpy() with evutil_snprintf() in evhttp

Get rid of strlcpy() and its implementation entirely, it doesn't buy anything
over snprintf(). Use evutil_snprintf() so as to match the rest of the code.
This commit is contained in:
Julien BLACHE 2010-01-26 17:54:45 +01:00
parent 98cb978b9b
commit 4701817333
4 changed files with 12 additions and 103 deletions

View File

@ -37,8 +37,8 @@ forked_daapd_SOURCES = main.c \
filescanner_ffmpeg.c filescanner_urlfile.c filescanner_m3u.c $(ITUNESSRC) \
mdns_avahi.c mdns_avahi.h \
remote_pairing.c remote_pairing.h \
evhttp/http.c evhttp/evhttp.h evhttp/strlcpy.c \
evhttp/http-internal.h evhttp/log.h evhttp/strlcpy-internal.h \
evhttp/http.c evhttp/evhttp.h \
evhttp/http-internal.h evhttp/log.h \
httpd.c httpd.h \
httpd_rsp.c httpd_rsp.h \
httpd_daap.c httpd_daap.h \

View File

@ -80,7 +80,6 @@
#undef timeout_pending
#undef timeout_initialized
#include "strlcpy-internal.h"
#include <event.h>
#include "evhttp.h"
#include <evutil.h>
@ -105,19 +104,21 @@ fake_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
size_t hostlen, char *serv, size_t servlen, int flags)
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
int ret;
if (serv != NULL) {
char tmpserv[16];
evutil_snprintf(tmpserv, sizeof(tmpserv),
"%d", ntohs(sin->sin_port));
if (strlcpy(serv, tmpserv, servlen) >= servlen)
ret = evutil_snprintf(serv, servlen, "%s", tmpserv);
if ((ret < 0) || (ret >= servlen))
return (-1);
}
if (host != NULL) {
if (flags & NI_NUMERICHOST) {
if (strlcpy(host, inet_ntoa(sin->sin_addr),
hostlen) >= hostlen)
ret = evutil_snprintf(host, hostlen, "%s", inet_ntoa(sin->sin_addr));
if ((ret < 0) || (ret >= hostlen))
return (-1);
else
return (0);
@ -128,7 +129,8 @@ fake_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
if (hp == NULL)
return (-2);
if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
ret = evutil_snprintf(host, hostlen, "%s", hp->h_name);
if ((ret < 0) || (ret >= hostlen))
return (-1);
else
return (0);
@ -553,6 +555,7 @@ evhttp_hostportfile(char *url, char **phost, u_short *pport, char **pfile)
char *p;
const char *p2;
int len;
int ret;
u_short port;
len = strlen(HTTP_PREFIX);
@ -562,7 +565,8 @@ evhttp_hostportfile(char *url, char **phost, u_short *pport, char **pfile)
url += len;
/* We might overrun */
if (strlcpy(host, url, sizeof (host)) >= sizeof(host))
ret = evutil_snprintf(host, sizeof(host), "%s", url);
if ((ret < 0) || (ret >= sizeof(host)))
return (-1);
p = strchr(host, '/');

View File

@ -1,21 +0,0 @@
#ifndef _STRLCPY_INTERNAL_H_
#define _STRLCPY_INTERNAL_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <event-config.h>
#ifndef _EVENT_HAVE_STRLCPY
#include <string.h>
size_t _event_strlcpy(char *dst, const char *src, size_t siz);
#define strlcpy _event_strlcpy
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,74 +0,0 @@
/* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <event-config.h>
#ifndef _EVENT_HAVE_STRLCPY
#include "strlcpy-internal.h"
/*
* 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
_event_strlcpy(dst, src, siz)
char *dst;
const char *src;
size_t siz;
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
#endif