]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::deque::operator[] Xmethod [PR112491]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 14 Nov 2023 15:08:13 +0000 (15:08 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 15 Nov 2023 11:16:49 +0000 (11:16 +0000)
The Xmethod for std::deque::operator[] has the same bug that I recently
fixed for the std::deque::size() Xmethod. The first node might have
unused capacity at the start, which needs to be accounted for when
indexing into the deque.

libstdc++-v3/ChangeLog:

PR libstdc++/112491
* python/libstdcxx/v6/xmethods.py (DequeWorkerBase.index):
Correctly handle unused capacity at the start of the first node.
* testsuite/libstdc++-xmethods/deque.cc: Check index operator
when elements have been removed from the front.

libstdc++-v3/python/libstdcxx/v6/xmethods.py
libstdc++-v3/testsuite/libstdc++-xmethods/deque.cc

index dcef285180a8dacf7358100a875116aff069bf43..f29bc2d40fb42c35d558f7abb48e24c8ca4688d5 100644 (file)
@@ -195,7 +195,7 @@ class DequeWorkerBase(gdb.xmethod.XMethodWorker):
     def size(self, obj):
         start = obj['_M_impl']['_M_start']
         finish = obj['_M_impl']['_M_finish']
-        if not start['_M_node']:
+        if start['_M_cur'] == finish['_M_cur']:
             return 0
         return (self._bufsize
                 * (finish['_M_node'] - start['_M_node'] - 1)
@@ -203,9 +203,13 @@ class DequeWorkerBase(gdb.xmethod.XMethodWorker):
                 + (start['_M_last'] - start['_M_cur']))
 
     def index(self, obj, idx):
-        first_node = obj['_M_impl']['_M_start']['_M_node']
-        index_node = first_node + int(idx) // self._bufsize
-        return index_node[0][idx % self._bufsize]
+        start = obj['_M_impl']['_M_start']
+        first_node_size = start['_M_last'] - start['_M_cur']
+        if idx < first_node_size:
+            return start['_M_cur'][idx]
+        idx = idx - first_node_size
+        index_node = start['_M_node'][1 + int(idx) // self._bufsize]
+        return index_node[idx % self._bufsize]
 
 
 class DequeEmptyWorker(DequeWorkerBase):
index e4077c14ff5a18b9ba1170abbe45b15146489312..6058d23c87ab3cacbc8bcda4ec697369136ce670 100644 (file)
@@ -69,20 +69,24 @@ main ()
 
   // PR libstdc++/112491
   std::deque<int> q5;
-  q5.push_front(0);
+  q5.push_front(5);
 // { dg-final { note-test q5.size() 1 } }
+// { dg-final { note-test q5\[0\] 5 } }
   std::deque<int> q6 = q1;
   q6.pop_front();
 // { dg-final { note-test {q6.size() == (q1_size-1)} true } }
+// { dg-final { note-test q6\[1\] 102 } }
   std::deque<int> q7 = q2;
   q7.pop_front();
   q7.pop_front();
 // { dg-final { note-test {q7.size() == (q2_size-2)} true } }
+// { dg-final { note-test q7\[1\] 203 } }
   std::deque<int> q8 = q3;
   q8.pop_front();
   q8.pop_front();
   q8.pop_front();
 // { dg-final { note-test {q8.size() == (q3_size-3)} true } }
+// { dg-final { note-test q8\[1\] 304 } }
   std::deque<int> q9 = q8;
   q9.clear();
 // { dg-final { note-test q9.size() 0 } }