diff --git a/source/loaders/cs_loader/CMakeLists.txt b/source/loaders/cs_loader/CMakeLists.txt index 1273d6817..860734901 100644 --- a/source/loaders/cs_loader/CMakeLists.txt +++ b/source/loaders/cs_loader/CMakeLists.txt @@ -251,7 +251,7 @@ loader_configuartion_end_development() # Install loader_configuration_begin(cs_loader "${CS_LOADER_CONFIG_TEMPLATE}") -set(DOTNET_CORE_LOADER_ASSEMBLY_PATH ${PROJECT_OUTPUT_DIR}/CSLoader.dll) +set(DOTNET_CORE_LOADER_ASSEMBLY_PATH ${CMAKE_INSTALL_PREFIX}/${INSTALL_LIB}/CSLoader.dll) loader_configuartion_end_install() # diff --git a/source/loaders/cs_loader/include/cs_loader/netcore_linux.h b/source/loaders/cs_loader/include/cs_loader/netcore_linux.h index 9239a804c..3c0e0d14e 100644 --- a/source/loaders/cs_loader/include/cs_loader/netcore_linux.h +++ b/source/loaders/cs_loader/include/cs_loader/netcore_linux.h @@ -16,12 +16,13 @@ * See the License for the specific language governing permissions and * limitations under the License. * -*/ + */ #ifndef _NETCORELINUX_H_ #define _NETCORELINUX_H_ #include +#include #include @@ -108,17 +109,31 @@ class netcore_linux : public netcore bool LoadMain(); - void AddFilesFromDirectoryToTpaList(std::string directory, std::string &tpaList) + bool AddFilesFromDirectoryToTpaList(std::string directory, std::string &tpaList) { - for (auto &dirent : fs::directory_iterator(directory)) + // Use the error_code overload so it can't throw an uncaught filesystem_error + std::error_code ec; + fs::directory_iterator it(directory, ec); + if (ec) + { + // Bad config directory, skip gracefully instead of std::terminate + log_write("metacall", LOG_LEVEL_ERROR, "cs_loader: skipping unreadable TPA directory '%s' (%s)", + directory.c_str(), ec.message().c_str()); + return false; + } + + for (const auto &dirent : it) { std::string path = dirent.path(); - if (!path.compare(path.length() - 4, 4, ".dll")) + // Length guard avoids size_t underflow for names shorter than 4 chars + if (path.length() >= 4 && path.compare(path.length() - 4, 4, ".dll") == 0) { tpaList.append(path + ":"); } } + + return true; } public: diff --git a/source/loaders/cs_loader/source/netcore_linux.cpp b/source/loaders/cs_loader/source/netcore_linux.cpp index 486cba15f..779b3ca01 100644 --- a/source/loaders/cs_loader/source/netcore_linux.cpp +++ b/source/loaders/cs_loader/source/netcore_linux.cpp @@ -84,7 +84,10 @@ bool netcore_linux::ConfigAssemblyName() if (this->dotnet_loader_assembly_path[0] == '/') { this->managedAssemblyFullName.append(this->dotnet_loader_assembly_path); - AddFilesFromDirectoryToTpaList(dotnet_loader_assembly_directory, tpaList); + if (AddFilesFromDirectoryToTpaList(dotnet_loader_assembly_directory, tpaList) == false) + { + return false; + } } else { @@ -108,7 +111,10 @@ bool netcore_linux::ConfigAssemblyName() this->nativeDllSearchDirs.append(":"); this->nativeDllSearchDirs.append(this->runtimePath); - AddFilesFromDirectoryToTpaList(this->runtimePath, tpaList); + if (AddFilesFromDirectoryToTpaList(this->runtimePath, tpaList) == false) + { + return false; + } log_write("metacall", LOG_LEVEL_DEBUG, "NetCore application absolute path: %s", this->appPath); diff --git a/source/tests/CMakeLists.txt b/source/tests/CMakeLists.txt index 17a431547..c686cd508 100644 --- a/source/tests/CMakeLists.txt +++ b/source/tests/CMakeLists.txt @@ -221,7 +221,7 @@ add_subdirectory(metacall_csharp_static_class_test) # TODO: # add_subdirectory(metacall_csharp_function_test) # add_subdirectory(metacall_csharp_empty_configuration_test) -# add_subdirectory(metacall_csharp_invalid_configuration_test) +add_subdirectory(metacall_csharp_invalid_configuration_test) add_subdirectory(metacall_julia_test) add_subdirectory(metacall_java_test) add_subdirectory(metacall_wasm_test) diff --git a/source/tests/metacall_csharp_invalid_configuration_test/source/metacall_csharp_invalid_configuration_test.cpp b/source/tests/metacall_csharp_invalid_configuration_test/source/metacall_csharp_invalid_configuration_test.cpp index d639edfa0..f0958c768 100644 --- a/source/tests/metacall_csharp_invalid_configuration_test/source/metacall_csharp_invalid_configuration_test.cpp +++ b/source/tests/metacall_csharp_invalid_configuration_test/source/metacall_csharp_invalid_configuration_test.cpp @@ -45,7 +45,7 @@ TEST_F(metacall_csharp_invalid_configuration_test, DefaultConstructor) "\t}\n" "}\n"; - ASSERT_EQ((int)0, (int)metacall_load_from_memory("cs", sum, sizeof(sum), NULL)); + ASSERT_NE((int)0, (int)metacall_load_from_memory("cs", sum, sizeof(sum), NULL)); metacall_destroy(); }