lib: replace strlcpy & strlcat with glibc versions

It seems these two were at some point copied in from rsync; replace with
more recent versions that will hopefully become available in glibc as
well.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2016-11-15 17:15:49 +09:00
parent 8ce70b1f5e
commit c5d9d3bb36
22 changed files with 138 additions and 132 deletions

View File

@ -25,7 +25,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#include "memory.h" #include "memory.h"
#include "vector.h" #include "vector.h"
#include "vty.h" #include "vty.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "stream.h" #include "stream.h"
#include "jhash.h" #include "jhash.h"

View File

@ -25,7 +25,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#include "linklist.h" #include "linklist.h"
#include "stream.h" #include "stream.h"
#include "command.h" #include "command.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "sockunion.h" #include "sockunion.h"
#include "memory.h" #include "memory.h"

View File

@ -27,7 +27,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#include "command.h" #include "command.h"
#include "stream.h" #include "stream.h"
#include "filter.h" #include "filter.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "routemap.h" #include "routemap.h"
#include "buffer.h" #include "buffer.h"

View File

@ -37,7 +37,6 @@
#include "memory.h" #include "memory.h"
#include "filter.h" #include "filter.h"
#include "routemap.h" #include "routemap.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "plist.h" #include "plist.h"
#include "linklist.h" #include "linklist.h"

View File

@ -37,7 +37,6 @@
#include "memory.h" #include "memory.h"
#include "filter.h" #include "filter.h"
#include "routemap.h" #include "routemap.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "plist.h" #include "plist.h"
#include "linklist.h" #include "linklist.h"

View File

@ -32,7 +32,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#include "memory.h" #include "memory.h"
#include "filter.h" #include "filter.h"
#include "routemap.h" #include "routemap.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "plist.h" #include "plist.h"
#include "linklist.h" #include "linklist.h"

View File

@ -25,7 +25,6 @@
#include "lib/linklist.h" #include "lib/linklist.h"
#include "lib/stream.h" #include "lib/stream.h"
#include "lib/command.h" #include "lib/command.h"
#include "lib/str.h"
#include "lib/log.h" #include "lib/log.h"
#include "bgpd/rfapi/vnc_debug.h" #include "bgpd/rfapi/vnc_debug.h"

View File

@ -12,13 +12,15 @@ libzebra_la_SOURCES = \
network.c pid_output.c getopt.c getopt1.c \ network.c pid_output.c getopt.c getopt1.c \
checksum.c vector.c linklist.c vty.c command.c \ checksum.c vector.c linklist.c vty.c command.c \
sockunion.c prefix.c thread.c if.c buffer.c table.c hash.c \ sockunion.c prefix.c thread.c if.c buffer.c table.c hash.c \
filter.c routemap.c distribute.c stream.c str.c log.c plist.c \ filter.c routemap.c distribute.c stream.c log.c plist.c \
zclient.c sockopt.c smux.c agentx.c snmp.c md5.c if_rmap.c keychain.c privs.c \ zclient.c sockopt.c smux.c agentx.c snmp.c md5.c if_rmap.c keychain.c privs.c \
sigevent.c pqueue.c jhash.c workqueue.c nexthop.c json.c \ sigevent.c pqueue.c jhash.c workqueue.c nexthop.c json.c \
ptm_lib.c csv.c bfd.c vrf.c systemd.c ns.c memory.c memory_vty.c \ ptm_lib.c csv.c bfd.c vrf.c systemd.c ns.c memory.c memory_vty.c \
imsg-buffer.c imsg.c skiplist.c \ imsg-buffer.c imsg.c skiplist.c \
qobj.c \ qobj.c \
event_counter.c event_counter.c \
strlcpy.c \
strlcat.c
BUILT_SOURCES = route_types.h gitversion.h BUILT_SOURCES = route_types.h gitversion.h
@ -28,7 +30,7 @@ pkginclude_HEADERS = \
buffer.h checksum.h command.h filter.h getopt.h hash.h \ buffer.h checksum.h command.h filter.h getopt.h hash.h \
if.h linklist.h log.h \ if.h linklist.h log.h \
memory.h network.h prefix.h routemap.h distribute.h sockunion.h \ memory.h network.h prefix.h routemap.h distribute.h sockunion.h \
str.h stream.h table.h thread.h vector.h version.h vty.h zebra.h \ stream.h table.h thread.h vector.h version.h vty.h zebra.h \
plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \ plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \
privs.h sigevent.h pqueue.h jhash.h zassert.h \ privs.h sigevent.h pqueue.h jhash.h zassert.h \
workqueue.h route_types.h libospf.h nexthop.h json.h \ workqueue.h route_types.h libospf.h nexthop.h json.h \

View File

@ -34,7 +34,6 @@
#include "memory.h" #include "memory.h"
#include "table.h" #include "table.h"
#include "buffer.h" #include "buffer.h"
#include "str.h"
#include "log.h" #include "log.h"
DEFINE_MTYPE( LIB, IF, "Interface") DEFINE_MTYPE( LIB, IF, "Interface")

View File

@ -23,7 +23,6 @@
#include "prefix.h" #include "prefix.h"
#include "table.h" #include "table.h"
#include "memory.h" #include "memory.h"
#include "str.h"
#include "command.h" #include "command.h"
#include "if.h" #include "if.h"
#include "log.h" #include "log.h"

View File

@ -25,7 +25,6 @@
#include "vty.h" #include "vty.h"
#include "sockunion.h" #include "sockunion.h"
#include "memory.h" #include "memory.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "jhash.h" #include "jhash.h"

View File

@ -1,95 +0,0 @@
/*
* zebra string function
*
* XXX This version of snprintf does not check bounds!
*/
/*
The implementations of strlcpy and strlcat are copied from rsync (GPL):
Copyright (C) Andrew Tridgell 1998
Copyright (C) 2002 by Martin Pool
Note that these are not terribly efficient, since they make more than one
pass over the argument strings. At some point, they should be optimized.
The implementation of strndup is copied from glibc-2.3.5:
Copyright (C) 1996, 1997, 1998, 2001, 2002 Free Software Foundation, Inc.
*/
/*
* This file is part of Quagga.
*
* Quagga is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* Quagga is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Quagga; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#ifndef HAVE_STRLCPY
/*
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(src - osrc - 1); /* count does not include NUL */
}
#endif
#ifndef HAVE_STRLCAT
/**
* Like strncat() but does not 0 fill the buffer and always null
* terminates.
*
* @param bufsize length of the buffer, which should be one more than
* the maximum resulting string length.
**/
size_t
strlcat(char *d, const char *s, size_t bufsize)
{
size_t len1 = strlen(d);
size_t len2 = strlen(s);
size_t ret = len1 + len2;
if (len1 < bufsize - 1) {
if (len2 >= bufsize - len1)
len2 = bufsize - len1 - 1;
memcpy(d+len1, s, len2);
d[len1+len2] = 0;
}
return ret;
}
#endif

View File

@ -1,17 +0,0 @@
/*
* $Id: str.h,v 1.4 2005/09/19 09:53:21 hasso Exp $
*/
#ifndef _ZEBRA_STR_H
#define _ZEBRA_STR_H
#ifndef HAVE_STRLCPY
extern size_t strlcpy(char *, const char *, size_t);
#endif
#ifndef HAVE_STRLCAT
extern size_t strlcat(char *, const char *, size_t);
#endif
#endif /* _ZEBRA_STR_H */

71
lib/strlcat.c Normal file
View File

@ -0,0 +1,71 @@
/* Append a null-terminated string to another string, with length checking.
Copyright (C) 2016 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* adapted for Quagga from glibc patch submission originally from
* Florian Weimer <fweimer@redhat.com>, 2016-05-18 */
#include <stdint.h>
#include <string.h>
#include "config.h"
#ifndef HAVE_STRLCAT
#undef strlcat
size_t
strlcat (char *__restrict dest, const char *__restrict src, size_t size);
size_t
strlcat (char *__restrict dest, const char *__restrict src, size_t size)
{
size_t src_length = strlen (src);
/* Our implementation strlcat supports dest == NULL if size == 0
(for consistency with snprintf and strlcpy), but strnlen does
not, so we have to cover this case explicitly. */
if (size == 0)
return src_length;
size_t dest_length = strnlen (dest, size);
if (dest_length != size)
{
/* Copy at most the remaining number of characters in the
destination buffer. Leave for the NUL terminator. */
size_t to_copy = size - dest_length - 1;
/* But not more than what is available in the source string. */
if (to_copy > src_length)
to_copy = src_length;
char *target = dest + dest_length;
memcpy (target, src, to_copy);
target[to_copy] = '\0';
}
/* If the sum wraps around, we have more than SIZE_MAX + 2 bytes in
the two input strings (including both null terminators). If each
byte in the address space can be assigned a unique size_t value
(which the static_assert checks), then by the pigeonhole
principle, the two input strings must overlap, which is
undefined. */
#if __STDC_VERSION__ >= 201112L
_Static_assert (sizeof (uintptr_t) == sizeof (size_t),
"theoretical maximum object size covers address space");
#endif
return dest_length + src_length;
}
#endif /* HAVE_STRLCAT */

56
lib/strlcpy.c Normal file
View File

@ -0,0 +1,56 @@
/* Copy a null-terminated string to a fixed-size buffer, with length checking.
Copyright (C) 2016 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* adapted for Quagga from glibc patch submission originally from
* Florian Weimer <fweimer@redhat.com>, 2016-05-18 */
#include <string.h>
#include "config.h"
#ifndef HAVE_STRLCPY
#undef strlcpy
size_t
strlcpy (char *__restrict dest, const char *__restrict src, size_t size);
size_t
strlcpy (char *__restrict dest, const char *__restrict src, size_t size)
{
size_t src_length = strlen (src);
if (__builtin_expect (src_length >= size, 0))
{
if (size > 0)
{
/* Copy the leading portion of the string. The last
character is subsequently overwritten with the NUL
terminator, but the destination size is usually a
multiple of a small power of two, so writing it twice
should be more efficient than copying an odd number of
bytes. */
memcpy (dest, src, size);
dest[size - 1] = '\0';
}
}
else
/* Copy the string and its terminating NUL character. */
memcpy (dest, src, src_length + 1);
return src_length;
}
#endif /* HAVE_STRLCPY */

View File

@ -29,7 +29,6 @@
#include "command.h" #include "command.h"
#include "sockunion.h" #include "sockunion.h"
#include "memory.h" #include "memory.h"
#include "str.h"
#include "log.h" #include "log.h"
#include "prefix.h" #include "prefix.h"
#include "filter.h" #include "filter.h"

View File

@ -221,8 +221,13 @@ typedef unsigned char u_int8_t;
#endif /* !__GNUC__ || VTYSH_EXTRACT_PL */ #endif /* !__GNUC__ || VTYSH_EXTRACT_PL */
#include "zassert.h" #include "zassert.h"
#include "str.h"
#ifndef HAVE_STRLCAT
size_t strlcat (char *__restrict dest, const char *__restrict src, size_t size);
#endif
#ifndef HAVE_STRLCPY
size_t strlcpy (char *__restrict dest, const char *__restrict src, size_t size);
#endif
#ifdef HAVE_BROKEN_CMSG_FIRSTHDR #ifdef HAVE_BROKEN_CMSG_FIRSTHDR
/* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>; /* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>;

View File

@ -33,7 +33,6 @@
#include "zebra_memory.h" #include "zebra_memory.h"
#include "ioctl.h" #include "ioctl.h"
#include "log.h" #include "log.h"
#include "str.h"
#include "table.h" #include "table.h"
#include "rib.h" #include "rib.h"
#include "privs.h" #include "privs.h"

View File

@ -29,7 +29,6 @@
#include "prefix.h" #include "prefix.h"
#include "sockunion.h" #include "sockunion.h"
#include "log.h" #include "log.h"
#include "str.h"
#include "privs.h" #include "privs.h"
#include "zebra/debug.h" #include "zebra/debug.h"

View File

@ -24,7 +24,6 @@
#include "prefix.h" #include "prefix.h"
#include "table.h" #include "table.h"
#include "memory.h" #include "memory.h"
#include "str.h"
#include "command.h" #include "command.h"
#include "if.h" #include "if.h"
#include "log.h" #include "log.h"

View File

@ -26,7 +26,6 @@
#include "table.h" #include "table.h"
#include "memory.h" #include "memory.h"
#include "zebra_memory.h" #include "zebra_memory.h"
#include "str.h"
#include "command.h" #include "command.h"
#include "log.h" #include "log.h"
#include "sockunion.h" #include "sockunion.h"

View File

@ -24,7 +24,6 @@
#include "prefix.h" #include "prefix.h"
#include "table.h" #include "table.h"
#include "memory.h" #include "memory.h"
#include "str.h"
#include "command.h" #include "command.h"
#include "if.h" #include "if.h"
#include "log.h" #include "log.h"