From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 9AC8E3858CD1 for ; Tue, 12 Mar 2024 01:03:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9AC8E3858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9AC8E3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710205418; cv=none; b=dJGrAU75X4GGyHV4T0FE9rrDoJuBavAF5W0fkWdU4DhQTWGCI+q/JYqk8RHcvnLYbR7udC47UWcl5/cM6XMab8gdDAkv0TtvyYZphAZkeX6zKysjJ5K+ZpJVf3UUXcLZIJhNO2Vq5VJdact00D/3cHzfEGsYuB3egd6tSfctiRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710205418; c=relaxed/simple; bh=9at/7NvRNn43fYiWqPS59HLT0Z0OJyoS1fPl0AFuPfk=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=ZF5ELlVZRAVm51GgoNlx2VdrDPxPzi8kBldmG5K33j/wynyOTbt35ZXcP+oU3CvR8nChMy6/dTLtc56mTvA/TGfGoEzBaAjx35X0a7CzgTUB9XEreBiGRu3eDTlpu5JWoRQ2QAyX+AcGChIY9yasBWkm75mfoQLLXePbKTWrSqs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710205407; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gLJGw07jGtF9s5zJeCKbpVMFs7FOMNUjZFh7uq0htsg=; b=h0HBNkc6tB/XxltM6L+8/IXu7bF6vidbkxDaX0lo+b7Rk4yq8t+V0nBHD8tMt8lR69N2Yd D0OS8AIiKxP3Di/7QULHegQlf/oCqn1icBMizZbm5VNLkouyKnw3xbsfYzTabiZylojggI GQ/U7/hdKbqRIKTNDtVlKabMOCfzul8= Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-461-9GRYSJV2N16HNfTRslb2zw-1; Mon, 11 Mar 2024 21:03:25 -0400 X-MC-Unique: 9GRYSJV2N16HNfTRslb2zw-1 Received: by mail-yb1-f197.google.com with SMTP id 3f1490d57ef6-dcf22e5b70bso6372314276.1 for ; Mon, 11 Mar 2024 18:03:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710205405; x=1710810205; h=content-transfer-encoding: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=gLJGw07jGtF9s5zJeCKbpVMFs7FOMNUjZFh7uq0htsg=; b=IwjCaN2AcJRZPe7MPEZMLSQiUQLe8qnlqsnGtZ4fSytxX4O7y7XsmYNYcKZiBYZVBi tGWhhuN/bEFldren6Z7+x6yi4wjUbmHTJnKhVWTajH6pMMYywJ61XetqQi8o5YdfPMDh 1+oxjIsT77NZlkkh8f9cyaGtZuNIi+Kig6kAnuIIf66eWnzRfz9I9LVEnfRWAVuNzaH3 ewFzH2aqAYxlRluVjto4ZQNnTZpiZY49oSMTetH2TKWfZmzuyeaQebv2/kZAscHE0Pne nPLSzi5KvmVQrWdJPGB1uWgmQnHwyaK/u2I5Wa0bfs36LkTHWP0HLtxnulkVf759Usqq D1zw== X-Gm-Message-State: AOJu0YxRLinTth1EmfkizIuq5uMQ5LdaHJ2MAHw8Pe306SErCdJJ/jBc Rwm5bMJq3BqstInLSKYKNIjALB+OC1qhcw9FbTy2b9bvdiJ0axPPmVznOkqDtKbd4lEDPsCtS61 /I/5hqZcdKZmULbNOI13vLSUqoZq0jQrz2KDSQ4ZdUUxmM0MvJxYicKJhbF8QJBx+H8C/09EEDa UMsBhkWNQrmgc5votn6THBjynOHD8= X-Received: by 2002:a25:688e:0:b0:dc5:f51e:6a60 with SMTP id d136-20020a25688e000000b00dc5f51e6a60mr5806814ybc.6.1710205405222; Mon, 11 Mar 2024 18:03:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFGinLogLdXEupGQGZx9qrImy0m99wEp3Zp9PlUmpuHaOvHfVbf6NDOCfjuEBFLf5d/1uRTKJXYZvbJ8A1wrao= X-Received: by 2002:a25:688e:0:b0:dc5:f51e:6a60 with SMTP id d136-20020a25688e000000b00dc5f51e6a60mr5806804ybc.6.1710205404940; Mon, 11 Mar 2024 18:03:24 -0700 (PDT) MIME-Version: 1.0 References: <73AC0523-2237-46FD-9885-7AE3F8663DF2@gmail.com> <28CE4FD1-FFB0-4300-81CA-C3CB07E436A6@gmail.com> In-Reply-To: <28CE4FD1-FFB0-4300-81CA-C3CB07E436A6@gmail.com> From: Jonathan Wakely Date: Tue, 12 Mar 2024 01:03:09 +0000 Message-ID: Subject: Re: _LIBCXX_DEBUG value initialized singular iterators assert failures in std algorithms To: Maciej Miera Cc: libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,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: 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 uti= lize _GLIBCXX_DEBUG in my test builds in order to catch faulty iterators. > However, I have encountered the following problem: I would like to utiliz= e 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 b= ehind 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 fixed > for the RB tree containers. > > > > Just to make sure there are no misunderstandings: comparison via =3D=3D w= orks 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 a= lgo function fail when compiled with _GLIBCXX_DEBUG. IMHO, intuitively it s= hould not, as comparing iterators equal would just ensure early exit and re= turn 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 si= ngular iterators is actually valid or not. Yes, it's valid. So the bug is in the __glibcxx_requires_valid_range macro. > This occurs with various std containers, I did check vector, multimap and= unordered_map; I can check all the containers but I daresay it=E2=80=99s a= pretty general thing. > > Best regards, > A. M. Miera