From b584cd7423686c3ee62d4b46d14e7a40471fbee7 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 11 Jan 2025 13:05:45 +0100 Subject: [PATCH] fix: make sure creating directories in OutputPathFilesystem cannot lead to a crash --- src/ObjCommon/SearchPath/OutputPathFilesystem.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp b/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp index 39210c76..41fb008c 100644 --- a/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp +++ b/src/ObjCommon/SearchPath/OutputPathFilesystem.cpp @@ -1,6 +1,8 @@ #include "OutputPathFilesystem.h" +#include #include +#include namespace fs = std::filesystem; @@ -17,11 +19,21 @@ std::unique_ptr OutputPathFilesystem::Open(const std::string& file return nullptr; const auto containingDirectory = fullNewPath.parent_path(); - fs::create_directories(containingDirectory); + + std::error_code ec; + fs::create_directories(containingDirectory, ec); + if (ec) + { + std::cerr << std::format("Failed to create folder '{}' when try to open file '{}'\n", containingDirectory, fileName); + return nullptr; + } std::ofstream stream(fullNewPath, std::ios::binary | std::ios::out); if (!stream.is_open()) + { + std::cerr << std::format("Failed to open file '{}'\n", fileName); return nullptr; + } return std::make_unique(std::move(stream)); }