From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5D42C3858D33; Tue, 24 Jan 2023 12:41:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D42C3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674564119; bh=bJm1RxKUZZM6BJTGkahbXxuDjVFZpo8ss1vOn4LfSS8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Nv9VXHYnQYn4r8t1a1ICDW2lrRL+d86ak+BuY5xvB+5J9lSVNcR1E1WtSvDthXoJJ Mpp1ANxYu71Gt4LhBw/qWpTY5Pl1PIn7MEpPU3Y1eYiaDcPVrhxVK2TqWFrDXlvvsy bMRwkwcN+VBKFSMhR35RtoSum2AAH+O//KBapD9I= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/108517] std::sort of empty range yield "warning: 'this' pointer is null" Date: Tue, 24 Jan 2023 12:41:59 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW 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: Message-ID: In-Reply-To: References: 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=3D108517 --- Comment #4 from Jonathan Wakely --- Libstdc++ isn't causing it, but we can give the compiler enough information= to make it shut up: --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -1814,6 +1814,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) { if (__first =3D=3D __last) return; + if (__first =3D=3D _RandomAccessIterator() + || __last =3D=3D _RandomAccessIterator()) + __builtin_unreachable(); + for (_RandomAccessIterator __i =3D __first + 1; __i !=3D __last; ++_= _i) { if (__comp(__i, __first)) As you can see there, we already return early and never reach line 1819 whe= re the dereference happens. We can tell the compiler that it's impossible to g= et to that that dereference with a value-initialized iterator, because if one = is null the other must be, and so we'd already have returned.=