Skip to content

Commit

Permalink
return error for src too small
Browse files Browse the repository at this point in the history
Change-Id: I745f447fb22a843257d1ae211a130cd39dad4ccc
  • Loading branch information
garymm committed Mar 10, 2024
1 parent dada981 commit 8783255
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/decompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ auto decompress(std::span<const std::byte> src, std::span<std::byte> dst)
if (len != static_cast<std::uint16_t>(~nlen)) {
return DecompressStatus::NoCompressionLenMismatch;
}

Check warning on line 56 in src/decompress.cpp

View check run for this annotation

Codecov / codecov/patch

src/decompress.cpp#L55-L56

Added lines #L55 - L56 were not covered by tests
// 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");
if (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 @@ -115,10 +115,13 @@ auto main(int, char* argv[]) -> int

std::array<std::byte, expected.size()> dst_array{};
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);

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 8783255

Please sign in to comment.