This is an automated email from the ASF dual-hosted git repository.

jmalkin pushed a commit to branch minor_cleanup
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git

commit 1deefe98dff7fb991d87ddbf4f67dfa96070f605
Author: Jon Malkin <[email protected]>
AuthorDate: Fri Apr 21 11:19:11 2023 -0700

    add better memory checks, fix a few typos
---
 count/include/count_min_impl.hpp        | 6 +++++-
 density/include/density_sketch_impl.hpp | 5 ++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/count/include/count_min_impl.hpp b/count/include/count_min_impl.hpp
index b7ff1cb..ec77be6 100644
--- a/count/include/count_min_impl.hpp
+++ b/count/include/count_min_impl.hpp
@@ -359,7 +359,7 @@ auto count_min_sketch<W,A>::deserialize(std::istream& is, 
uint64_t seed, const A
 
 template<typename W, typename A>
 size_t count_min_sketch<W,A>::get_serialized_size_bytes() const {
-  // The header is always 2 bytes, whether empty or full
+  // The header is always 2 longs, whether empty or full
   size_t preamble_longs = PREAMBLE_LONGS_SHORT;
 
   // If the sketch is empty, we're done. Otherwise, we need the total weight
@@ -411,6 +411,8 @@ auto count_min_sketch<W,A>::serialize(unsigned 
header_size_bytes) const -> vecto
 
 template<typename W, typename A>
 auto count_min_sketch<W,A>::deserialize(const void* bytes, size_t size, 
uint64_t seed, const A& allocator) -> count_min_sketch {
+  ensure_minimum_memory(size, PREAMBLE_LONGS_SHORT * sizeof(uint64_t));
+
   const char* ptr = static_cast<const char*>(bytes);
 
   // First 8 bytes are 4 bytes of preamble and 4 unused bytes.
@@ -443,6 +445,8 @@ auto count_min_sketch<W,A>::deserialize(const void* bytes, 
size_t size, uint64_t
   const bool is_empty = (flags_byte & (1 << flags::IS_EMPTY)) > 0;
   if (is_empty) return c ; // sketch is empty, no need to read further.
 
+  ensure_minimum_memory(size, sizeof(W) * (1 + nbuckets * nhashes));
+
   // Long 2 is the weight.
   W weight;
   ptr += copy_from_mem(ptr, weight) ;
diff --git a/density/include/density_sketch_impl.hpp 
b/density/include/density_sketch_impl.hpp
index 4b4e3b7..763f4e7 100755
--- a/density/include/density_sketch_impl.hpp
+++ b/density/include/density_sketch_impl.hpp
@@ -354,10 +354,11 @@ density_sketch<T, K, A> density_sketch<T, K, 
A>::deserialize(const void* bytes,
 
   // levels arrays
   Levels levels(allocator);
-  int64_t num_to_read = num_retained; // num_retrained is uint32_t so this 
allows error checking
+  int64_t num_to_read = num_retained; // num_retained is uint32_t so this 
allows error checking
   while (num_to_read > 0) {
     uint32_t level_size;
     ptr += copy_from_mem(ptr, level_size);
+    ensure_minimum_memory(end_ptr - ptr, level_size * pt_size);
     Level lvl(allocator);
     lvl.reserve(level_size);
     for (uint32_t i = 0; i < level_size; ++i) {
@@ -442,7 +443,6 @@ string<A> density_sketch<T, K, A>::to_string(bool 
print_levels, bool print_items
 
   if (print_items) {
     os << "### Density sketch data:" << std::endl;
-    unsigned level = 0;
     for (unsigned height = 0; height < levels_.size(); ++height) {
       os << " level " << height << ": " << std::endl;
       for (const auto& point: levels_[height]) {
@@ -458,7 +458,6 @@ string<A> density_sketch<T, K, A>::to_string(bool 
print_levels, bool print_items
         }
         os << "]" << std::endl;
       }
-      ++level;
     }
     os << "### End sketch data" << std::endl;
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to