From 45b5f75f21cd184e2e7cd7bdb2cca816f67d91f4 Mon Sep 17 00:00:00 2001 From: Jhett Black <10942655+jhett12321@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:07:10 +0100 Subject: [PATCH] Clean NWNX binaries from LD_PRELOAD. --- Core/NWNXCore.cpp | 15 +++++++++++++++ Core/NWNXCore.hpp | 1 + 2 files changed, 16 insertions(+) diff --git a/Core/NWNXCore.cpp b/Core/NWNXCore.cpp index 3aa4d262363..0c4b1d98ca9 100644 --- a/Core/NWNXCore.cpp +++ b/Core/NWNXCore.cpp @@ -127,6 +127,7 @@ NWNXCore::NWNXCore() : m_ScriptChunkRecursion(0) { g_core = this; + CleanupPreload(); // NOTE: We should do the version check here, but the global in the binary hasn't been initialised yet at this point. // This will be fixed in a future release of NWNX:EE. For now, the version check will happen *too late* - we may @@ -145,6 +146,20 @@ NWNXCore::~NWNXCore() Shutdown(); } +void NWNXCore::CleanupPreload() +{ + const auto* preload = std::getenv("LD_PRELOAD"); + const std::regex regex("(([^: ]+)?NWNX_[^: ]+)"); + + std::string newPreload = preload; + while (std::regex_search(newPreload, regex)) + { + newPreload = std::regex_replace(newPreload, regex, ""); + } + + setenv("LD_PRELOAD", newPreload.c_str(), true); +} + std::unique_ptr NWNXCore::ConstructCoreServices() { using namespace NWNXLib::Services; diff --git a/Core/NWNXCore.hpp b/Core/NWNXCore.hpp index e54af0b470f..6c5900d3974 100644 --- a/Core/NWNXCore.hpp +++ b/Core/NWNXCore.hpp @@ -42,6 +42,7 @@ class NWNXCore std::unique_ptr ConstructCoreServices(); std::unique_ptr ConstructProxyServices(const std::string& plugin); + void CleanupPreload(); void ConfigureLogLevel(const std::string& plugin); void InitialSetupHooks();