diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2003-04-02 20:42:23 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2003-04-02 20:42:23 +0000 |
commit | 1229b08a41cb0455441509e3bc20934ee825f8ab (patch) | |
tree | 69dd2e573803eaf1827fd7bbf2338097fc18a8c7 | |
parent | 4e51ab25aa97d38c6863d8b1297f8f4b7cffbc67 (diff) |
o Use realloc() instead of leaking memory when we need more than 1024 bytes
o use strlcpy()
deraadt@ OK
-rw-r--r-- | usr.sbin/tcpdump/savestr.c | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/usr.sbin/tcpdump/savestr.c b/usr.sbin/tcpdump/savestr.c index 17e089e9949..e9850ccb010 100644 --- a/usr.sbin/tcpdump/savestr.c +++ b/usr.sbin/tcpdump/savestr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: savestr.c,v 1.3 2000/10/03 14:31:59 ho Exp $ */ +/* $OpenBSD: savestr.c,v 1.4 2003/04/02 20:42:22 millert Exp $ */ /* * Copyright (c) 1997 @@ -23,7 +23,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /cvs/OpenBSD/src/usr.sbin/tcpdump/savestr.c,v 1.3 2000/10/03 14:31:59 ho Exp $ (LBL)"; + "@(#) $Header: /cvs/OpenBSD/src/usr.sbin/tcpdump/savestr.c,v 1.4 2003/04/02 20:42:22 millert Exp $ (LBL)"; #endif #include <sys/types.h> @@ -43,23 +43,23 @@ static const char rcsid[] = char * savestr(register const char *str) { - register u_int size; + register size_t size; register char *p; static char *strptr = NULL; - static u_int strsize = 0; + static size_t strsize = 0; size = strlen(str) + 1; if (size > strsize) { strsize = 1024; if (strsize < size) strsize = size; - strptr = (char *)malloc(strsize); + strptr = strptr ? realloc(strptr, strsize) : malloc(strsize); if (strptr == NULL) { - fprintf(stderr, "savestr: malloc\n"); + fprintf(stderr, "savestr: cannot allocate memory\n"); exit(1); } } - (void)strcpy(strptr, str); + (void)strlcpy(strptr, str, size); p = strptr; strptr += size; strsize -= size; |