From 1a4c4955c641320483d422bd59075522e2b483a4 Mon Sep 17 00:00:00 2001 From: Luis Michaelis Date: Wed, 5 Jun 2024 19:48:13 +0200 Subject: [PATCH] fix(Stream): truncate existing files when creating a `WriteStream` This fixes unexpected behaviour when passing a path to a file which already exists. Reported and debugged by @zirael137 on the GMC Discord. Thank you! --- src/Stream.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Stream.cc b/src/Stream.cc index b0389830..8443dee9 100644 --- a/src/Stream.cc +++ b/src/Stream.cc @@ -376,7 +376,8 @@ namespace zenkit { public: explicit WriteStream(std::ostream* stream) : _m_stream(stream), _m_own(false) {} explicit WriteStream(std::filesystem::path const& path) - : _m_stream(new std::ofstream(path, std::ios::binary)), _m_own(true) {} + : _m_stream(new std::ofstream(path, std::ios::binary | std::ios::out | std::ios::trunc)), _m_own(true) { + } ~WriteStream() noexcept override { if (_m_own) {