From 4701817333363d6cce7e1e97f86e15022b23ae41 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Tue, 26 Jan 2010 17:54:45 +0100 Subject: [PATCH] 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. --- src/Makefile.am | 4 +- src/evhttp/http.c | 16 +++++--- src/evhttp/strlcpy-internal.h | 21 ---------- src/evhttp/strlcpy.c | 74 ----------------------------------- 4 files changed, 12 insertions(+), 103 deletions(-) delete mode 100644 src/evhttp/strlcpy-internal.h delete mode 100644 src/evhttp/strlcpy.c diff --git a/src/Makefile.am b/src/Makefile.am index d6faa652..c4584dbe 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/evhttp/http.c b/src/evhttp/http.c index 4b76d4c4..e2b992ed 100644 --- a/src/evhttp/http.c +++ b/src/evhttp/http.c @@ -80,7 +80,6 @@ #undef timeout_pending #undef timeout_initialized -#include "strlcpy-internal.h" #include #include "evhttp.h" #include @@ -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, '/'); diff --git a/src/evhttp/strlcpy-internal.h b/src/evhttp/strlcpy-internal.h deleted file mode 100644 index 40242264..00000000 --- a/src/evhttp/strlcpy-internal.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _STRLCPY_INTERNAL_H_ -#define _STRLCPY_INTERNAL_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#ifndef _EVENT_HAVE_STRLCPY -#include -size_t _event_strlcpy(char *dst, const char *src, size_t siz); -#define strlcpy _event_strlcpy -#endif - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/src/evhttp/strlcpy.c b/src/evhttp/strlcpy.c deleted file mode 100644 index 72343e70..00000000 --- a/src/evhttp/strlcpy.c +++ /dev/null @@ -1,74 +0,0 @@ -/* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */ - -/* - * Copyright (c) 1998 Todd C. Miller - * 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 - -#include - -#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