Skip to content

Commit

Permalink
return error for src too small (#130)
Browse files Browse the repository at this point in the history
Change-Id: I745f447fb22a843257d1ae211a130cd39dad4ccc
  • Loading branch information
garymm authored Mar 10, 2024
1 parent 4c37384 commit 1cd6846
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
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

0 comments on commit 1cd6846

Please sign in to comment.