]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: replace strlcpy & strlcat with glibc versions
authorDavid Lamparter <equinox@opensourcerouting.org>
Tue, 15 Nov 2016 08:15:49 +0000 (17:15 +0900)
committerDavid Lamparter <equinox@opensourcerouting.org>
Tue, 15 Nov 2016 08:35:36 +0000 (17:35 +0900)
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>
22 files changed:
bgpd/bgp_aspath.c
bgpd/bgp_debug.c
bgpd/bgp_route.c
bgpd/bgp_updgrp.c
bgpd/bgp_updgrp_packet.c
bgpd/bgpd.c
bgpd/rfapi/vnc_debug.c
lib/Makefile.am
lib/if.c
lib/nexthop.c
lib/sockunion.c
lib/str.c [deleted file]
lib/str.h [deleted file]
lib/strlcat.c [new file with mode: 0644]
lib/strlcpy.c [new file with mode: 0644]
lib/vty.c
lib/zebra.h
zebra/kernel_socket.c
zebra/rt_socket.c
zebra/zebra_mpls.c
zebra/zebra_rib.c
zebra/zebra_rnh.c

index bf1e92b26d430a1033f5cba17106042c44e94ebd..89a529d7387376c51407edc61bc7af910bfb0424 100644 (file)
@@ -25,7 +25,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "memory.h"
 #include "vector.h"
 #include "vty.h"
-#include "str.h"
 #include "log.h"
 #include "stream.h"
 #include "jhash.h"
index 4d9ff60e92c72ee3bd9c751bd20f5650e30ea3e1..46d3afbf79ef59931fecbef33d9cc708b170594c 100644 (file)
@@ -25,7 +25,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "linklist.h"
 #include "stream.h"
 #include "command.h"
-#include "str.h"
 #include "log.h"
 #include "sockunion.h"
 #include "memory.h"
index 57e38ce10c3f4470a6ee4d2e02167364a29700cb..8520466995722a6e37df4dbbea60f77bd5d55460 100644 (file)
@@ -27,7 +27,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "command.h"
 #include "stream.h"
 #include "filter.h"
-#include "str.h"
 #include "log.h"
 #include "routemap.h"
 #include "buffer.h"
index 77b979e56e67d02fc45d22ac0c56f545dfbd8f7b..9e5ec4b26de64de285c1994e1749917025c51548 100644 (file)
@@ -37,7 +37,6 @@
 #include "memory.h"
 #include "filter.h"
 #include "routemap.h"
-#include "str.h"
 #include "log.h"
 #include "plist.h"
 #include "linklist.h"
index 3cb3ddcdea256061eb420079fdd1a2e8f4e9f5be..e18ba235c702c8428ab405c58bf49c63bcedb5a6 100644 (file)
@@ -37,7 +37,6 @@
 #include "memory.h"
 #include "filter.h"
 #include "routemap.h"
-#include "str.h"
 #include "log.h"
 #include "plist.h"
 #include "linklist.h"
index f804022981a8045a851a8f17bf8d490fc1b344a9..7554b512e8efd44e4208e89e81d38963c517244c 100644 (file)
@@ -32,7 +32,6 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "memory.h"
 #include "filter.h"
 #include "routemap.h"
-#include "str.h"
 #include "log.h"
 #include "plist.h"
 #include "linklist.h"
index eaa8c56ee98d49f952ae85f8d1208cd46e53f862..b1cf4f43e6a621b1429d4c6e7481484e44c1294e 100644 (file)
@@ -25,7 +25,6 @@
 #include "lib/linklist.h"
 #include "lib/stream.h"
 #include "lib/command.h"
-#include "lib/str.h"
 #include "lib/log.h"
 #include "bgpd/rfapi/vnc_debug.h"
 
index 1d2392f0ed32080546ad28951849ff888aa4c0fc..72395068137f86669e2d180ceaaff10353450917 100644 (file)
@@ -12,13 +12,15 @@ libzebra_la_SOURCES = \
        network.c pid_output.c getopt.c getopt1.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 \
-       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 \
        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 \
        imsg-buffer.c imsg.c skiplist.c \
        qobj.c \
-       event_counter.c
+       event_counter.c \
+       strlcpy.c \
+       strlcat.c
 
 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 \
        if.h linklist.h log.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 \
        privs.h sigevent.h pqueue.h jhash.h zassert.h \
        workqueue.h route_types.h libospf.h nexthop.h json.h \
index 0932fa1acde74e9926bbbaeaf475cc30d3fbd81c..df53f8d4e8339808ec3067c6f9f4d1a48ba45c2b 100644 (file)
--- a/lib/if.c
+++ b/lib/if.c
@@ -34,7 +34,6 @@
 #include "memory.h"
 #include "table.h"
 #include "buffer.h"
-#include "str.h"
 #include "log.h"
 
 DEFINE_MTYPE(       LIB, IF,              "Interface")
index 23ee28b7dcb58ec1ff3fca0956155fc378c04ef9..d7d8f0cd30430f9f8684dbab3e3e364427a5476c 100644 (file)
@@ -23,7 +23,6 @@
 #include "prefix.h"
 #include "table.h"
 #include "memory.h"
-#include "str.h"
 #include "command.h"
 #include "if.h"
 #include "log.h"
index 2aa19dc74cb68c93ec6abd494a7fad7090ae8760..f4b6ce12cc4d9cb9d5c6b41717b6796e83a606e1 100644 (file)
@@ -25,7 +25,6 @@
 #include "vty.h"
 #include "sockunion.h"
 #include "memory.h"
-#include "str.h"
 #include "log.h"
 #include "jhash.h"
 
diff --git a/lib/str.c b/lib/str.c
deleted file mode 100644 (file)
index 619cffd..0000000
--- a/lib/str.c
+++ /dev/null
@@ -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
diff --git a/lib/str.h b/lib/str.h
deleted file mode 100644 (file)
index 9c0e515..0000000
--- a/lib/str.h
+++ /dev/null
@@ -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 */
-
diff --git a/lib/strlcat.c b/lib/strlcat.c
new file mode 100644 (file)
index 0000000..1d04b43
--- /dev/null
@@ -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 */
diff --git a/lib/strlcpy.c b/lib/strlcpy.c
new file mode 100644 (file)
index 0000000..41bab97
--- /dev/null
@@ -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 */
index 26d0b67ecf7d5492df6afd665194814f4b951449..b25a4eeffd8192eddc3ce60cc668e6082d0780d9 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -29,7 +29,6 @@
 #include "command.h"
 #include "sockunion.h"
 #include "memory.h"
-#include "str.h"
 #include "log.h"
 #include "prefix.h"
 #include "filter.h"
index 9a73d2d30701037566025246cc0acbd5b19f41c0..a67713c30d5e9d129bb3090d095ff4a423350047 100644 (file)
@@ -221,8 +221,13 @@ typedef unsigned char   u_int8_t;
 #endif  /* !__GNUC__ || VTYSH_EXTRACT_PL */
 
 #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
 /* This bug is present in Solaris 8 and pre-patch Solaris 9 <sys/socket.h>;
index 6cb9f785982c60452e3be318b2a610d19dac75bf..bcd92e15fea0e0130d4f8f68cc3fefe02e0e326b 100644 (file)
@@ -33,7 +33,6 @@
 #include "zebra_memory.h"
 #include "ioctl.h"
 #include "log.h"
-#include "str.h"
 #include "table.h"
 #include "rib.h"
 #include "privs.h"
index 2bde3f4b0862d70b2915e484740d880fb1b2cba0..b2c99d98136f91a98080726f0e3c67087e95a602 100644 (file)
@@ -29,7 +29,6 @@
 #include "prefix.h"
 #include "sockunion.h"
 #include "log.h"
-#include "str.h"
 #include "privs.h"
 
 #include "zebra/debug.h"
index 15e5c330e48ab253028c8d2895bdea188e63aa75..3b19a5da06e1f969be0bf628a33400dfa34d942c 100644 (file)
@@ -24,7 +24,6 @@
 #include "prefix.h"
 #include "table.h"
 #include "memory.h"
-#include "str.h"
 #include "command.h"
 #include "if.h"
 #include "log.h"
index 394469950dde335618a90bffbd74ee5bcade2eba..8b36eb8060d63b07626a3f6cb5a67a9c6d7b8d41 100644 (file)
@@ -26,7 +26,6 @@
 #include "table.h"
 #include "memory.h"
 #include "zebra_memory.h"
-#include "str.h"
 #include "command.h"
 #include "log.h"
 #include "sockunion.h"
index fdb187a23a6db089d76d37c19106084b904f0bfe..f88a6f0a98923058c0213c62ca3a319d13440fac 100644 (file)
@@ -24,7 +24,6 @@
 #include "prefix.h"
 #include "table.h"
 #include "memory.h"
-#include "str.h"
 #include "command.h"
 #include "if.h"
 #include "log.h"