Skip to content

Commit 56cd265

Browse files
committed
fix(plugin): Use UTF-8 paths on Windows
Document that plugin paths should be UTF-8 on Windows, and convert them to UTF-16 internally. In sinsp-example, add an application manifest that sets activeCodePage to UTF-8 on Windows. Signed-off-by: Gerald Combs <[email protected]>
1 parent ff19858 commit 56cd265

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

userspace/libsinsp/examples/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ include(jsoncpp)
1919
set(sources util.cpp test.cpp)
2020

2121
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
22-
set(sources ${sources} perftest.cpp)
22+
list(APPEND sources perftest.cpp)
23+
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
24+
list(APPEND sources sinsp-example.manifest)
2325
endif()
2426

2527
add_executable(sinsp-example ${sources})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity type="win32" name="FalcoSecurity.SinspExample" version="6.0.0.0"/>
4+
<application>
5+
<windowsSettings>
6+
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
7+
</windowsSettings>
8+
</application>
9+
</assembly>

userspace/libsinsp/sinsp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ class SINSP_PUBLIC sinsp : public capture_stats_source {
734734
// Create and register a plugin from a shared library pointed
735735
// to by filepath, and add it to the inspector.
736736
// The created sinsp_plugin is returned.
737+
// filepath must be encoded as UTF-8 on Windows.
738+
737739
std::shared_ptr<sinsp_plugin> register_plugin(const std::string& filepath);
738740

739741
// Create and register a plugin given a custom API vtable.

userspace/plugin/plugin_loader.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,24 @@ plugin_handle_t* plugin_load(const char* path, char* err) {
7171

7272
// open dynamic library
7373
#ifdef _WIN32
74-
ret->handle = LoadLibrary(path);
74+
// Using LoadLibraryW ensures that we have a valid path independent
75+
// of the system or application code page.
76+
int wpath_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);
77+
if(wpath_len <= 0) {
78+
free(ret);
79+
strlcpy(err, "unable to decode plugin path", PLUGIN_MAX_ERRLEN);
80+
return NULL;
81+
}
82+
83+
WCHAR* wpath = malloc(wpath_len * sizeof(WCHAR));
84+
if(MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, wpath_len) <= 0) {
85+
free(ret);
86+
free(wpath);
87+
strlcpy(err, "unable to decode plugin path", PLUGIN_MAX_ERRLEN);
88+
return NULL;
89+
}
90+
ret->handle = LoadLibraryW(wpath);
91+
free(wpath);
7592
if(ret->handle == NULL) {
7693
DWORD flg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
7794
FORMAT_MESSAGE_IGNORE_INSERTS;

userspace/plugin/plugin_loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ plugin_handle_t* plugin_load_api(const plugin_api* api, char* err);
7272
\brief Loads a dynamic library from the given path and returns a
7373
plugin_handle_t* representing the loaded plugin. In case of error,
7474
returns NULL and fills the err string up to PLUGIN_MAX_ERRLEN chars.
75+
76+
The path must be encoded as UTF-8 on Windows.
7577
*/
7678
plugin_handle_t* plugin_load(const char* path, char* err);
7779

0 commit comments

Comments
 (0)