Attention is currently required from: Arne Schwabe.

Hello Arne Schwabe,

I'd like you to do a code review.
Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1994?usp=email

to review the following change.


Change subject: compat: Add strlcpy and strlcat
......................................................................

compat: Add strlcpy and strlcat

Avaibable on most systems (Linux since
glibc 2.38), but not all. We already
have strncpynt, but switching to the
standard strlcpy makes the code more
accessible for both humans and tools.

This commit only introduces the
possibility to use strlcpy in general
code (we already have one usage in
BSD-specific code). No users are
introduced, yet.

Change-Id: I906b3fad5d521e1e4b0f15aff763cf4d4d8bc35e
Signed-off-by: Frank Lichtenheld <[email protected]>
---
M CMakeLists.txt
M config.h.cmake.in
M configure.ac
A src/compat/compat-strlcpy.c
M src/compat/compat.h
5 files changed, 66 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/94/1994/1

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bac4dd4..825f8b3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -248,6 +248,7 @@

 check_symbol_exists(chsize io.h HAVE_CHSIZE)
 check_symbol_exists(getrlimit sys/resource.h HAVE_GETRLIMIT)
+check_symbol_exists(strlcpy string.h HAVE_STRLCPY)
 check_symbol_exists(strsep string.h HAVE_STRSEP)
 check_symbol_exists(strtok_r string.h HAVE_STRTOK_R)

diff --git a/config.h.cmake.in b/config.h.cmake.in
index 8c3088e..483de45 100644
--- a/config.h.cmake.in
+++ b/config.h.cmake.in
@@ -267,6 +267,9 @@
 /* Define to 1 if you have the <string.h> header file. */
 #define HAVE_STRING_H 1

+/* Define to 1 if you have the `strlcpy' function. */
+#cmakedefine HAVE_STRLCPY
+
 /* Define to 1 if you have the `strsep' function. */
 #cmakedefine HAVE_STRSEP

diff --git a/configure.ac b/configure.ac
index 56109af..ad3f8f1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -568,7 +568,7 @@
        setgroups flock gettimeofday \
        setsid chdir \
        chsize ftruncate execve getpeereid basename dirname \
-       epoll_create strsep strtok_r \
+       epoll_create strlcpy strsep strtok_r \
 ])

 AC_CHECK_LIB(
diff --git a/src/compat/compat-strlcpy.c b/src/compat/compat-strlcpy.c
new file mode 100644
index 0000000..8c1e80a
--- /dev/null
+++ b/src/compat/compat-strlcpy.c
@@ -0,0 +1,52 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2002-2026 OpenVPN Inc <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program 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 this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifndef HAVE_STRLCPY
+#include <string.h>
+
+size_t
+strlcpy(char *dest, const char *src, size_t maxlen)
+{
+    if (maxlen > 0)
+    {
+        strncpy(dest, src, maxlen - 1);
+        dest[maxlen - 1] = 0;
+    }
+    return strlen(src);
+}
+
+size_t
+strlcat(char *dest, const char *src, size_t maxlen)
+{
+    size_t dest_len = strnlen(dest, maxlen);
+    if (maxlen > dest_len)
+    {
+        strncat(dest, src, maxlen - dest_len - 1);
+    }
+    return dest_len + strlen(src);
+}
+
+#endif /* ifndef HAVE_STRLCPY */
diff --git a/src/compat/compat.h b/src/compat/compat.h
index 9e6f95c..4ee9141 100644
--- a/src/compat/compat.h
+++ b/src/compat/compat.h
@@ -56,6 +56,15 @@

 #endif

+#ifndef HAVE_STRLCPY
+size_t
+strlcpy(char *dest, const char *src, size_t maxlen);
+
+size_t
+strlcat(char *dest, const char *src, size_t maxlen);
+
+#endif
+
 #ifndef HAVE_STRSEP
 char *strsep(char **stringp, const char *delim);


--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1994?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I906b3fad5d521e1e4b0f15aff763cf4d4d8bc35e
Gerrit-Change-Number: 1994
Gerrit-PatchSet: 1
Gerrit-Owner: Frank Lichtenheld <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to