srctree

Robin Linden parent 9de2322e 715d1b6b
archive: Fix crash when fed empty input

terminate called after throwing an instance of 'std::logic_error' what(): basic_string: construction from null is not valid
inlinesplit
archive/zlib.cpp added: 11, removed: 4, total 7
@@ -48,7 +48,10 @@ tl::expected<std::string, ZlibError> zlib_decode(std::string_view data, ZlibMode
s.avail_out = static_cast<uInt>(buf.size());
int ret = inflate(&s, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
std::string msg = s.msg;
std::string msg;
if (s.msg != nullptr) {
msg = s.msg;
}
inflateEnd(&s);
return tl::unexpected{ZlibError{.message = std::move(msg), .code = ret}};
}
 
archive/zlib_test.cpp added: 11, removed: 4, total 7
@@ -28,12 +28,16 @@ constexpr auto kZlibbedCss =
 
int main() {
etest::test("zlib", [] {
expect_eq(zlib_decode(kZlibbedCss, ZlibMode::Zlib), kExpected);
expect(!zlib_decode("", ZlibMode::Zlib).has_value());
expect(!zlib_decode(kGzippedCss, ZlibMode::Zlib).has_value());
 
expect_eq(zlib_decode(kZlibbedCss, ZlibMode::Zlib), kExpected);
});
 
etest::test("gzip", [] {
expect(!zlib_decode("", ZlibMode::Gzip).has_value());
expect(!zlib_decode(kZlibbedCss, ZlibMode::Gzip), std::nullopt);
 
expect_eq(zlib_decode(kGzippedCss, ZlibMode::Gzip), kExpected);
});