Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive: Fix crash when fed empty input #753

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions archive/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("//bzl:copts.bzl", "HASTUR_COPTS")
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
load("//bzl:copts.bzl", "HASTUR_COPTS", "HASTUR_FUZZ_PLATFORMS")

cc_library(
name = "archive",
Expand Down Expand Up @@ -27,4 +28,16 @@ cc_library(
":archive",
"//etest",
],
) for src in glob(["*_test.cpp"])]
) for src in glob(
include = ["*_test.cpp"],
exclude = ["*_fuzz_test.cpp"],
)]

[cc_fuzz_test(
name = src[:-4],
size = "small",
srcs = [src],
copts = HASTUR_COPTS,
target_compatible_with = HASTUR_FUZZ_PLATFORMS,
deps = [":archive"],
) for src in glob(["*_fuzz_test.cpp"])]
18 changes: 18 additions & 0 deletions archive/gzip_fuzz_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

#include "archive/zlib.h"

#include <stddef.h> // NOLINT
#include <stdint.h> // NOLINT
#include <string_view>
#include <tuple>

extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size); // NOLINT

extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) {
std::string_view input{reinterpret_cast<char const *>(data), size};
std::ignore = archive::zlib_decode(input, archive::ZlibMode::Gzip);
return 0;
}
11 changes: 6 additions & 5 deletions archive/zlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ tl::expected<std::string, ZlibError> zlib_decode(std::string_view data, ZlibMode
// only the gzip format <...>.
int const zlib_mode = [mode] {
switch (mode) {
case ZlibMode::Zlib:
return 0;
case ZlibMode::Gzip:
return 15;
case ZlibMode::GzipAndZlib:
default:
return 32;
case ZlibMode::Zlib:
return 0;
}
}();
constexpr int kWindowBits = 15;
Expand All @@ -50,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}};
}
Expand Down
1 change: 0 additions & 1 deletion archive/zlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct ZlibError {
enum class ZlibMode {
Zlib,
Gzip,
GzipAndZlib,
};

tl::expected<std::string, ZlibError> zlib_decode(std::string_view, ZlibMode);
Expand Down
18 changes: 18 additions & 0 deletions archive/zlib_fuzz_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

#include "archive/zlib.h"

#include <stddef.h> // NOLINT
#include <stdint.h> // NOLINT
#include <string_view>
#include <tuple>

extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size); // NOLINT

extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) {
std::string_view input{reinterpret_cast<char const *>(data), size};
std::ignore = archive::zlib_decode(input, archive::ZlibMode::Zlib);
return 0;
}
13 changes: 6 additions & 7 deletions archive/zlib_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ 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(kZlibbedCss, ZlibMode::Gzip), std::nullopt);
expect_eq(zlib_decode(kGzippedCss, ZlibMode::Gzip), kExpected);
});
expect(!zlib_decode("", ZlibMode::Gzip).has_value());
expect(!zlib_decode(kZlibbedCss, ZlibMode::Gzip).has_value());

etest::test("zlib and gzip", [] {
expect_eq(zlib_decode(kZlibbedCss, ZlibMode::GzipAndZlib), kExpected);
expect_eq(zlib_decode(kGzippedCss, ZlibMode::GzipAndZlib), kExpected);
expect_eq(zlib_decode(kGzippedCss, ZlibMode::Gzip), kExpected);
});

return etest::run_all_tests();
Expand Down