Add OpenBSD's strlcpy and strlcat and replace all strcat/strcpy/strncat/strncpy by those calls.

remotes/origin/master-1.0.x
Victor Julien 16 years ago
parent 7d920a1254
commit 0d34990d7f

@ -132,6 +132,8 @@ util-fix_checksum.c util-fix_checksum.h \
util-daemon.c util-daemon.h \
util-random.c util-random.h \
util-classification-config.c util-classification-config.h \
util-strlcatu.c \
util-strlcpyu.c \
tm-modules.c tm-modules.h \
tm-queues.c tm-queues.h \
tm-queuehandlers.c tm-queuehandlers.h \

@ -71,11 +71,11 @@ void hexdump(const void *buf, size_t len) {
/* store hex str (for left side) */
snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1);
strlcat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1);
/* store char str (for right side) */
snprintf(bytestr, sizeof(bytestr), "%c", c);
strncat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1);
strlcat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1);
if (n % 16 == 0) {
/* line completed */
@ -84,8 +84,8 @@ void hexdump(const void *buf, size_t len) {
charstr[0] = 0;
} else if (n % 8 == 0) {
/* half line: add whitespaces */
strncat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1);
strncat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1);
strlcat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1);
strlcat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1);
}
p++; /* next byte */
}

@ -73,11 +73,11 @@ void hexdump(const void *buf, size_t len) {
/* store hex str (for left side) */
snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1);
strlcat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1);
/* store char str (for right side) */
snprintf(bytestr, sizeof(bytestr), "%c", c);
strncat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1);
strlcat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1);
if (n % 16 == 0) {
/* line completed */
@ -86,8 +86,8 @@ void hexdump(const void *buf, size_t len) {
charstr[0] = 0;
} else if (n % 8 == 0) {
/* half line: add whitespaces */
strncat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1);
strncat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1);
strlcat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1);
strlcat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1);
}
p++; /* next byte */
}

@ -68,9 +68,9 @@ GetKeyName(char **key, int level)
return NULL;
}
else {
strncat(print_key, key[i], strlen(key[i]));
strlcat(print_key, key[i], sizeof(print_key));
if (i < level)
strncat(print_key, ".", 1);
strlcat(print_key, ".", sizeof(print_key));
}
}

@ -406,9 +406,9 @@ ConfPrintNameArray(char **name_arr, int level)
name[0] = '\0';
for (i = 0; i <= level; i++) {
strcat(name, name_arr[i]);
strlcat(name, name_arr[i], sizeof(name));
if (i < level)
strcat(name, ".");
strlcat(name, ".", sizeof(name));
}
return name;

@ -83,7 +83,7 @@ static void DetectContentDebugPrint(DetectContentData *co) {
} else {
snprintf(tmp,sizeof(tmp),"\\%02x", co->content[u]);
}
strncat(buf,tmp,sizeof(buf));
strlcat(buf,tmp,sizeof(buf));
}
SCLogDebug("content \"%s\"",buf);

@ -97,7 +97,7 @@ int DetectMsgSetup (DetectEngineCtx *de_ctx, Signature *s, SigMatch *m, char *ms
if (s->msg == NULL)
goto error;
strncpy(s->msg, str, len + 1);
strlcpy(s->msg, str, len + 1);
free(str);
return 0;

@ -200,12 +200,13 @@ char *DetectLoadCompleteSigPath(char *sig_file)
if (index(sig_file, '/') == NULL) {
if (ConfGet("default-rule-path", &defaultpath) == 1) {
SCLogDebug("Default path: %s", defaultpath);
path = malloc(sizeof(char) * (strlen(defaultpath) +
strlen(sig_file) + 2));
strcpy(path, defaultpath);
size_t path_len = sizeof(char) * (strlen(defaultpath) +
strlen(sig_file) + 2);
path = malloc(path_len);
strlcpy(path, defaultpath, path_len);
if (path[strlen(path) - 1] != '/')
strcat(path, "/");
strcat(path, sig_file);
strlcat(path, "/", path_len);
strlcat(path, sig_file, path_len);
} else {
path = strdup(sig_file);
}

@ -3991,7 +3991,7 @@ static int StreamTcpTest14 (void) {
/* Get the IP address as string and add it to Host info tree for lookups */
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
strcpy(os_policy_name, "linux\0");
strlcpy(os_policy_name, "linux\0", sizeof(os_policy_name));
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
@ -4367,7 +4367,7 @@ static int StreamTcpTest15 (void) {
/* Get the IP address as string and add it to Host info tree for lookups */
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
strcpy(os_policy_name, "linux\0");
strlcpy(os_policy_name, "linux\0", sizeof(os_policy_name));
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
@ -4533,7 +4533,7 @@ static int StreamTcpTest16 (void) {
/* Get the IP address as string and add it to Host info tree for lookups */
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
strcpy(os_policy_name, "linux\0");
strlcpy(os_policy_name, "linux\0", sizeof(os_policy_name));
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
@ -4700,7 +4700,7 @@ static int StreamTcpTest17 (void) {
/* Get the IP address as string and add it to Host info tree for lookups */
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);
strcpy(os_policy_name, "linux\0");
strlcpy(os_policy_name, "linux\0", sizeof(os_policy_name));
ip_addr = StreamTcpParseOSPolicy(os_policy_name);
SCHInfoAddHostOSInfo(os_policy_name, ip_addr, -1);

@ -52,5 +52,8 @@
#define SigIntId uint16_t
//#define SigIntId uint32_t
size_t strlcat(char *, const char *src, size_t siz);
size_t strlcpy(char *dst, const char *src, size_t siz);
#endif /* __SURICATA_COMMON_H__ */

@ -162,7 +162,7 @@ void *PoolTestAlloc(void *allocdata) {
void *PoolTestAllocArg(void *allocdata) {
size_t len = strlen((char *)allocdata) + 1;
char *str = malloc(len);
strncpy(str,(char *)allocdata,len);
strlcpy(str,(char *)allocdata,len);
return (void *)str;
}

@ -0,0 +1,79 @@
/*
* 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.
*/
/* $Id: strlcatu.c,v 1.4 2003/10/20 15:03:27 chrisgreen Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef HAVE_STRLCAT
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcat.c,v 1.5 2001/01/13 16:17:24 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <string.h>
/*
* 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
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(initial dst) + strlen(src); if retval >= siz,
* truncation occurred.
*/
size_t strlcat(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;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif

@ -0,0 +1,75 @@
/*
* 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.
*/
/* $Id: strlcpyu.c,v 1.4 2003/10/20 15:03:27 chrisgreen Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef HAVE_STRLCPY
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <string.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 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
Loading…
Cancel
Save