diff --git a/filters/include/bloom_filter_impl.hpp b/filters/include/bloom_filter_impl.hpp index 9db86c32f..65a954bf3 100644 --- a/filters/include/bloom_filter_impl.hpp +++ b/filters/include/bloom_filter_impl.hpp @@ -302,6 +302,7 @@ bloom_filter_alloc bloom_filter_alloc::deserialize(std::istream& is, const const uint64_t seed = read(is); const uint32_t num_longs = read(is); // sized in java longs read(is); // unused + if (!is.good()) throw std::runtime_error("error reading from std::istream"); // if empty, stop reading if (is_empty) { @@ -317,10 +318,11 @@ bloom_filter_alloc bloom_filter_alloc::deserialize(std::istream& is, const if (bit_array == nullptr) { throw std::bad_alloc(); } - read(is, bit_array, num_bytes); uint64_t num_bits_set; try { + read(is, bit_array, num_bytes); + if (!is.good()) throw std::runtime_error("error reading from std::istream"); num_bits_set = validate_bloom_filter_num_bits_set( raw_num_bits_set, bit_array, num_bytes, DIRTY_BITS_VALUE); } catch (...) { diff --git a/filters/test/bloom_filter_allocation_test.cpp b/filters/test/bloom_filter_allocation_test.cpp index 7f1536632..e8236199e 100644 --- a/filters/test/bloom_filter_allocation_test.cpp +++ b/filters/test/bloom_filter_allocation_test.cpp @@ -72,4 +72,28 @@ TEST_CASE("bloom filter allocation test", "[bloom_filter][test_type]") { REQUIRE(test_allocator_net_allocations == 0); } +TEST_CASE("bloom filter: truncated stream frees bit array", "[bloom_filter][test_type]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + auto bf = bloom_filter_test_alloc::builder::create_by_accuracy(1000, 0.01, 12345, alloc(0)); + for (int i = 0; i < 100; ++i) bf.update(i); + + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + bf.serialize(ss); + std::string serialized = ss.str(); + serialized.resize(serialized.size() - 8); + + // bf is dirty, so num_bits_set validation is skipped and only the stream check catches truncation + std::istringstream is(serialized, std::ios::in | std::ios::binary); + REQUIRE_THROWS_AS(bloom_filter_test_alloc::deserialize(is, alloc(0)), std::runtime_error); + + std::istringstream is_throwing(serialized, std::ios::in | std::ios::binary); + is_throwing.exceptions(std::ios::failbit | std::ios::badbit); + REQUIRE_THROWS_AS(bloom_filter_test_alloc::deserialize(is_throwing, alloc(0)), std::ios_base::failure); + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + } diff --git a/filters/test/bloom_filter_test.cpp b/filters/test/bloom_filter_test.cpp index ff37378f9..4ce4203b6 100644 --- a/filters/test/bloom_filter_test.cpp +++ b/filters/test/bloom_filter_test.cpp @@ -430,4 +430,17 @@ TEST_CASE("bloom_filter: inconsistent num bits set is rejected", "[bloom_filter] } } +TEST_CASE("bloom_filter: truncated empty stream is rejected", "[bloom_filter]") { + auto bf = bloom_filter::builder::create_by_size(32769, 7); + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + bf.serialize(ss); + const std::string serialized = ss.str(); + REQUIRE(serialized.size() == bf.get_serialized_size_bytes()); + + for (size_t len = 4; len < serialized.size(); ++len) { + std::istringstream is(serialized.substr(0, len), std::ios::in | std::ios::binary); + REQUIRE_THROWS_AS(bloom_filter::deserialize(is), std::runtime_error); + } +} + } // namespace datasketches