From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id C48E73858408; Sun, 17 Mar 2024 11:45:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C48E73858408 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C48E73858408 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::229 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710675963; cv=none; b=T3iFvUEG1AxndrXHl7JZJHUvBS2VvtQ8jAO4OPB+RneSsMdpnQVF5JvSTiPM+bWMB6ZMCTo+s2eFjRVBmvq66N2mQH4xL/lv3Gv2gx4hy/OPTsHKgHqxM8QGgviI5nYLkWEilugGByeRx38lKfe060hO0AtOwHt/IzwSG2u585o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710675963; c=relaxed/simple; bh=55tITkSMRE7me6VPl0O2lfF2UeiSMDjvsTIhVqoA4MI=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=G20zmeGdkQdPoQB1a2z5DWm6VfiUcCcFB8HENMqcCZYzrBCaVEvNv2hm5tKuvoRmx7S7ZbhVVjqx8KA9RTQ24M04Il6WRs2Rcs+o4/OyvbZ/E54WnrckzMLo0sX4Q4KjMDXzmL/+YmD8SoEz+4eGN65IoB9jRXfbD2DEuXecHyk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2d41d1bedc9so58231891fa.3; Sun, 17 Mar 2024 04:45:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710675957; x=1711280757; darn=gcc.gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=dydn7iBA36++syU30BcZ1FJhsWjh5cD/MJVUimtVW+k=; b=L40yB+7JTCA3m2I1O73/Nfh/PwNBszS865m73ovOec+nPXNFh3EVI+rSIBYQ8a+I+N yzX0XbURQbh3up94hgFVYsT4JCXZdetAhceylTxSa6KysRY7BDPzB+AEsoXcFKRtTsco mPJQiZa8iCYWmuvJf7yNYD1CPe4HBCH6+8H/TM/CuI36SeGxSmTkT8Xw9OM0ZfJRdfDT EpuI5YSXHryL+/RHq24TXHtrzGY6lgjYV8NHwe0D/u/l2eZgjAQuzT1B8CjMeMc8EIug CB6y5lzMFXN8p3rEldRP0m2bKeVf7fJvIohsLGSpUoJLlmfF/MuPCk24N4+9lD4pYU0O veZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710675957; x=1711280757; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=dydn7iBA36++syU30BcZ1FJhsWjh5cD/MJVUimtVW+k=; b=YoCOUP2elSVST/GfqZaWBzvF8gTmymsPpIuzbgjSamqzopf4UAF417/aeWfgy226Z+ QdPgT9YlC12k4XGNVdgmRVm5/YnS+wGmRyl0HsE1Om+evPAvTEzor8PASD+1iJDaswKv pwFsHU7Wd9nRWk/EvodIdgg7XUCVrjPSqBkOhLZwQT4rxkimxN9vqyGdXHYIW1RfijaJ gwgtyUqhMntGhA6H4lrAdszq+hWYgwf09L+1W6N/UKLeiG7OydZGqu1DtxQHd1pUKKNC WHP/l5TZBDNXc1SneAazfGH/VN8pA3d2qK6EBkItelxvkM+e8BKlAS//Z8PkrEANTP9D X/ZA== X-Forwarded-Encrypted: i=1; AJvYcCXVmFU7IBvQPA4fdGJcN3p4q9Sx/gmc2LT1hWmG/Lpq8QalbmUCZ99LBt5vPFcN3Yp1JIb2ixhunM6UCsGCu7fIDkPYMMyt4FAk3jP9N9n3SH2MJgfSi+styNZtuzvg X-Gm-Message-State: AOJu0Yw1RXfkJw9NkgoE2HPhPFXPvxwJYvXqEVGqsY7ttgSht5BFrjrN 3HuLPcpZmHX+4VtpnYHWL9Ir/BAqyRJjG8nd7YGRGrvqebUKOAsjaVEFEHR+o4DRtvIURtxQCN+ uJKjes9UYtK1EeAWkSs1FCR29crs= X-Google-Smtp-Source: AGHT+IFjTOrLZ8+AB4/Bw8VV+bPf9IxP9WJfoBjJNNQos9oM3t0r84J53Cv5ip828kjcx3TyVfh0WFqwMIiMnQF8OR8= X-Received: by 2002:a19:644d:0:b0:513:d5ec:afb with SMTP id b13-20020a19644d000000b00513d5ec0afbmr2728304lfj.40.1710675956845; Sun, 17 Mar 2024 04:45:56 -0700 (PDT) MIME-Version: 1.0 References: <73AC0523-2237-46FD-9885-7AE3F8663DF2@gmail.com> <28CE4FD1-FFB0-4300-81CA-C3CB07E436A6@gmail.com> <19f5939a-9341-4237-90d9-4f1279f03a88@gmail.com> <2f29d2f2-a21b-42bb-997f-3918935d0dba@gmail.com> In-Reply-To: <2f29d2f2-a21b-42bb-997f-3918935d0dba@gmail.com> From: Jonathan Wakely Date: Sun, 17 Mar 2024 11:45:43 +0000 Message-ID: Subject: Re: _LIBCXX_DEBUG value initialized singular iterators assert failures in std algorithms [PR104316] To: =?UTF-8?Q?Fran=C3=A7ois_Dumont?= Cc: Jonathan Wakely , Maciej Miera , "libstdc++" , gcc-patches Content-Type: multipart/alternative; boundary="000000000000bbd9490613d9c861" X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000bbd9490613d9c861 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, 16 Mar 2024, 12:16 Fran=C3=A7ois Dumont, wro= te: > With the patch, sorry. > > On 14/03/2024 22:49, Fran=C3=A7ois Dumont wrote: > > Hi > > > > This is what I started to do. > > > > For now I haven't touch to __cpp_lib_null_iterators definition as > > _Safe_local_iterator still need some work. > > > > libstdc++: Implement N3644 on _Safe_iterator<> [PR114316] > > > > Consider range of value-initialized iterators as valid and empty. > > > > libstdc++-v3/ChangeLog: > > > > PR libstdc++/114316 > > * include/debug/safe_iterator.tcc > > (_Safe_iterator<>::_M_valid_range): > > First check if both iterators are value-initialized before > > checking if > > singular. > > * testsuite/23_containers/set/debug/114316.cc: New test case. > > * testsuite/23_containers/vector/debug/114316.cc: New test case. > > > > Tested under Linux x86_64, ok to commit ? > OK for trunk, thanks! I think this is OK to backport to 13 too. Maybe after this we can define the __cpp_lib_null_itetators macro for debug mode? > > > Fran=C3=A7ois > > > > > > On 12/03/2024 10:52, Jonathan Wakely wrote: > >> On Tue, 12 Mar 2024 at 01:03, Jonathan Wakely > >> wrote: > >>> On Tue, 12 Mar 2024 at 00:55, Maciej Miera > >>> wrote: > >>>> > >>>> > >>>> Wiadomo=C5=9B=C4=87 napisana przez Jonathan Wakely w > >>>> dniu 11.03.2024, o godz. 21:40: > >>>> > >>>> On Mon, 11 Mar 2024 at 20:07, Maciej Miera > >>>> wrote: > >>>> > >>>> > >>>> Hello, > >>>> > >>>> I have tried to introduce an extra level of safety to my codebase > >>>> and utilize _GLIBCXX_DEBUG in my test builds in order to catch > >>>> faulty iterators. > >>>> However, I have encountered the following problem: I would like to > >>>> utilize singular, value-initialized iterators as an arbitrary "null > >>>> range=E2=80=9D. > >>>> However, this leads to failed assertions in std:: algorithms taking > >>>> such range. > >>>> > >>>> Consider the following code sample with find_if: > >>>> > >>>> #include > >>>> #include > >>>> #include > >>>> > >>>> #ifndef __cpp_lib_null_iterators > >>>> #warning "Not standard compliant" > >>>> #endif > >>>> > >>>> int main() > >>>> { > >>>> std::multimap::iterator it1{}; > >>>> std::multimap::iterator it2{}; > >>>> > >>>> (void) (it1=3D=3Dit2); // OK > >>>> (void) std::find_if( > >>>> it1, it2, [](const auto& el) { return el.second =3D=3D 8;}); > >>>> } > >>>> > >>>> Compiled with -std=3Dc++20 and -D_GLIBCXX_DEBUG it produces the > >>>> warning "Not standard compliant" > >>>> and the execution results in the following assert failure: > >>>> > >>>> > /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_algo.h:3875: > >>>> > >>>> In function: > >>>> constexpr _IIter std::find_if(_IIter, _IIter, _Predicate) [with > >>>> _IIter =3D > >>>> gnu_debug::_Safe_iterator<_Rb_tree_iterator >, > >>>> debug::multimap, bidirectional_iterator_tag>; > >>>> _Predicate =3D > >>>> main()::] > >>>> > >>>> The question is though: is it by design, or is it just a mere > >>>> oversight? The warning actually suggest the first option. > >>>> If it is an intentional design choice, could you provide some > >>>> rationale behind it, please? > >>>> > >>>> > >>>> The macro was not defined because the C++14 rule wasn't implemented > >>>> for debug mode, but that should have been fixed for GCC 11, according > >>>> to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98466 and > >>>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D70303 > >>>> So we should be able to define macro now, except maybe it wasn't fix= ed > >>>> for the RB tree containers. > >>>> > >>>> > >>>> > >>>> Just to make sure there are no misunderstandings: comparison via =3D= =3D > >>>> works fine. The feature check macro without _GLIBCXX_DEBUG and with > >>>> included is also fine. Maybe the need to include a > >>>> header is the issue, but that=E2=80=99s not the core of the problem = anyway. > >>> No, it has nothing to do with the headers included. The feature test > >>> macro is defined like so: > >>> > >>> # if (__cplusplus >=3D 201402L) && (!defined(_GLIBCXX_DEBUG)) > >>> # define __glibcxx_null_iterators 201304L > >>> > >>> It's a very deliberate choice to not define it when _GLIBCXX_DEBUG is > >>> defined. But as I said, I think we should have changed that. > >>> > >>>> The actual question is though, whether passing singular iterators > >>>> to std algorithms (like find_if) should make the asserts at the > >>>> beginning of the algo function fail when compiled with > >>>> _GLIBCXX_DEBUG. IMHO, intuitively it should not, as comparing > >>>> iterators equal would just ensure early exit and return of the same > >>>> singular iterator. > >>>> This seems not to be the case though. The actual message is this: > >>>> Error: the function requires a valid iterator range [first, last). > >>>> What bothers me is whether the empty virtual range limited by two > >>>> same singular iterators is actually valid or not. > >>> Yes, it's valid. So the bug is in the __glibcxx_requires_valid_range > >>> macro. > >> Thanks for the bugzilla report: > >> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114316 > >> We'll get it fixed! > >> --000000000000bbd9490613d9c861--