zeroshade commented on PR #14026:
URL: https://github.com/apache/arrow/pull/14026#issuecomment-1242261130

   Ah, i see the issue. Stay with me here....
   
   The CI runs our tests using the `-race` option which automatically enables 
the `checkptr` option to validate pointer usage. In this case the slice being 
passed to `bytesToUint64` is less than 8 bytes but we're using `unsafe.Slice` 
to assert it as a `*uint64` and treat it as such. This ends up invoking 
`checkptr` when the option is set (since it is) and it looks at the whole 8 
bytes. If it turns out that the place it ended up in memory is straddling 
multiple memory allocations (since we're less than 8 bytes) we end up failing 
the checkptr and it crashes. 
   
   My preferred solution would this:
   
   ```go
   func CountSetBits(buf []byte, offset, n int) int {
       if offset > 0 {
           return countSetBitsWithOffset(buf, offset, n)
       }
   
      count := 0
      uint64Bytes := n / uint64SizeBits * 8
      if uint64Bytes > 0 {  // <-- add this check to wrap the bytesToUint64 
call so we don't call it with less than 8 bytes
          for _, v := range bytesToUint64(buf[:uint64Bytes]) {
                count += bits.OnesCount64(v)
          }
      }
   
     ...
   ```
   
   Adding that `if` check to the function makes `checkptr` happy as we stop 
trying to look at one byte and treat it like a uint64 pointer.


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

Reply via email to