Skip to content

Commit

Permalink
[gutil] fix ASAN warning in CountOnes()
Browse files Browse the repository at this point in the history
Running various tests on aarch64 (Graviton3) under ASAN produced
warnings like below:

  src/kudu/gutil/bits.h:19:42: runtime error: unsigned integer overflow:
    134678536 * 16843009 cannot be represented in type 'unsigned int'
      #0 0xffffa1ebd8d4 in Bits::CountOnes(unsigned int) src/kudu/gutil/bits.h:19:42
      #1 0xffffa1ebd830 in Bits::CountOnes64(unsigned long) src/kudu/gutil/bits.h:30:12
      #2 0xffffa1ebd7f8 in Bits::CountOnes64withPopcount(unsigned long) src/kudu/gutil/bits.h:43:12

  SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/kudu/gutil/bits.h:19:42

This patch addresses the issue.

Change-Id: I47bff62676ee57706d6b5ef841e3891bba5a62fa
Reviewed-on: http://gerrit.cloudera.org:8080/20558
Reviewed-by: Marton Greber <[email protected]>
Tested-by: Alexey Serbin <[email protected]>
Reviewed-by: Abhishek Chennaka <[email protected]>
  • Loading branch information
alexeyserbin authored and achennaka committed Oct 11, 2023
1 parent 71e2ba2 commit 8aab39e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/kudu/gutil/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Bits {
static int CountOnes(uint32 n) {
n -= ((n >> 1) & 0x55555555);
n = ((n >> 2) & 0x33333333) + (n & 0x33333333);
return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
return static_cast<int>((((n + (n >> 4)) & 0xF0F0F0FULL) * 0x1010101ULL) >> 24);
}

// Count bits using sideways addition [WWG'57]. See Knuth TAOCP v4 7.1.3(59)
Expand Down

0 comments on commit 8aab39e

Please sign in to comment.