aboutsummaryrefslogtreecommitdiff
path: root/libs/zmq/patches/020-no-libbsd.patch
blob: 641b69b6e55a1ae33c0f260ce2d7102b58d4f3c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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