public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/113786] New: cppcheck: return value from find_if not properly checked ?
Date: Tue, 06 Feb 2024 13:07:21 +0000	[thread overview]
Message-ID: <bug-113786-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2024-02-06 13:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-06 13:07 dcb314 at hotmail dot com [this message]
2024-02-06 16:20 ` [Bug c++/113786] " redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-113786-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).