Skip to content

Commit c66ebae

Browse files
footkaobdev
andauthored
Fix the insecure comparison function
* fix rpc error Co-authored-by: footka <672528926@qq.com> * Fix the insecure comparison function Co-authored-by: footka <footka@users.noreply.github.com> --------- Co-authored-by: obdev <obdev@oceanbase.com> Co-authored-by: footka <footka@users.noreply.github.com>
1 parent 0f02926 commit c66ebae

1 file changed

Lines changed: 23 additions & 49 deletions

File tree

src/sql/engine/sort/ob_sort_compare_vec_op.ipp

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,24 @@ bool GeneralCompare<Store_Row, has_addon>::operator()(const Store_Row *l, const
2424
{
2525
bool less = false;
2626
int &ret = ret_;
27-
if (OB_UNLIKELY(OB_SUCCESS != ret)) {
28-
// Use pointer comparison as a consistent fallback to maintain strict weak ordering.
29-
// Returning false for all pairs would cause introsort's right-scan loop
30-
// (while !comp(pivot, *i)) to advance past array bounds, triggering abort().
31-
less = l < r;
32-
} else if (OB_FAIL(fast_check_status())) {
33-
SQL_ENG_LOG(WARN, "fast check failed", K(ret));
34-
less = l < r;
27+
if (CompareBase::ENABLE == encode_sk_state_) {
28+
ObLength l_len = 0;
29+
ObLength r_len = 0;
30+
const char *l_data = nullptr;
31+
const char *r_data = nullptr;
32+
l->get_cell_payload(*sk_row_meta_, 0, l_data, l_len);
33+
r->get_cell_payload(*sk_row_meta_, 0, r_data, r_len);
34+
int cmp = 0;
35+
cmp = MEMCMP(l_data, r_data, min(l_len, r_len));
36+
less = cmp != 0 ? (cmp < 0) : (l_len - r_len) < 0;
37+
} else if (CompareBase::FALLBACK_TO_DISABLE == encode_sk_state_ && has_addon) {
38+
const Store_Row *l_real_cmp_row = l->get_addon_ptr(*sk_row_meta_);
39+
const Store_Row *r_real_cmp_row = r->get_addon_ptr(*sk_row_meta_);
40+
less = (compare(l_real_cmp_row, r_real_cmp_row, addon_row_meta_) > 0);
3541
} else {
36-
if (CompareBase::ENABLE == encode_sk_state_) {
37-
ObLength l_len = 0;
38-
ObLength r_len = 0;
39-
const char *l_data = nullptr;
40-
const char *r_data = nullptr;
41-
l->get_cell_payload(*sk_row_meta_, 0, l_data, l_len);
42-
r->get_cell_payload(*sk_row_meta_, 0, r_data, r_len);
43-
int cmp = 0;
44-
cmp = MEMCMP(l_data, r_data, min(l_len, r_len));
45-
less = cmp != 0 ? (cmp < 0) : (l_len - r_len) < 0;
46-
} else if (CompareBase::FALLBACK_TO_DISABLE == encode_sk_state_ && has_addon) {
47-
const Store_Row *l_real_cmp_row = l->get_addon_ptr(*sk_row_meta_);
48-
const Store_Row *r_real_cmp_row = r->get_addon_ptr(*sk_row_meta_);
49-
less = (compare(l_real_cmp_row, r_real_cmp_row, addon_row_meta_) > 0);
50-
} else {
51-
__builtin_prefetch(l, 0 /* read */, 2 /*high temp locality*/);
52-
__builtin_prefetch(r, 0 /* read */, 2 /*high temp locality*/);
53-
less = (compare(l, r, sk_row_meta_) > 0);
54-
}
42+
__builtin_prefetch(l, 0 /* read */, 2 /*high temp locality*/);
43+
__builtin_prefetch(r, 0 /* read */, 2 /*high temp locality*/);
44+
less = (compare(l, r, sk_row_meta_) > 0);
5545
}
5646
return less;
5747
}
@@ -263,17 +253,9 @@ template <typename Store_Row, bool is_basic_cmp, bool is_topn_sort>
263253
bool SingleColCompare<Store_Row, is_basic_cmp, is_topn_sort>::operator()(const Store_Row *l, const Store_Row *r)
264254
{
265255
bool less = false;
266-
int &ret = ret_;
267-
if (OB_UNLIKELY(OB_SUCCESS != ret)) {
268-
less = l < r;
269-
} else if (OB_FAIL(fast_check_status())) {
270-
SQL_ENG_LOG(WARN, "fast check failed", K(ret));
271-
less = l < r;
272-
} else {
273-
__builtin_prefetch(l, 0 /* read */, 2 /*high temp locality*/);
274-
__builtin_prefetch(r, 0 /* read */, 2 /*high temp locality*/);
275-
less = (compare(l, r, sk_row_meta_) > 0);
276-
}
256+
__builtin_prefetch(l, 0 /* read */, 2 /*high temp locality*/);
257+
__builtin_prefetch(r, 0 /* read */, 2 /*high temp locality*/);
258+
less = (compare(l, r, sk_row_meta_) > 0);
277259
return less;
278260
}
279261

@@ -497,17 +479,9 @@ template <typename Store_Row, bool has_addon>
497479
bool FixedCompare<Store_Row, has_addon>::operator()(const Store_Row *l, const Store_Row *r)
498480
{
499481
bool less = false;
500-
int &ret = ret_;
501-
if (OB_UNLIKELY(OB_SUCCESS != ret)) {
502-
less = l < r;
503-
} else if (OB_FAIL(fast_check_status())) {
504-
SQL_ENG_LOG(WARN, "fast check failed", K(ret));
505-
less = l < r;
506-
} else {
507-
__builtin_prefetch(l, 0 /* read */, 2 /*high temp locality*/);
508-
__builtin_prefetch(r, 0 /* read */, 2 /*high temp locality*/);
509-
less = (compare(l, r, sk_row_meta_) > 0);
510-
}
482+
__builtin_prefetch(l, 0 /* read */, 2 /*high temp locality*/);
483+
__builtin_prefetch(r, 0 /* read */, 2 /*high temp locality*/);
484+
less = (compare(l, r, sk_row_meta_) > 0);
511485
return less;
512486
}
513487

0 commit comments

Comments
 (0)