public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r14-5450] libstdc++: Fix std::deque::size() Xmethod [PR112491]
Date: Tue, 14 Nov 2023 15:58:29 +0000 (GMT)	[thread overview]
Message-ID: <20231114155829.6148F3858D20@sourceware.org> (raw)

https://gcc.gnu.org/g:4db820928065eccbeb725406450d826186582b9f

commit r14-5450-g4db820928065eccbeb725406450d826186582b9f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 14 15:08:13 2023 +0000

    libstdc++: Fix std::deque::size() Xmethod [PR112491]
    
    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.

Diff:
---
 libstdc++-v3/python/libstdcxx/v6/xmethods.py       | 13 ++++++++-----
 libstdc++-v3/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<int> q5;
+  q5.push_front(0);
+// { dg-final { note-test q5.size() 1 } }
+  std::deque<int> q6 = q1;
+  q6.pop_front();
+// { dg-final { note-test {q6.size() == (q1_size-1)} true } }
+  std::deque<int> q7 = q2;
+  q7.pop_front();
+  q7.pop_front();
+// { dg-final { note-test {q7.size() == (q2_size-2)} true } }
+  std::deque<int> q8 = q3;
+  q8.pop_front();
+  q8.pop_front();
+  q8.pop_front();
+// { dg-final { note-test {q8.size() == (q3_size-3)} true } }
+  std::deque<int> q9 = q8;
+  q9.clear();
+// { dg-final { note-test q9.size() 0 } }
+
   return 0;  // Mark SPOT
 }

                 reply	other threads:[~2023-11-14 15:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231114155829.6148F3858D20@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).