public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113786] New: cppcheck: return value from find_if not properly checked ?
@ 2024-02-06 13:07 dcb314 at hotmail dot com
  2024-02-06 16:20 ` [Bug c++/113786] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: dcb314 at hotmail dot com @ 2024-02-06 13:07 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/113786] cppcheck: return value from find_if not properly checked ?
  2024-02-06 13:07 [Bug c++/113786] New: cppcheck: return value from find_if not properly checked ? dcb314 at hotmail dot com
@ 2024-02-06 16:20 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-06 16:20 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |87403
           Severity|normal                      |enhancement
           Keywords|                            |diagnostic

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I guess we could put [[gnu::returns_nonnull]] on those functions, but it
wouldn't help due to PR 86879.

This feels like something that belongs to static analysers, not a compiler.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87403
[Bug 87403] [Meta-bug] Issues that suggest a new warning

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-06 16:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-06 13:07 [Bug c++/113786] New: cppcheck: return value from find_if not properly checked ? dcb314 at hotmail dot com
2024-02-06 16:20 ` [Bug c++/113786] " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).