Skip to content

Commit fe8047c

Browse files
committed
MDEV-37320 ASAN errors in Field::is_null / Item_param::assign_default
don't construct a "default value field" by moving field's ptr/null_ptr. Field can have its null_ptr moved to extra_null_bitmap for BEFORE triggers. Perhaps there can be other reasons for null_ptr and ptr not to be at the right offset to each other. Instead, use pointers from TABLE_SHARE::field, which always point to default values. Except when there's no TABLE_SHARE::field, which can happen for TEMPTABLE views, for example, but these views are not updatable anyway. Add an assert to Field::move_field_offset() to ensure it's only used for appropriately set ptr/null_ptr pairs.
1 parent 6334173 commit fe8047c

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

mysql-test/main/default.result

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,10 +3432,8 @@ DEFAULT(a) CASE a WHEN 0 THEN 1 ELSE 2 END
34323432
NULL 2
34333433
DROP TABLE t;
34343434
DROP VIEW v;
3435-
#
34363435
# End of 10.2 test
34373436
#
3438-
#
34393437
# MDEV-22703 DEFAULT() on a BLOB column can overwrite the default
34403438
# record, which can cause crashes when accessing already released
34413439
# memory.
@@ -3450,10 +3448,8 @@ length(DEFAULT(h))
34503448
25
34513449
INSERT INTO t1 () VALUES ();
34523450
drop table t1;
3453-
#
34543451
# End of 10.3 test
34553452
#
3456-
#
34573453
# MDEV-26423: MariaDB server crash in Create_tmp_table::finalize
34583454
#
34593455
CREATE TABLE t1 (pk text DEFAULT length(uuid()));
@@ -3483,6 +3479,14 @@ column_name column_default has_default is_nullable
34833479
a NULL 1 YES
34843480
drop view v1;
34853481
drop table t1;
3486-
#
34873482
# End of 10.4 test
34883483
#
3484+
# MDEV-37320 ASAN errors in Field::is_null / Item_param::assign_default
3485+
#
3486+
create table t1 (f01 timestamp, f03 timestamp);
3487+
insert into t1 () values ();
3488+
create trigger tr before insert on t1 for each row set @a=1;
3489+
prepare stmt from "update t1 set f03 = ?";
3490+
execute stmt using default;
3491+
drop table t1;
3492+
# End of 10.6 test

mysql-test/main/default.test

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,9 +2137,8 @@ CREATE ALGORITHM=TEMPTABLE VIEW v AS SELECT * FROM t;
21372137
SELECT DISTINCT DEFAULT(a), CASE a WHEN 0 THEN 1 ELSE 2 END FROM v GROUP BY a WITH ROLLUP;
21382138
DROP TABLE t;
21392139
DROP VIEW v;
2140-
--echo #
2140+
21412141
--echo # End of 10.2 test
2142-
--echo #
21432142

21442143
--echo #
21452144
--echo # MDEV-22703 DEFAULT() on a BLOB column can overwrite the default
@@ -2157,9 +2156,7 @@ SELECT length(DEFAULT(h)) FROM t1;
21572156
INSERT INTO t1 () VALUES ();
21582157
drop table t1;
21592158

2160-
--echo #
21612159
--echo # End of 10.3 test
2162-
--echo #
21632160

21642161
--echo #
21652162
--echo # MDEV-26423: MariaDB server crash in Create_tmp_table::finalize
@@ -2183,6 +2180,16 @@ select column_name, column_default, column_default is not null as 'has_default',
21832180
drop view v1;
21842181
drop table t1;
21852182

2186-
--echo #
21872183
--echo # End of 10.4 test
2184+
2185+
--echo #
2186+
--echo # MDEV-37320 ASAN errors in Field::is_null / Item_param::assign_default
21882187
--echo #
2188+
create table t1 (f01 timestamp, f03 timestamp);
2189+
insert into t1 () values ();
2190+
create trigger tr before insert on t1 for each row set @a=1;
2191+
prepare stmt from "update t1 set f03 = ?";
2192+
execute stmt using default;
2193+
drop table t1;
2194+
2195+
--echo # End of 10.6 test

sql/field.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,14 @@ class Field: public Value_source
15371537
{
15381538
ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
15391539
if (null_ptr)
1540+
{
15401541
null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
1542+
if (table)
1543+
{
1544+
DBUG_ASSERT(null_ptr < ptr);
1545+
DBUG_ASSERT(ptr - null_ptr <= (int)table->s->rec_buff_length);
1546+
}
1547+
}
15411548
}
15421549
void get_image(uchar *buff, uint length, CHARSET_INFO *cs) const
15431550
{ get_image(buff, length, ptr, cs); }

sql/item.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5231,10 +5231,17 @@ static Field *make_default_field(THD *thd, Field *field_arg)
52315231
def_field->default_value->expr->update_used_tables();
52325232
def_field->move_field(newptr + 1, def_field->maybe_null() ? newptr : 0, 1);
52335233
}
5234-
else
5234+
else if (field_arg->table && field_arg->table->s->field)
5235+
{
5236+
Field *def_val= field_arg->table->s->field[field_arg->field_index];
5237+
def_field->move_field(def_val->ptr, def_val->null_ptr, def_val->null_bit);
5238+
}
5239+
else /* e.g. non-updatable view */
5240+
{
52355241
def_field->move_field_offset((my_ptrdiff_t)
52365242
(def_field->table->s->default_values -
52375243
def_field->table->record[0]));
5244+
}
52385245
return def_field;
52395246
}
52405247

0 commit comments

Comments
 (0)