]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cpp-btree: fix array bounds warning in btree_node::swap() 65450/head
authorKefu Chai <tchaikov@gmail.com>
Tue, 9 Sep 2025 10:10:18 +0000 (18:10 +0800)
committerKefu Chai <tchaikov@gmail.com>
Wed, 10 Sep 2025 08:07:44 +0000 (16:07 +0800)
Replace direct array indexing with pointer arithmetic to avoid
compiler warning when forming ranges for std::swap_ranges().

The original code was functionally correct but triggered
-Werror=array-bounds when accessing mutable_child(count + 1)
to create the end iterator. Using pointer arithmetic achieves
the same result without the bounds check warning.

No functional changes.

Fixes: https://tracker.ceph.com/issues/72477
Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/include/cpp-btree/btree.h

index 2eddc2abe80cf74d1c71fa0e7f1d11335ac43546..66538a25a22d963d1269f9f6e3d3dad2d6d8f00a 100644 (file)
@@ -462,8 +462,8 @@ class btree_node {
 
   void set_parent(btree_node *p) { GetField<&base_fields::parent>() = p; }
   field_type &mutable_count() { return GetField<&base_fields::count>(); }
-  slot_type *slot(int i) { return &GetField<&leaf_fields::values>()[i]; }
-  const slot_type *slot(int i) const { return &GetField<&leaf_fields::values>()[i]; }
+  slot_type *slot(int i) { return GetField<&leaf_fields::values>() + i; }
+  const slot_type *slot(int i) const { return GetField<&leaf_fields::values>() + i; }
   void set_position(field_type v) { GetField<&base_fields::position>() = v; }
   void set_count(field_type v) { GetField<&base_fields::count>() = v; }
   // This method is only called by the node init methods.
@@ -1700,9 +1700,12 @@ void btree_node<P>::swap(btree_node *x, allocator_type *alloc) {
 
   if (!leaf()) {
     // Swap the child pointers.
-    std::swap_ranges(&smaller->mutable_child(0),
-                     &smaller->mutable_child(smaller->count() + 1),
-                     &larger->mutable_child(0));
+    auto* smaller_begin = &smaller->mutable_child(0);
+    auto* larger_begin = &larger->mutable_child(0);
+    auto count = smaller->count() + 1;
+    std::swap_ranges(smaller_begin,
+                     smaller_begin + count,
+                     larger_begin);
     // Update swapped children's parent pointers.
     int i = 0;
     for (; i <= smaller->count(); ++i) {