From 33fc1b4f4d07345e57e772b5c1dc415a5c8449ae Mon Sep 17 00:00:00 2001 From: Gary Miguel Date: Mon, 12 Feb 2024 11:54:14 +0000 Subject: [PATCH] return error for src too small Change-Id: I745f447fb22a843257d1ae211a130cd39dad4ccc --- src/decompress.cpp | 8 +++----- src/decompress.hpp | 1 + src/test/decompress_test.cpp | 7 +++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/decompress.cpp b/src/decompress.cpp index ba996bf..b99a22a 100644 --- a/src/decompress.cpp +++ b/src/decompress.cpp @@ -54,11 +54,9 @@ auto decompress(std::span src, std::span dst) if (len != static_cast(~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"); + if (src_bits.size() < std::size_t{len} * std::size_t{CHAR_BIT}) { + return DecompressStatus::SrcTooSmall; + } if (dst.size() < len) { return DecompressStatus::DstTooSmall; diff --git a/src/decompress.hpp b/src/decompress.hpp index e13b57a..07f88f4 100644 --- a/src/decompress.hpp +++ b/src/decompress.hpp @@ -17,6 +17,7 @@ enum class DecompressStatus : std::uint8_t InvalidBlockHeader, NoCompressionLenMismatch, DstTooSmall, + SrcTooSmall, }; namespace detail { diff --git a/src/test/decompress_test.cpp b/src/test/decompress_test.cpp index 7a9c1f6..c80083f 100644 --- a/src/test/decompress_test.cpp +++ b/src/test/decompress_test.cpp @@ -116,10 +116,13 @@ auto main(int, char* argv[]) -> int std::array dst_array{}; const std::span 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 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));