From eef5dcca9d21a69273d8bcd4d6178b0ff696577a Mon Sep 17 00:00:00 2001 From: PatTheMav Date: Sat, 8 Feb 2025 19:09:44 +0100 Subject: [PATCH] utils: Use u8path to fix possible string conversion crashes on Windows All other path operations in the code base already use u8path to ensure that the strings provided by libobs (which will either be ASCII or UTF-8 byte sequences) are correctly converted into UTF-16 strings. --- src/utils/Json.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/Json.cpp b/src/utils/Json.cpp index 68f05881..923020a4 100644 --- a/src/utils/Json.cpp +++ b/src/utils/Json.cpp @@ -178,7 +178,7 @@ json Utils::Json::ObsDataToJson(obs_data_t *d, bool includeDefault) bool Utils::Json::GetJsonFileContent(std::string fileName, json &content) { - std::ifstream f(fileName); + std::ifstream f(std::filesystem::u8path(fileName)); if (!f.is_open()) return false; @@ -195,9 +195,11 @@ bool Utils::Json::GetJsonFileContent(std::string fileName, json &content) bool Utils::Json::SetJsonFileContent(std::string fileName, const json &content, bool makeDirs) { + auto jsonFilePath = std::filesystem::u8path(fileName); + if (makeDirs) { + auto p = jsonFilePath.parent_path(); std::error_code ec; - auto p = std::filesystem::path(fileName).parent_path(); if (!ec && !std::filesystem::exists(p, ec)) std::filesystem::create_directories(p, ec); if (ec) { @@ -207,7 +209,7 @@ bool Utils::Json::SetJsonFileContent(std::string fileName, const json &content, } } - std::ofstream f(fileName); + std::ofstream f(jsonFilePath); if (!f.is_open()) { blog(LOG_ERROR, "[Utils::Json::SetJsonFileContent] Failed to open file `%s` for writing", fileName.c_str()); return false;