From: Kefu Chai Date: Tue, 9 Sep 2025 10:10:18 +0000 (+0800) Subject: cpp-btree: fix array bounds warning in btree_node::swap() X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8458a19abf810e41ad8e4616dca2e50de353594e;p=ceph.git cpp-btree: fix array bounds warning in btree_node::swap() 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 --- diff --git a/src/include/cpp-btree/btree.h b/src/include/cpp-btree/btree.h index 2eddc2abe80c..66538a25a22d 100644 --- a/src/include/cpp-btree/btree.h +++ b/src/include/cpp-btree/btree.h @@ -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

::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) {