From 872a773fac1e2880428d82e9f589ff16a5fde727 Mon Sep 17 00:00:00 2001
From: Guilherme Janczak <guilherme.janczak@yandex.com>
Date: Fri, 6 May 2022 18:42:52 +0000
Subject: [PATCH] remove libbsd

libbsd is only used once and as part of a larger, incorrect function.
I rewrote the code that used it without the need for it.
---
 CMakeLists.txt               | 41 ++++++-----------------------
 builds/cmake/platform.hpp.in |  2 --
 src/compat.hpp               | 51 +++++++++++++++++++++++-------------
 8 files changed, 50 insertions(+), 101 deletions(-)

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1484,10 +1484,6 @@ if(BUILD_SHARED)
     target_link_libraries(libzmq ${NSS3_LIBRARIES})
   endif()
 
-  if(LIBBSD_FOUND)
-    target_link_libraries(libzmq ${LIBBSD_LIBRARIES})
-  endif()
-
   if(SODIUM_FOUND)
     target_link_libraries(libzmq ${SODIUM_LIBRARIES})
     # On Solaris, libsodium depends on libssp
@@ -1534,10 +1530,6 @@ if(BUILD_STATIC)
     target_include_directories(libzmq-static PRIVATE "${GNUTLS_INCLUDE_DIR}")
   endif()
 
-  if(LIBBSD_FOUND)
-    target_link_libraries(libzmq-static ${LIBBSD_LIBRARIES})
-  endif()
-
   if(NSS3_FOUND)
     target_link_libraries(libzmq-static ${NSS3_LIBRARIES})
   endif()
@@ -1607,10 +1599,6 @@ if(BUILD_SHARED)
         target_include_directories(${perf-tool} PRIVATE "${GNUTLS_INCLUDE_DIR}")
       endif()
 
-      if(LIBBSD_FOUND)
-        target_link_libraries(${perf-tool} ${LIBBSD_LIBRARIES})
-      endif()
-
       if(NSS3_FOUND)
         target_link_libraries(${perf-tool} ${NSS3_LIBRARIES})
       endif()
--- a/builds/cmake/platform.hpp.in
+++ b/builds/cmake/platform.hpp.in
@@ -56,8 +56,6 @@
 #cmakedefine ZMQ_HAVE_PTHREAD_SET_AFFINITY
 #cmakedefine HAVE_ACCEPT4
 #cmakedefine HAVE_STRNLEN
-#cmakedefine ZMQ_HAVE_STRLCPY
-#cmakedefine ZMQ_HAVE_LIBBSD
 
 #cmakedefine ZMQ_HAVE_IPC
 #cmakedefine ZMQ_HAVE_STRUCT_SOCKADDR_UN
--- a/src/compat.hpp
+++ b/src/compat.hpp
@@ -10,26 +10,41 @@
 #define strcasecmp _stricmp
 #define strtok_r strtok_s
 #else
-#ifndef ZMQ_HAVE_STRLCPY
-#ifdef ZMQ_HAVE_LIBBSD
-#include <bsd/string.h>
-#else
-static inline size_t
-strlcpy (char *dest_, const char *src_, const size_t dest_size_)
-{
-    size_t remain = dest_size_;
-    for (; remain && *src_; --remain, ++src_, ++dest_) {
-        *dest_ = *src_;
-    }
-    return dest_size_ - remain;
-}
-#endif
-#endif
+/*
+ * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170
+ */
 template <size_t size>
-static inline int strcpy_s (char (&dest_)[size], const char *const src_)
+static inline int strcpy_s (char (&dst)[size], const char *const src)
 {
-    const size_t res = strlcpy (dest_, src_, size);
-    return res >= size ? ERANGE : 0;
+    size_t i;
+
+    if (src == NULL) {
+        /*
+         * XXX:
+         * Microsoft's documentation is ambiguous.
+         *
+         * How does Microsoft handle size == 0 when src is NULL?
+         * Do they return ERANGE?
+         *
+         * How does Microsoft handle size == 0 when src is non-NULL?
+         * Do they write a '\0' to *dst anyway?
+         */
+        if (size > 0)
+            *dst = '\0';
+        return (errno = EINVAL);
+    }
+
+    for (i = 0;; i++) {
+        if (i >= size) {
+            if (size > 0)
+                *dst = '\0';
+            return (errno = ERANGE);
+        }
+        dst[i] = src[i];
+        if (src[i] == '\0')
+            return 0;
+    }
+    /* NOTREACHED */
 }
 #endif
 
