From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 306CE3858D38; Tue, 6 Feb 2024 13:07:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 306CE3858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707224842; bh=WYSezqhhzAbMvOQEBC1GuOBwI27qWm0OGaRiLh8zrB0=; h=From:To:Subject:Date:From; b=wSKm0eUSz8GuQLqz5RPRNIIzWTCslAfhLbFTVkgvLKNnuA7orrAllc710ETkyX/DO IGmdeVgUdydYstsSHtrss38TR3yYmFyrp9Ds+D2+UeZuq7W/e9vmIkYIsjYDMi6o9r DfgYpag09oIz8MCEreG6Se/hZ1PoenHsVNuPa8NI= From: "dcb314 at hotmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dcb314 at hotmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113786 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 #include #include int main() { auto is_even =3D [](int i) { return i % 2 =3D=3D 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";=20 } 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=3D3 bug1003.cc Alphasrc $ ~/llvm/results/bin/clang++ -g -O2 -Wall -Wextra -pedantic -D_FORTIFY_SOURCE=3D3 bug1003.cc Alphasrc $=20 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 compile= r, so it does occur in practice.=