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 08D6C38515F3 for ; Thu, 27 Jan 2022 23:31:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 08D6C38515F3 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-389-kOCrrXyCNGOCan_U6tnNvA-1; Thu, 27 Jan 2022 18:31:20 -0500 X-MC-Unique: kOCrrXyCNGOCan_U6tnNvA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 391BC1006AA0; Thu, 27 Jan 2022 23:31:19 +0000 (UTC) Received: from localhost (unknown [10.33.37.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9F4C101F6CA; Thu, 27 Jan 2022 23:31:18 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Prevent -Wstringop-overread warning in std::deque [PR100516] Date: Thu, 27 Jan 2022 23:31:17 +0000 Message-Id: <20220127233117.1112432-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Jan 2022 23:31:23 -0000 Tested powerpc64le-linux, pushed to trunk. The compiler warns about the loop in deque::_M_range_initialize because it doesn't know that the number of nodes has already been correctly sized to match the size of the input. Use __builtin_unreachable to tell it that the loop will never be entered if the number of elements is smaller than a single node. libstdc++-v3/ChangeLog: PR libstdc++/100516 * include/bits/deque.tcc (_M_range_initialize): Add __builtin_unreachable to loop. * testsuite/23_containers/deque/100516.cc: New test. --- libstdc++-v3/include/bits/deque.tcc | 3 +++ .../testsuite/23_containers/deque/100516.cc | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 libstdc++-v3/testsuite/23_containers/deque/100516.cc diff --git a/libstdc++-v3/include/bits/deque.tcc b/libstdc++-v3/include/bits/deque.tcc index 80f1813bc76..03e0a505e14 100644 --- a/libstdc++-v3/include/bits/deque.tcc +++ b/libstdc++-v3/include/bits/deque.tcc @@ -454,6 +454,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER __cur_node < this->_M_impl._M_finish._M_node; ++__cur_node) { + if (__n < _S_buffer_size()) + __builtin_unreachable(); // See PR 100516 + _ForwardIterator __mid = __first; std::advance(__mid, _S_buffer_size()); std::__uninitialized_copy_a(__first, __mid, *__cur_node, diff --git a/libstdc++-v3/testsuite/23_containers/deque/100516.cc b/libstdc++-v3/testsuite/23_containers/deque/100516.cc new file mode 100644 index 00000000000..ef32ae10545 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/deque/100516.cc @@ -0,0 +1,14 @@ +// { dg-options "-O2 -Wstringop-overread" } +// { dg-do compile { target c++11 } } + +// Bug 100516 +// Unexpected -Wstringop-overread in deque initialization from empty +// initializer_list + +#include + +void f() +{ + std::initializer_list il{}; + std::deque{il}; +} -- 2.34.1