Skip to content

Commit 1babb1a

Browse files
leogrpoiana
authored andcommitted
build: patch plugin_loader.c to use an internal strlcat impl
Signed-off-by: Leonardo Grasso <[email protected]>
1 parent 8a28f32 commit 1babb1a

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pluginlib:
3838
$(CURL) -Lso pkg/loader/plugin_loader.c $(PLUGINLIB_URL)/plugin_loader.c
3939
$(PATCH) -p1 < pkg/loader/plugin_api_include.patch
4040
$(PATCH) -p1 < pkg/loader/strlcpy.patch
41+
$(PATCH) -p1 < pkg/loader/strlcat.patch
4142
$(PATCH) -p1 < pkg/sdk/plugin_types_include.patch
4243

4344
clean-pluginlib:

pkg/loader/plugin_loader.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef void* library_handle_t;
3333
// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs
3434
// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38,
3535
// the func has been renamed to plugin_loader_strlcpy.
36+
// The same applies to `strlcat`.
3637
// N.B.: our building system here is not smart enough to detect if the function
3738
// was declared already.
3839
#include <stdint.h>
@@ -61,6 +62,37 @@ static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t si
6162
return srcsize;
6263
}
6364

65+
/*!
66+
\brief Append the NUL-terminated string src to the end of dst. It will append at most size −
67+
strlen(dst) − 1 bytes, NUL-terminating the result.
68+
69+
\return The initial length of dst plus the length of src
70+
*/
71+
72+
static inline size_t plugin_loader_strlcat(char *dst, const char *src, size_t size) {
73+
size_t srcsize = strlen(src);
74+
size_t dstsize = strlen(dst);
75+
76+
if(dstsize >= size) {
77+
return size;
78+
}
79+
80+
if(srcsize == 0) {
81+
return dstsize;
82+
}
83+
84+
size_t totalsize = srcsize + dstsize;
85+
if(totalsize > size - 1) {
86+
totalsize = size - 1;
87+
}
88+
89+
size_t copysize = totalsize - dstsize;
90+
memcpy(dst + dstsize, src, copysize);
91+
dst[totalsize] = '\0';
92+
93+
return dstsize + srcsize;
94+
}
95+
6496
static inline void err_prepend(char* s, const char* prefix, const char* sep) {
6597
char tmp[PLUGIN_MAX_ERRLEN];
6698
size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN);
@@ -74,9 +106,9 @@ static inline void err_prepend(char* s, const char* prefix, const char* sep) {
74106

75107
static inline void err_append(char* s, const char* suffix, const char* sep) {
76108
if(*s != '\0') {
77-
strlcat(s, sep, PLUGIN_MAX_ERRLEN);
109+
plugin_loader_strlcat(s, sep, PLUGIN_MAX_ERRLEN);
78110
}
79-
strlcat(s, suffix, PLUGIN_MAX_ERRLEN);
111+
plugin_loader_strlcat(s, suffix, PLUGIN_MAX_ERRLEN);
80112
}
81113

82114
static void* getsym(library_handle_t handle, const char* name) {

pkg/loader/strlcat.patch

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
diff --git a/pkg/loader/plugin_loader.c b/pkg/loader/plugin_loader.c
2+
index 3aea70c..a68327a 100644
3+
--- a/pkg/loader/plugin_loader.c
4+
+++ b/pkg/loader/plugin_loader.c
5+
@@ -33,6 +33,7 @@ typedef void* library_handle_t;
6+
// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs
7+
// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38,
8+
// the func has been renamed to plugin_loader_strlcpy.
9+
+// The same applies to `strlcat`.
10+
// N.B.: our building system here is not smart enough to detect if the function
11+
// was declared already.
12+
#include <stdint.h>
13+
@@ -61,6 +62,37 @@ static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t si
14+
return srcsize;
15+
}
16+
17+
+/*!
18+
+ \brief Append the NUL-terminated string src to the end of dst. It will append at most size −
19+
+ strlen(dst) − 1 bytes, NUL-terminating the result.
20+
+
21+
+ \return The initial length of dst plus the length of src
22+
+*/
23+
+
24+
+static inline size_t plugin_loader_strlcat(char *dst, const char *src, size_t size) {
25+
+ size_t srcsize = strlen(src);
26+
+ size_t dstsize = strlen(dst);
27+
+
28+
+ if(dstsize >= size) {
29+
+ return size;
30+
+ }
31+
+
32+
+ if(srcsize == 0) {
33+
+ return dstsize;
34+
+ }
35+
+
36+
+ size_t totalsize = srcsize + dstsize;
37+
+ if(totalsize > size - 1) {
38+
+ totalsize = size - 1;
39+
+ }
40+
+
41+
+ size_t copysize = totalsize - dstsize;
42+
+ memcpy(dst + dstsize, src, copysize);
43+
+ dst[totalsize] = '\0';
44+
+
45+
+ return dstsize + srcsize;
46+
+}
47+
+
48+
static inline void err_prepend(char* s, const char* prefix, const char* sep) {
49+
char tmp[PLUGIN_MAX_ERRLEN];
50+
size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN);
51+
@@ -74,9 +106,9 @@ static inline void err_prepend(char* s, const char* prefix, const char* sep) {
52+
53+
static inline void err_append(char* s, const char* suffix, const char* sep) {
54+
if(*s != '\0') {
55+
- strlcat(s, sep, PLUGIN_MAX_ERRLEN);
56+
+ plugin_loader_strlcat(s, sep, PLUGIN_MAX_ERRLEN);
57+
}
58+
- strlcat(s, suffix, PLUGIN_MAX_ERRLEN);
59+
+ plugin_loader_strlcat(s, suffix, PLUGIN_MAX_ERRLEN);
60+
}
61+
62+
static void* getsym(library_handle_t handle, const char* name) {

0 commit comments

Comments
 (0)