Skip to content

Commit

Permalink
fix violation under gcc of warning maybe-uninitialized in fingerprint…
Browse files Browse the repository at this point in the history
… bench

Summary:
Fix the following violations of warning `maybe-uninitialized`:
```
[521/919] Building CXX object CMakeFiles/fingerprint_benchmark.dir/folly/test/FingerprintBenchmark.cpp.o
In file included from folly/test/FingerprintBenchmark.cpp:20:
folly/Fingerprint.h: In static member function ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes&& ...) [with _Res = folly::detail::TimeIterData; _Functor = folly::detail::BenchmarkingState<Clock>::addBenchmark(const char*, folly::StringPiece, Lambda&&) [with Lambda = main(int, char**)::<lambda(int)>&; Clock = std::chrono::_V2::system_clock]::<lambda(unsigned int)>; _ArgTypes = {unsigned int}]’:
folly/Fingerprint.h:165:7: warning: array subscript 1 is outside array bounds of ‘uint64_t [1]’ {aka ‘long unsigned int [1]’} [-Warray-bounds]
  165 |       out[i] = fp_[i];
      |       ^~~
folly/test/FingerprintBenchmark.cpp:66:14: note: while referencing ‘out’
   66 |     uint64_t out;
      |              ^~~
In file included from folly/test/FingerprintBenchmark.cpp:20:
folly/Fingerprint.h: In static member function ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes&& ...) [with _Res = folly::detail::TimeIterData; _Functor = folly::detail::BenchmarkingState<Clock>::addBenchmark(const char*, folly::StringPiece, Lambda&&) [with Lambda = main(int, char**)::<lambda(int)>&; Clock = std::chrono::_V2::system_clock]::<lambda(unsigned int)>; _ArgTypes = {unsigned int}]’:
folly/Fingerprint.h:165:7: warning: array subscript 1 is outside array bounds of ‘uint64_t [1]’ {aka ‘long unsigned int [1]’} [-Warray-bounds]
  165 |       out[i] = fp_[i];
      |       ^~~
folly/test/FingerprintBenchmark.cpp:81:14: note: while referencing ‘out’
   81 |     uint64_t out;
      |              ^~~
```

These are true positives, and when the benchmark is run with address sanitizer the bug is trapped as `stack-buffer-overflow`.

Reviewed By: Orvid

Differential Revision: D48497640

fbshipit-source-id: f2cb5d22c3edbdd174f3dcdf74b3670d0292365b
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Aug 22, 2023
1 parent ccbf0d8 commit 9746a85
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions folly/test/FingerprintBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void initialize() {
}
}

template <template <int> class T, int Bits>
constexpr size_t fpBits(tag_t<T<Bits>>) {
return Bits;
}

template <class FP>
void fingerprintIds(int num_iterations, int num_ids) {
for (int iter = 0; iter < num_iterations; iter++) {
Expand All @@ -63,9 +68,9 @@ void fingerprintIds(int num_iterations, int num_ids) {
}
// GOTCHA: if we don't actually call write(), compiler optimizes
// away the inner loop!
uint64_t out;
fp.write(&out);
VLOG(1) << out;
uint64_t out[fpBits(tag<FP>) + 63 / 64]; // ceil(bits / 64.0)
fp.write(out);
compiler_must_not_elide(out);
}
}

Expand All @@ -78,9 +83,9 @@ void fingerprintTerms(int num_iterations, int num_terms) {
}
// GOTCHA: if we don't actually call write(), compiler optimizes
// away the inner loop!
uint64_t out;
fp.write(&out);
VLOG(1) << out;
uint64_t out[fpBits(tag<FP>) + 63 / 64]; // ceil(bits / 64.0)
fp.write(out);
compiler_must_not_elide(out);
}
}

Expand Down

0 comments on commit 9746a85

Please sign in to comment.