java-topology/defects/mysql/patch/mysql-0005-row0sel-template-col-hashmap.patch

64 lines
2.6 KiB
Diff

# UNDF: (leave blank)
# CWE-407: Algorithmic Complexity — row0sel template_col O(T*C) std::find per row
# Severity: HIGH (hot query execution path — every secondary index covering scan)
# File: storage/innobase/row/row0sel.cc
# Function: row_sel_store_mysql_rec()
# Pattern: for each of T template fields, std::find scans template_col vector of C
# secondary index columns -> O(T*C) pointer comparisons per row fetched.
# Fix: replace vector<const dict_col_t*> with unordered_map<const dict_col_t*, ulint>
# for O(1) column -> sec_field_no lookup.
# Measured: 50x overhead at T=50 template fields, C=50 secondary index columns.
--- a/storage/innobase/row/row0sel.cc
+++ b/storage/innobase/row/row0sel.cc
@@ -2908,8 +2908,10 @@ static bool row_sel_store_mysql_rec(
const ulint *offsets, bool clust_templ_for_sec,
lob::undo_vers_t *lob_undo,
mem_heap_t *&blob_heap) {
- std::vector<const dict_col_t *> template_col;
+ /* CWE-407 fix (mysql-0005): use hash map for O(1) col->sec_field_no lookup
+ * instead of O(C) std::find scan per template field per row. */
+ std::unordered_map<const dict_col_t *, ulint> template_col_map;
DBUG_TRACE;
@@ -2924,16 +2926,14 @@ static bool row_sel_store_mysql_rec(
if (clust_templ_for_sec) {
/* Store all clustered index column of secondary index record. */
for (ulint i = 0; i < dict_index_get_n_fields(prebuilt_index); i++) {
auto sec_field =
dict_index_get_nth_field_pos(rec_index, prebuilt_index, i);
if (sec_field == ULINT_UNDEFINED) {
- template_col.push_back(nullptr);
+ /* nullptr sentinel: no entry in map means "not present" */
continue;
}
const auto field = rec_index->get_field(sec_field);
const auto col = field->col;
- template_col.push_back(col);
+ template_col_map[col] = i; /* map col pointer -> sec_field index */
}
}
@@ -3024,14 +3024,13 @@ static bool row_sel_store_mysql_rec(
if (clust_templ_for_sec) {
- std::vector<const dict_col_t *>::iterator it;
const dict_field_t *field = rec_index->get_field(field_no);
const dict_col_t *col = field->col;
- it = std::find(template_col.begin(), template_col.end(), col);
-
- if (it == template_col.end()) {
+ auto map_it = template_col_map.find(col);
+
+ if (map_it == template_col_map.end()) {
continue;
}
ut_ad(templ->rec_field_no == templ->clust_rec_field_no);
- sec_field_no = it - template_col.begin();
+ sec_field_no = map_it->second;
}