From patchwork Tue Nov 14 15:58:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 165026 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a59:b0:164:83eb:24d7 with SMTP id 25csp2075124rwb; Tue, 14 Nov 2023 09:18:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IFYpODf63L33Jy9JWxco/JA23c1oKd6k3cMJDD38gZNCYhGFYkATsiYIc3Cqclt/6Ch9m3v X-Received: by 2002:a05:6808:178e:b0:3a8:4d1f:9da5 with SMTP id bg14-20020a056808178e00b003a84d1f9da5mr11471221oib.31.1699982325228; Tue, 14 Nov 2023 09:18:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699982325; cv=pass; d=google.com; s=arc-20160816; b=ciDBwPUZUA0dAst2swn5dlG+/uQ38DI33t1D66xqDKsP8ZlCPM+wVdi/groDctBx1N 3cqP0NXRvTkmpUitY1IInrqF7SZ144zOhTbQcQSi4vveLQ29Xu/Y7buDN+4BZj9Iv7r8 wsW5Dvu/bTPDmMwveo2rca2agkVkI3HZX19D1JslUE+meNk1G0NaU4+YLo6gZkyeoeGM 1kQDw4zGEJndrpv7ym5AiTpF3Nm6FYXVy01zaPPFRXjp0Tv8PJyoFaS89aCrMtlyElVb 1STXON9wZ88vqNE1sfhHpEa0TWdTlibqdbnPEzFQZQmBoP0Y0uW4fBlPJ50TTarM5S6M 3VNg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=ObwgSDDoChEYjIlX2Lg05uLbjIPdgduz7S2JNxa/XOo=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=yhCqoc+iV3GRUf7lPJFBRTI9SK6X3Cw8cKzkUdFfdKj0z/c86TPazOn8sOW8lhkJGM gRspImMfCBNnoYpqgO8Vjh4LqQ8H4I+M+AalDG1ThY3PSAvtfFLrfjL0lhW5g7hZf2kc vpQLtHrKRqBTZ8WKgQY/bubeQERY4SvZ5VloOfWjwFfYHo4Kq3GutC9Entw1Fc/7Y64O qooZ3rN2DH1J4nWirPa6AiFoih7MpymHey8Jg1jL+62IhjxnKsvbMkDxk40lC4LbfUOe 3uO+L0rMUwPvS/SjRHNYRyAL6IFt1oRN2pq73XRh5W3qdIyjwcvSa0s8M5bG0+iNQDDS cTQQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dsCSvH8o; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id e6-20020a056102034600b0045fba8a621csi1018656vsa.558.2023.11.14.09.18.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 09:18:44 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dsCSvH8o; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBA7F3858019 for ; Tue, 14 Nov 2023 15:59:17 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 C2FFA3858D39 for ; Tue, 14 Nov 2023 15:58:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2FFA3858D39 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 C2FFA3858D39 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=1699977533; cv=none; b=kHcQpMClhUBOm4rnRasI06wukhKWY8I5vWztcKPhsydBoVSrLkcuSkOkUVv5CH0m9ohfEqQKQcIyRyQpQfxkih7MvIKLxFJWxgBH/CMf3dpiweq03awSyh1cgll/PKOvRtYH21TJVXtiD6PR03E5wXFJwNQegFKcve+t2TdkDjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699977533; c=relaxed/simple; bh=dTguaBvT2KzTP8/c9HC1oARNp4Q+ACqSaCeZ0valpLg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XYRholSnCBrbcnxdoRUAtmamcghGxBQwKQu+3Q0idel36ZGi1SxU1qNX0USsfAmaI7jFpdJOVnOGIFySQpDyIXNpWFnANdarHIcDBwbGnbR6zGflP+x2wfR28894wxh/ObnuAccHia3iP+vi8NLm/28qvep9qwDSvOO/zTlvcRA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699977531; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ObwgSDDoChEYjIlX2Lg05uLbjIPdgduz7S2JNxa/XOo=; b=dsCSvH8o84xXVksLLYFfJ3q2uDICGyid3gQeW3Pdz3fIQsMBFqEq/2HrsR4q4wusdIU/L/ CTJb69Vr33W0U73sMJ2dOWg4sTnosH9wikUxm3xNBnDFTTwYcgM56l9KvXw/XRVmXOfpCX AwTB3jkGTJtIQhMly9aWsOad5KOmncE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-BUT5TnhEOeeVMtrD8-kfZA-1; Tue, 14 Nov 2023 10:58:50 -0500 X-MC-Unique: BUT5TnhEOeeVMtrD8-kfZA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C9B1329AB3F0; Tue, 14 Nov 2023 15:58:49 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 94AD91C060B0; Tue, 14 Nov 2023 15:58:49 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::deque::size() Xmethod [PR112491] Date: Tue, 14 Nov 2023 15:58:29 +0000 Message-ID: <20231114155848.892568-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782560665269392936 X-GMAIL-MSGID: 1782560665269392936 Tested x86_64-linux. Pushed to trunk. Backports to follow. -- >8 -- The Xmethod for std::deque::size() assumed that the first element would be at the start of the first node. That's only true if elements are only added at the back. If an element is inserted at the front, or removed from the front (or anywhere before the middle) then the first node will not be completely populated, and the Xmethod will give the wrong result. libstdc++-v3/ChangeLog: PR libstdc++/112491 * python/libstdcxx/v6/xmethods.py (DequeWorkerBase.size): Fix calculation to use _M_start._M_cur. * testsuite/libstdc++-xmethods/deque.cc: Check failing cases. --- libstdc++-v3/python/libstdcxx/v6/xmethods.py | 13 +++++++----- .../testsuite/libstdc++-xmethods/deque.cc | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/python/libstdcxx/v6/xmethods.py b/libstdc++-v3/python/libstdcxx/v6/xmethods.py index 42e60eb57b1..dcef285180a 100644 --- a/libstdc++-v3/python/libstdcxx/v6/xmethods.py +++ b/libstdc++-v3/python/libstdcxx/v6/xmethods.py @@ -193,11 +193,14 @@ class DequeWorkerBase(gdb.xmethod.XMethodWorker): self._bufsize = 512 // val_type.sizeof or 1 def size(self, obj): - first_node = obj['_M_impl']['_M_start']['_M_node'] - last_node = obj['_M_impl']['_M_finish']['_M_node'] - cur = obj['_M_impl']['_M_finish']['_M_cur'] - first = obj['_M_impl']['_M_finish']['_M_first'] - return (last_node - first_node) * self._bufsize + (cur - first) + start = obj['_M_impl']['_M_start'] + finish = obj['_M_impl']['_M_finish'] + if not start['_M_node']: + return 0 + return (self._bufsize + * (finish['_M_node'] - start['_M_node'] - 1) + + (finish['_M_cur'] - finish['_M_first']) + + (start['_M_last'] - start['_M_cur'])) def index(self, obj, idx): first_node = obj['_M_impl']['_M_start']['_M_node'] diff --git a/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc b/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc index 2d7e846334f..e4077c14ff5 100644 --- a/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc +++ b/libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc @@ -67,6 +67,26 @@ main () // { dg-final { whatis-test q1.back() int } } // { dg-final { whatis-test q3\[0\] int } } + // PR libstdc++/112491 + std::deque q5; + q5.push_front(0); +// { dg-final { note-test q5.size() 1 } } + std::deque q6 = q1; + q6.pop_front(); +// { dg-final { note-test {q6.size() == (q1_size-1)} true } } + std::deque q7 = q2; + q7.pop_front(); + q7.pop_front(); +// { dg-final { note-test {q7.size() == (q2_size-2)} true } } + std::deque q8 = q3; + q8.pop_front(); + q8.pop_front(); + q8.pop_front(); +// { dg-final { note-test {q8.size() == (q3_size-3)} true } } + std::deque q9 = q8; + q9.clear(); +// { dg-final { note-test q9.size() 0 } } + return 0; // Mark SPOT }