MDEV-38716: Server crashes in TABLE::evaluate_update_default_function#4609
Merged
MDEV-38716: Server crashes in TABLE::evaluate_update_default_function#4609
Conversation
21d37d4 to
5d9df52
Compare
This patch fixes two issues: Problem (1) A regression from MDEV-36290 would cause the server to crash when one uses TIMESTAMP or DATETIME types with option ON UPDATE. The root cause of this is actually from problem (2) described below, MDEV-36290 simply forced the bug to be revealed. Problem (2) After a table is rebuilt (i.e. via ALTER TABLE .. FORCE), any fields which use ON UPDATE lose the update trigger after the ALTER. This is because recreated fields (i.e those in alter_info->create_list) had no checks to ensure their representation in the target table's default_fields list. The intent is provided by the following code comment: /* Update the set of auto-update fields to contain only the new fields added to the table. Only these fields should be updated automatically. Old fields keep their current values, and therefore should not be present in the set of autoupdate fields. */ However, fields with trigger-based default values were left unconsidered. Linking back to problem (1), the crash happened because of the following MDEV-36290 changes: diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 13ed810..5cc7478 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -12737,7 +12737,12 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to, } } if (dfield_ptr) - *dfield_ptr= NULL; + { + if (dfield_ptr == to->default_field) + to->default_field= 0; // No default fields left + else + *dfield_ptr= NULL; // Mark end of default field pointers + } That is, the destination table's default_field becomes nullified when no default_field changes exist. However, because ON UPDATE default_fields were not moved to the destination table, the server would crash with a segfault when updating the records in the post-ALTER table (in TABLE::evaluate_update_default_function()) because it would de-reference the nullified pointer in default_field. The fix for these problems is the same. ON UPDATE based default_fields are added back to the destination table after the actual data copy. Note this must be done after the data copy for fields which have both DEFAULT and ON UPDATE set for timestamps. Additionally, this patch removes the set/reset of table->s->default_fields from TABLE::evaluate_update_default_function() because it is no longer used when updating default fields. The functions that use default_field use the NULL terminator at the end of the list to stop iteration. This patch adds a test case to main.temp_table to test the case of a table with a field column with a NULL default value but ON UPDATE CURRENT_TIMESTAMP. Existing MTR tests have cases for DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP (i.e. any test which deals with upgrades, e.g. main.upgrade_mdev_24363 and main.mysql_upgrade_mysql_json_with_plugin_loaded). Reviewed-by: Monty <monty@mariadb.com> Signed-off-by: Brandon Nesterenko <brandon.nesterenko@mariadb.com>
5d9df52 to
fa36b26
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch fixes two issues:
Problem (1)
A regression from MDEV-36290 would cause the server to crash when one uses TIMESTAMP or DATETIME types with option ON UPDATE. The root cause of this is actually from problem (2) described below, MDEV-36290 simply forced the bug to be revealed.
Problem (2)
After a table is rebuilt (i.e. via ALTER TABLE .. FORCE), any fields which use ON UPDATE lose the update trigger after the ALTER. This is because recreated fields (i.e those in alter_info->create_list) had no checks to ensure their representation in the target table's default_fields list. The intent is provided by the following code comment:
However, fields with trigger-based default values were left unconsidered.
Linking back to problem (1), the crash happened because of the following MDEV-36290 changes:
That is, the destination table's default_field becomes nullified when no default_field changes exist. However, because ON UPDATE default_fields were not moved to the destination table, the server would crash with a segfault when updating the records in the post-ALTER table (in TABLE::evaluate_update_default_function()) because it would de-reference the nullified pointer in default_field.
The fix for these problems is the same. The default_field information is copied from these recreated fields when default values are used later by ON UPDATE options.
Note a number of tests currently fail from this change, which seem to rely on losing ON UPDATE options. I don't see that behavior documented anywhere, so it seems to me these tests rely on bugged behavior. But before fixing these tests, I'd like the general idea reviewed.