P92Z opened a new pull request, #578:
URL: https://github.com/apache/datasketches-cpp/pull/578

   ## Problem
   
   `bloom_filter` checks the result of `allocate()` for `nullptr` in four 
places and throws `std::bad_alloc` if it is null. A standard-conforming 
allocator never returns `nullptr` from `allocate(n)` for `n > 0` — it throws 
`std::bad_alloc` itself, so these checks can never fire.
   
   The other sketches in the library don't make such checks, and neither do the 
standard containers that the library uses.
   
   ### Misplaced check
   
   One of the checks was also in the wrong place. In the main constructor, 
`std::fill_n` wrote to the buffer **before** the check:
   
   ```cpp
   bit_array_ = AllocUint8(allocator_).allocate(num_bytes);
   std::fill_n(bit_array_, num_bytes, 0);
   if (bit_array_ == nullptr) {
     throw std::bad_alloc();
   }
   ```
   
   With an allocator that returns `nullptr`, this writes through a null pointer 
before the check runs. UBSan reports *store to null pointer*, and ASan reports 
a SEGV in `std::fill_n`.
   
   ## Changes
   
   `bloom_filter_impl.hpp`: removed the `nullptr` checks after `allocate()` in:
   
   - the constructor from `num_bits`
   - the copy constructor
   - `deserialize()` from a stream
   - `deserialize()` and `wrap()` from memory (`internal_deserialize_or_wrap()`)
   
   ### Behavioral impact
   
   - **Standard-conforming allocators:** behavior is unchanged — a failed 
allocation still throws `std::bad_alloc`, now from `allocate()` itself.
   - **Allocators that return `nullptr` instead of throwing:** these do **not** 
meet the Allocator requirements and aren't supported elsewhere in the library. 
With such an allocator, three of these paths used to throw `std::bad_alloc`; 
they no longer do.
   
   ### Alternative
   
   If you'd rather keep the checks, the misplaced one can instead be moved 
**before** `std::fill_n`, as the copy constructor and both `deserialize()` 
paths already do.
   
   ## Tests
   
   `bloom_filter_allocation_test.cpp` has a new test with an allocator that 
fails in the standard way — by throwing `std::bad_alloc`. It checks that the 
exception reaches the caller from:
   
   - `builder::create_by_size()`
   - `deserialize()` from memory
   - `deserialize()` from a stream
   
   The Bloom filter tests pass under ASan and UBSan.
   
   ## Scope
   
   This PR touches only `filters/`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to