Skip to content

Commit 4bf9592

Browse files
PatTheMavtt2468
authored andcommitted
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.
1 parent d957b27 commit 4bf9592

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/utils/Json.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ json Utils::Json::ObsDataToJson(obs_data_t *d, bool includeDefault)
178178

179179
bool Utils::Json::GetJsonFileContent(std::string fileName, json &content)
180180
{
181-
std::ifstream f(fileName);
181+
std::ifstream f(std::filesystem::u8path(fileName));
182182
if (!f.is_open())
183183
return false;
184184

@@ -195,9 +195,11 @@ bool Utils::Json::GetJsonFileContent(std::string fileName, json &content)
195195

196196
bool Utils::Json::SetJsonFileContent(std::string fileName, const json &content, bool makeDirs)
197197
{
198+
auto jsonFilePath = std::filesystem::u8path(fileName);
199+
198200
if (makeDirs) {
201+
auto p = jsonFilePath.parent_path();
199202
std::error_code ec;
200-
auto p = std::filesystem::path(fileName).parent_path();
201203
if (!ec && !std::filesystem::exists(p, ec))
202204
std::filesystem::create_directories(p, ec);
203205
if (ec) {
@@ -207,7 +209,7 @@ bool Utils::Json::SetJsonFileContent(std::string fileName, const json &content,
207209
}
208210
}
209211

210-
std::ofstream f(fileName);
212+
std::ofstream f(jsonFilePath);
211213
if (!f.is_open()) {
212214
blog(LOG_ERROR, "[Utils::Json::SetJsonFileContent] Failed to open file `%s` for writing", fileName.c_str());
213215
return false;

0 commit comments

Comments
 (0)