|
| 1 | +diff --git a/pkg/loader/plugin_loader.c b/pkg/loader/plugin_loader.c |
| 2 | +index aad119f..169f696 100644 |
| 3 | +--- a/pkg/loader/plugin_loader.c |
| 4 | ++++ b/pkg/loader/plugin_loader.c |
| 5 | +@@ -23,23 +23,53 @@ limitations under the License. |
| 6 | + typedef void* library_handle_t; |
| 7 | + #endif |
| 8 | + |
| 9 | +-#include "strlcpy.h" |
| 10 | + #include <stdio.h> |
| 11 | + #include <stdlib.h> |
| 12 | + #include <string.h> |
| 13 | + #include "plugin_loader.h" |
| 14 | + |
| 15 | ++// note(jasondellaluce,therealbobo): implementation taken from falcosecurity/libs |
| 16 | ++// note(leogr): to avoid clashing with `strlcpy` introduced by glibc 2.38, |
| 17 | ++// the func has been renamed to plugin_loader_strlcpy. |
| 18 | ++// N.B.: our building system here is not smart enough to detect if the function |
| 19 | ++// was declared already. |
| 20 | ++#include <stdint.h> |
| 21 | ++#include <string.h> |
| 22 | ++/*! |
| 23 | ++ \brief Copy up to size - 1 characters from the NUL-terminated string src to dst, NUL-terminating the result. |
| 24 | ++ |
| 25 | ++ \return The length of the source string. |
| 26 | ++*/ |
| 27 | ++ |
| 28 | ++static inline size_t plugin_loader_strlcpy(char *dst, const char *src, size_t size) { |
| 29 | ++ size_t srcsize = strlen(src); |
| 30 | ++ if (size == 0) { |
| 31 | ++ return srcsize; |
| 32 | ++ } |
| 33 | ++ |
| 34 | ++ size_t copysize = srcsize; |
| 35 | ++ |
| 36 | ++ if (copysize > size - 1) { |
| 37 | ++ copysize = size - 1; |
| 38 | ++ } |
| 39 | ++ |
| 40 | ++ memcpy(dst, src, copysize); |
| 41 | ++ dst[copysize] = '\0'; |
| 42 | ++ |
| 43 | ++ return srcsize; |
| 44 | ++} |
| 45 | ++ |
| 46 | + static inline void err_prepend(char* s, const char* prefix, const char* sep) |
| 47 | + { |
| 48 | + char tmp[PLUGIN_MAX_ERRLEN]; |
| 49 | +- size_t prefix_len = strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN); |
| 50 | ++ size_t prefix_len = plugin_loader_strlcpy(tmp, prefix, PLUGIN_MAX_ERRLEN); |
| 51 | + if (*s != '\0') |
| 52 | + { |
| 53 | +- strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len); |
| 54 | ++ plugin_loader_strlcpy(&tmp[prefix_len], sep, PLUGIN_MAX_ERRLEN - prefix_len); |
| 55 | + prefix_len += strlen(sep); |
| 56 | + } |
| 57 | +- strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len); |
| 58 | +- strlcpy(s, tmp, PLUGIN_MAX_ERRLEN); |
| 59 | ++ plugin_loader_strlcpy(&tmp[prefix_len], s, PLUGIN_MAX_ERRLEN - prefix_len); |
| 60 | ++ plugin_loader_strlcpy(s, tmp, PLUGIN_MAX_ERRLEN); |
| 61 | + } |
| 62 | + |
| 63 | + static inline void err_append(char* s, const char* suffix, const char* sep) |
| 64 | +@@ -71,7 +101,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) |
| 65 | + plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t)); |
| 66 | + if (!ret) |
| 67 | + { |
| 68 | +- strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); |
| 69 | ++ plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); |
| 70 | + return NULL; |
| 71 | + } |
| 72 | + |
| 73 | +@@ -86,7 +116,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) |
| 74 | + LPTSTR msg_buf = 0; |
| 75 | + if (FormatMessageA(flg, 0, GetLastError(), 0, (LPTSTR) &msg_buf, 0, NULL) && msg_buf) |
| 76 | + { |
| 77 | +- strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN); |
| 78 | ++ plugin_loader_strlcpy(err, msg_buf, PLUGIN_MAX_ERRLEN); |
| 79 | + LocalFree(msg_buf); |
| 80 | + } |
| 81 | + } |
| 82 | +@@ -94,7 +124,7 @@ plugin_handle_t* plugin_load(const char* path, char* err) |
| 83 | + ret->handle = dlopen(path, RTLD_LAZY); |
| 84 | + if (ret->handle == NULL) |
| 85 | + { |
| 86 | +- strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN); |
| 87 | ++ plugin_loader_strlcpy(err, (const char*) dlerror(), PLUGIN_MAX_ERRLEN); |
| 88 | + } |
| 89 | + #endif |
| 90 | + |
| 91 | +@@ -143,14 +173,14 @@ plugin_handle_t* plugin_load_api(const plugin_api* api, char* err) |
| 92 | + err[0] = '\0'; |
| 93 | + if (!api) |
| 94 | + { |
| 95 | +- strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN); |
| 96 | ++ plugin_loader_strlcpy(err, "can't allocate plugin handle with invalid API table", PLUGIN_MAX_ERRLEN); |
| 97 | + return NULL; |
| 98 | + } |
| 99 | + |
| 100 | + plugin_handle_t* ret = (plugin_handle_t*) calloc (1, sizeof(plugin_handle_t)); |
| 101 | + if (!ret) |
| 102 | + { |
| 103 | +- strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); |
| 104 | ++ plugin_loader_strlcpy(err, "error allocating plugin handle", PLUGIN_MAX_ERRLEN); |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + ret->api = *api; |
| 108 | +@@ -203,7 +233,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err) |
| 109 | + const char *ver, *failmsg; |
| 110 | + if (h->api.get_required_api_version == NULL) |
| 111 | + { |
| 112 | +- strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN); |
| 113 | ++ plugin_loader_strlcpy(err, "plugin_get_required_api_version symbol not implemented", PLUGIN_MAX_ERRLEN); |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | +@@ -243,7 +273,7 @@ bool plugin_check_required_api_version(const plugin_handle_t* h, char* err) |
| 118 | + plugin_caps_t plugin_get_capabilities(const plugin_handle_t* h, char* err) |
| 119 | + { |
| 120 | + plugin_caps_t caps = CAP_NONE; |
| 121 | +- strlcpy(err, "", PLUGIN_MAX_ERRLEN); |
| 122 | ++ plugin_loader_strlcpy(err, "", PLUGIN_MAX_ERRLEN); |
| 123 | + |
| 124 | + if (h->api.open != NULL && h->api.close != NULL && h->api.next_batch != NULL) |
| 125 | + { |
0 commit comments