P92Z commented on PR #578:
URL: https://github.com/apache/datasketches-cpp/pull/578#issuecomment-6982392853

   > Not num_longs == 0. We should check num_bytes directly. Because overflow 
can happens in shifts
   
   Thanks @proost, you're right. I reproduced it: a non-empty image with 
`num_longs = 2^29` passes the `num_longs == 0` check, but `num_longs << 3` is 
computed in 32 bits and gives 0, so `deserialize()` called `allocate(0)` and 
created a filter with a capacity of 0.
   
   The same overflow in `num_longs << 6` also affects valid filters of 2^32 
bits or more: they were read back with their capacity modulo 2^32. For example, 
a wrapped image of a 2^32-bit filter had a capacity of 0.
   
   The new commit widens `num_longs` to 64 bits before both shifts in both 
deserialize paths, and checks `num_bytes == 0` instead of `num_longs == 0`, 
right before `allocate()`. A new test uses an image with 2^29 longs: from 
memory it is now rejected as too short, and from a stream the allocator is 
asked for 2^32 bytes rather than 0 (the test allocator records the size and 
refuses, so nothing is allocated). On the previous commit, the test fails in 
both sections.
   
   One more thing, not in this PR: neither deserialize path checks `num_longs` 
against the maximum filter size, so the stream path asks the allocator for 
whatever size the header says. I'm happy to look at that separately if you 
think it's worth it.


-- 
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