Search code examples
c++c++14clang-static-analyzer

What's a garbage value for clang-check


I have got the following warning:

test.cpp:14:25: warning: The right operand of '/' is a garbage value
    return (std::abs(a) / size) > 10;
                        ^ ~~~~

for this piece of code:

#include <algorithm>
#include <complex>
#include <vector>
#include <iostream>

using namespace std;
double
pitchDetect(const std::vector<std::complex<double>> &dft,
                              unsigned int samplingRate) noexcept {
  if (dft.empty())
    return 0.0;
  auto it = find_if(begin(dft), end(dft),
                    [size = dft.size()](const std::complex<double> &a) {
    return (std::abs(a) / size) > 10;
  });
  return 0.0;
}

I don't understand what the problem is!


Solution

  • This looks like bug 22833, which is fixed in trunk:

    Giving a lambda capture parameter an explicit value (new feature in C++14) causes the analyzer to believe that value is undefined.

    As a workaround, you could try hoisting the init-capture outside the lambda:

      auto const size = dft.size();
      auto it = find_if(begin(dft), end(dft),
                        [size](const std::complex<double> &a) {
        return (std::abs(a) / size) > 10;
      });