Skip to content

Commit ab90eaa

Browse files
committed
MDEV-22695 Server crashes in heap_rnext upon DELETE from a HEAP table
Quick read record uses different handler (H1) for finding records. It cannot use ha_delete_row() handler (H2) as it is different search mode: inited == INDEX for H1, inited == RND for H2. So, read handler H1 uses index while write handler H2 uses random access. For going next record in H1 there is info->last_pos optimization for stepping index via tree_search_next(). This optimization can work with deleted rows only if delete is conducted in the same handler, there is: 67 int hp_rb_delete_key(HP_INFO *info, register HP_KEYDEF *keyinfo, 68 const uchar *record, uchar *recpos, int flag) 69 { ... 74 if (flag) 75 info->last_pos= NULL; /* For heap_rnext/heap_rprev */ But this cannot work for different handler. So, last_pos in H1 after delete in H2 contains stale info->parents array and last_pos points into that parents. In the specific test case last_pos' parent is already freed node and tree_search_next() steps into it. The fix invalidates local savings of info->parents and info->last_pos based on key_version. Record deletion increments share->key_version in H2, so in H1 we know the tree might be changed. Another good measure would be to use H1 for delete. But this is bigger refactoring than just bug fixing.
1 parent 4a58d10 commit ab90eaa

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

mysql-test/suite/heap/heap.result

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,10 @@ DELETE FROM t1 WHERE ts = 1 AND color = 'GREEN';
877877
SELECT * from t1 WHERE ts = 1 AND color = 'GREEN';
878878
id color ts
879879
DROP TABLE t1;
880+
#
881+
# MDEV-22695 Server crashes in heap_rnext upon DELETE from a HEAP table
882+
#
883+
CREATE TABLE t1 (a VARCHAR(128), b VARCHAR(32), KEY(a) USING BTREE, KEY(b) USING BTREE) ENGINE=HEAP;
884+
INSERT INTO t1 VALUES ('foo',NULL),('m','b'),(6,'j'),('bar','qux'),(NULL,NULL);
885+
DELETE FROM t1 WHERE a <=> 'm' OR b <=> NULL;
886+
DROP TABLE t1;

mysql-test/suite/heap/heap.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,12 @@ INSERT INTO t1 VALUES("7","GREEN", 2);
659659
DELETE FROM t1 WHERE ts = 1 AND color = 'GREEN';
660660
SELECT * from t1 WHERE ts = 1 AND color = 'GREEN';
661661
DROP TABLE t1;
662+
663+
--echo #
664+
--echo # MDEV-22695 Server crashes in heap_rnext upon DELETE from a HEAP table
665+
--echo #
666+
CREATE TABLE t1 (a VARCHAR(128), b VARCHAR(32), KEY(a) USING BTREE, KEY(b) USING BTREE) ENGINE=HEAP;
667+
INSERT INTO t1 VALUES ('foo',NULL),('m','b'),(6,'j'),('bar','qux'),(NULL,NULL);
668+
DELETE FROM t1 WHERE a <=> 'm' OR b <=> NULL;
669+
# Cleanup
670+
DROP TABLE t1;

storage/heap/hp_rnext.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int heap_rnext(HP_INFO *info, uchar *record)
4646
&info->last_pos, offsetof(TREE_ELEMENT, left));
4747
}
4848
}
49-
else if (info->last_pos)
49+
else if (info->last_pos && info->key_version == info->s->key_version)
5050
{
5151
/*
5252
We enter this branch for non-DELETE queries after heap_rkey()
@@ -72,6 +72,7 @@ int heap_rnext(HP_INFO *info, uchar *record)
7272
*/
7373
pos= tree_search_edge(&keyinfo->rb_tree, info->parents,
7474
&info->last_pos, offsetof(TREE_ELEMENT, left));
75+
info->key_version= info->s->key_version;
7576
}
7677
else
7778
{
@@ -87,6 +88,7 @@ int heap_rnext(HP_INFO *info, uchar *record)
8788
info->last_find_flag= HA_READ_KEY_OR_NEXT;
8889
pos = tree_search_key(&keyinfo->rb_tree, info->lastkey, info->parents,
8990
&info->last_pos, info->last_find_flag, &custom_arg);
91+
info->key_version= info->s->key_version;
9092
}
9193
if (pos)
9294
{

storage/heap/hp_rprev.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int heap_rprev(HP_INFO *info, uchar *record)
4646
&info->last_pos, offsetof(TREE_ELEMENT, right));
4747
}
4848
}
49-
else if (info->last_pos)
49+
else if (info->last_pos && info->key_version == info->s->key_version)
5050
pos = tree_search_next(&keyinfo->rb_tree, &info->last_pos,
5151
offsetof(TREE_ELEMENT, right),
5252
offsetof(TREE_ELEMENT, left));
@@ -58,6 +58,7 @@ int heap_rprev(HP_INFO *info, uchar *record)
5858
info->last_find_flag= HA_READ_KEY_OR_PREV;
5959
pos = tree_search_key(&keyinfo->rb_tree, info->lastkey, info->parents,
6060
&info->last_pos, info->last_find_flag, &custom_arg);
61+
info->key_version= info->s->key_version;
6162
}
6263
if (pos)
6364
{

0 commit comments

Comments
 (0)