summaryrefslogtreecommitdiffhomepage
path: root/serialize/world-writer.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-12 01:47:41 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-12 01:47:41 +0100
commit3f6ba6056c5c63249022e52b3c6000e337856d1a (patch)
treecba43d1379aa917ac446d0cb6525bc860afcc4d0 /serialize/world-writer.cpp
parentff7828727b8967e10f020836a4876da294cb379a (diff)
call strerror before errno gets clobbered
Diffstat (limited to 'serialize/world-writer.cpp')
-rw-r--r--serialize/world-writer.cpp18
1 files changed, 13 insertions, 5 deletions
diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp
index 5e6c07fe..747610c1 100644
--- a/serialize/world-writer.cpp
+++ b/serialize/world-writer.cpp
@@ -252,27 +252,35 @@ void world::serialize(StringView filename)
{
collect(true);
char errbuf[128];
- constexpr auto strerror = []<std::size_t N> (char (&buf)[N]) -> const char* {
+ constexpr auto get_error_string = []<std::size_t N> (char (&buf)[N]) {
buf[0] = '\0';
#ifndef _WIN32
(void)::strerror_r(errno, buf, std::size(buf));
#else
(void)::strerror_s(buf, std::size(buf), errno);
#endif
- return buf;
};
fm_assert(filename.flags() & StringViewFlag::NullTerminated);
if (Path::exists(filename))
Path::remove(filename);
FILE_raii file = ::fopen(filename.data(), "wb");
if (!file)
- fm_abort("fopen(\"%s\", \"w\"): %s", filename.data(), strerror(errbuf));
+ {
+ get_error_string(errbuf);
+ fm_abort("fopen(\"%s\", \"w\"): %s", filename.data(), errbuf);
+ }
Serialize::writer_state s{*this};
const auto array = s.serialize_world();
if (auto len = ::fwrite(array.data(), array.size(), 1, file); len != 1)
- fm_abort("fwrite: %s", strerror(errbuf));
+ {
+ get_error_string(errbuf);
+ fm_abort("fwrite: %s", errbuf);
+ }
if (int ret = ::fflush(file); ret != 0)
- fm_abort("fflush: %s", strerror(errbuf));
+ {
+ get_error_string(errbuf);
+ fm_abort("fflush: %s", errbuf);
+ }
}
} // namespace floormat