1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-27 09:41:16 +00:00

Avoid null pointer dereference

This commit is contained in:
Andrei Kortunov 2023-07-31 15:20:35 +04:00
parent 1d207ed318
commit 1ae2a994f0

View File

@ -48,6 +48,11 @@ namespace ESM4
{ {
namespace namespace
{ {
std::string getError(const std::string& header, const int errorCode, const char* msg)
{
return header + ": code " + std::to_string(errorCode) + ", " + std::string(msg != nullptr ? msg : "(null)");
}
std::u8string_view getStringsSuffix(LocalizedStringType type) std::u8string_view getStringsSuffix(LocalizedStringType type)
{ {
switch (type) switch (type)
@ -78,12 +83,12 @@ namespace ESM4
stream.avail_out = decompressed.size(); stream.avail_out = decompressed.size();
if (const int ec = inflateInit(&stream); ec != Z_OK) if (const int ec = inflateInit(&stream); ec != Z_OK)
return "inflateInit error: " + std::to_string(ec) + " " + std::string(stream.msg); return getError("inflateInit error", ec, stream.msg);
const std::unique_ptr<z_stream, InflateEnd> streamPtr(&stream); const std::unique_ptr<z_stream, InflateEnd> streamPtr(&stream);
if (const int ec = inflate(&stream, Z_NO_FLUSH); ec != Z_STREAM_END) if (const int ec = inflate(&stream, Z_NO_FLUSH); ec != Z_STREAM_END)
return "inflate error: " + std::to_string(ec) + " " + std::string(stream.msg); return getError("inflate error", ec, stream.msg);
return std::nullopt; return std::nullopt;
} }
@ -94,7 +99,7 @@ namespace ESM4
z_stream stream{}; z_stream stream{};
if (const int ec = inflateInit(&stream); ec != Z_OK) if (const int ec = inflateInit(&stream); ec != Z_OK)
return "inflateInit error: " + std::to_string(ec) + " " + std::string(stream.msg); return getError("inflateInit error", ec, stream.msg);
const std::unique_ptr<z_stream, InflateEnd> streamPtr(&stream); const std::unique_ptr<z_stream, InflateEnd> streamPtr(&stream);
@ -110,8 +115,8 @@ namespace ESM4
if (ec == Z_STREAM_END) if (ec == Z_STREAM_END)
break; break;
if (ec != Z_OK) if (ec != Z_OK)
return "inflate error after reading " + std::to_string(stream.total_in) return getError(
+ " bytes: " + std::to_string(ec) + " " + std::string(stream.msg); "inflate error after reading " + std::to_string(stream.total_in) + " bytes", ec, stream.msg);
compressed = compressed.subspan(stream.total_in - prevTotalIn); compressed = compressed.subspan(stream.total_in - prevTotalIn);
decompressed = decompressed.subspan(stream.total_out - prevTotalOut); decompressed = decompressed.subspan(stream.total_out - prevTotalOut);
} }