From d7e1c410823f209697f74d88c20eccbfd496b67c Mon Sep 17 00:00:00 2001 From: Jonson Petard <41122242+greenhat616@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:08:55 +0800 Subject: [PATCH] Fix split string lifetime and contain updater exceptions Return owning strings from BNString::split so callers do not read destroyed substring temporaries. Reject empty delimiters to avoid an infinite loop. Catch standard and unknown exceptions at the detached remote-update thread boundary, including failures while handling a fallback source. Add regression coverage for long URLs, source lifetime and delimiter cases. Validated with AddressSanitizer, updater exception injection, an x64 Release build, and an isolated CloudMusic startup test with Segment Heap enabled. --- src/PluginManager.cpp | 12 +++++++++++- src/utils/BNString.hpp | 10 +++++++--- tests/bnstring_split.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/bnstring_split.cpp diff --git a/src/PluginManager.cpp b/src/PluginManager.cpp index b73e9756..73988cf3 100644 --- a/src/PluginManager.cpp +++ b/src/PluginManager.cpp @@ -199,7 +199,17 @@ std::optional Plugin::getStartupScript() void PluginManager::performForceInstallAndUpdateAsync(const std::string& source) { std::thread([source]() { - performForceInstallAndUpdateSync(source); + try { + performForceInstallAndUpdateSync(source); + } + catch (const std::exception& e) { + std::cerr << "[ BetterNCM ] [Plugin Remote Tasks] Update failed on " + << source << ": " << e.what() << std::endl; + } + catch (...) { + std::cerr << "[ BetterNCM ] [Plugin Remote Tasks] Unknown update error on " + << source << std::endl; + } }).detach(); } diff --git a/src/utils/BNString.hpp b/src/utils/BNString.hpp index 5173d476..822907b9 100644 --- a/src/utils/BNString.hpp +++ b/src/utils/BNString.hpp @@ -201,12 +201,16 @@ class BNString : public std::wstring { return static_cast(index); } - [[nodiscard]] std::vector split(const std::wstring& delimiter) const { - std::vector result; + [[nodiscard]] std::vector split(const std::wstring& delimiter) const { + if (delimiter.empty()) { + throw std::invalid_argument("split delimiter must not be empty"); + } + + std::vector result; size_t start = 0; size_t end = 0; while ((end = this->find(delimiter, start)) != std::wstring::npos) { - result.push_back(this->substr(start, end - start)); + result.push_back(this->substr(start, end - start)); start = end + delimiter.length(); } result.push_back(this->substr(start)); diff --git a/tests/bnstring_split.cpp b/tests/bnstring_split.cpp new file mode 100644 index 00000000..c03f627b --- /dev/null +++ b/tests/bnstring_split.cpp @@ -0,0 +1,34 @@ +#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING +#include +#include +#include +#include +#include +#include "../src/utils/BNString.hpp" + +static void check(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +int main() { + const std::wstring url = L"https://gitcode.net/qq_21551787/bncm-plugin-packed/-/raw/master/"; + const BNString config(std::wstring(url + L"\n" + url)); + auto lines = config.split(L"\n"); + check(lines == std::vector{url, url}, "long URL contents"); + + // Results must own their data even after a temporary source is destroyed. + auto retained = BNString(std::wstring(url + L"\nshort")).split(L"\n"); + std::wstring reuse(4096, L'x'); + check(retained == std::vector{url, L"short"}, "source lifetime"); + check(BNString(std::wstring(L"a\n\nb\n")).split(L"\n") == + std::vector{L"a", L"", L"b", L""}, "empty fields"); + check(BNString(std::wstring(L"")).split(L"\n") == + std::vector{L""}, "empty source"); + check(BNString(std::wstring(L"a::b")).split(L"::") == + std::vector{L"a", L"b"}, "multi-character delimiter"); + bool rejected = false; + try { (void)config.split(L""); } + catch (const std::invalid_argument&) { rejected = true; } + check(rejected, "empty delimiter must be rejected"); + std::cout << "PASS: BNString split ownership and edge cases\n"; +}