https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113786

            Bug ID: 113786
           Summary: cppcheck: return value from find_if not properly
                    checked ?
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

Consider the following newish C++ code:

#include <algorithm>
#include <array>
#include <iostream>

int main()
{
    auto is_even = [](int i) { return i % 2 == 0; };

    for (auto const& w : {std::array{3, 1, 4}, {1, 3, 5}})
        if (std::find_if(begin(w), end(w), is_even))
            std::cout << "w contains an even number " << '\n';
        else
            std::cout << "w does not contain even numbers\n"; 
}

Here is static analyser cppcheck finding the problem with the find_if:

bug1003.cc:11:13: warning: Suspicious condition. The result of find() is an
iterator, but it is not properly checked. [stlIfFind]

Recent Gcc and clang have little to say:

Alphasrc $ ~/gcc/results/bin/g++ -g -O2 -Wall -Wextra -pedantic
-D_FORTIFY_SOURCE=3 bug1003.cc
Alphasrc $ ~/llvm/results/bin/clang++ -g -O2 -Wall -Wextra -pedantic
-D_FORTIFY_SOURCE=3 bug1003.cc
Alphasrc $ 

I guess any C++ STL function that returns something non-zero (in this case
end(w) )
on error is liable to this problem.

I found this problem in the source code of flang, the clang Fortran compiler,
so it does occur in practice.

Reply via email to