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

return error for src too small #130

Merged
merged 1 commit into from
Mar 10, 2024
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
10 changes: 5 additions & 5 deletions src/decompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ auto decompress(std::span<const std::byte> src, std::span<std::byte> dst)
if (len != static_cast<std::uint16_t>(~nlen)) {
return DecompressStatus::NoCompressionLenMismatch;
}
// TODO: should we return an error instead of assert?
assert(
std::cmp_greater_equal(
src_bits.size(), std::size_t{len} * CHAR_BIT) and
"not enough bits in src");
// Surprisingly size() does not return size_t on libstdc++ 13, hence cast.
if (static_cast<size_t>(src_bits.size()) <
std::size_t{len} * std::size_t{CHAR_BIT}) {
return DecompressStatus::SrcTooSmall;
}

if (dst.size() < len) {
return DecompressStatus::DstTooSmall;
Expand Down
1 change: 1 addition & 0 deletions src/decompress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum class DecompressStatus : std::uint8_t
InvalidBlockHeader,
NoCompressionLenMismatch,
DstTooSmall,
SrcTooSmall,
};

namespace detail {
Expand Down
7 changes: 5 additions & 2 deletions src/test/decompress_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ auto main(int, char* argv[]) -> int
std::array<std::byte, expected.size()> dst_array{};
const std::span<std::byte> dst_too_small{
dst_array.data(), dst_array.size() - 1};
const auto status_too_small = decompress(src, dst_too_small);
expect(status_too_small == DecompressStatus::DstTooSmall);
const auto status_dst_too_small = decompress(src, dst_too_small);
expect(status_dst_too_small == DecompressStatus::DstTooSmall);

const std::span<std::byte> dst{dst_array};
const auto status_src_too_small = decompress(src.subspan(0, 5), dst);
expect(status_src_too_small == DecompressStatus::SrcTooSmall);

const auto status = decompress(src, dst);
expect(status == DecompressStatus::Success);
expect(std::ranges::equal(dst, expected));
Expand Down
Loading