From f318b23204f62c34d66d7cb055bddae35f7147df Mon Sep 17 00:00:00 2001 From: Luqun Lou Date: Tue, 1 Jun 2021 19:38:39 -0700 Subject: [PATCH] fix memory during MYSQL_BIN_LOG::open_existing_binlog Summary: asandebug complain there are memory leaks during MYSQL_BIN_LOG open Direct leak of 50 byte(s) in 1 object(s) allocated from: #0 0x67460ef in malloc #1 0x93f0777 in my_raw_malloc(unsigned long, int) #2 0x93f064a in my_malloc(unsigned int, unsigned long, int) #3 0x93f0eb0 in my_strdup(unsigned int, char const*, int) #4 0x8af01a6 in MYSQL_BIN_LOG::open(unsigned int, char const*, char const*, unsigned int) #5 0x8af8064 in MYSQL_BIN_LOG::open_binlog(char const*, char const*, unsigned long, bool, bool, bool, Format_description_log_event*, unsigned int, RaftRotateInfo*, bool) #6 0x8b00c00 in MYSQL_BIN_LOG::new_file_impl(bool, Format_description_log_event*, RaftRotateInfo*) #7 0x8d65e47 in rotate_relay_log(Master_info*, bool, bool, bool, RaftRotateInfo*) #8 0x8d661c0 in rotate_relay_log_for_raft(RaftRotateInfo*) #9 0x8c7696a in process_raft_queue #10 0xa0fa1fd in pfs_spawn_thread(void*) #11 0x7f8c9a12b20b in start_thread release these memory before assign them Reviewed By: Pushapgl Differential Revision: D28819752 --- sql/binlog.cc | 6 ++++-- sql/log_event.cc | 20 +++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/sql/binlog.cc b/sql/binlog.cc index d35e2e36d929..89e9c37e1fb4 100644 --- a/sql/binlog.cc +++ b/sql/binlog.cc @@ -6439,8 +6439,8 @@ bool MYSQL_BIN_LOG::open_existing_binlog(const char *log_name, DBUG_RETURN(1); } - if (!(this->name = - my_strdup(key_memory_MYSQL_LOG_name, log_name, MYF(MY_WME)))) { + my_free(name); + if (!(name = my_strdup(key_memory_MYSQL_LOG_name, log_name, MYF(MY_WME)))) { // NO_LINT_DEBUG sql_print_error("Could not allocate name %s (error %d)", log_name, errno); DBUG_RETURN(1); @@ -6450,6 +6450,8 @@ bool MYSQL_BIN_LOG::open_existing_binlog(const char *log_name, Binlog_ofile::open_existing(m_key_file_log, existing_file, MYF(MY_WME)); if (!binlog_file) goto err; + // release current point before assign + delete m_binlog_file; m_binlog_file = binlog_file.release(); file = mysql_file_open(m_key_file_log, existing_file, O_CREAT | O_WRONLY, diff --git a/sql/log_event.cc b/sql/log_event.cc index 906694a6f20e..f1a3632e36df 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -4676,17 +4676,15 @@ int Query_log_event::do_apply_event(Relay_log_info const *rli) { } int Query_log_event::do_apply_event_worker(Slave_worker *w) { - if (ends_group()) { - // Note: We're using event's future_event_relay_log_pos instead of - // rli->get_event_relay_log_pos() because rli is only updated in - // do_update_pos() which is called after applying the event and we might - // need to use this pos during application (e.g. during commit) - Slave_job_group *ptr_g = w->c_rli->gaq->get_job_group(mts_group_idx); - thd->set_trans_relay_log_pos(ptr_g->group_relay_log_name - ? ptr_g->group_relay_log_name - : w->get_group_relay_log_name(), - future_event_relay_log_pos); - } + // Note: We're using event's future_event_relay_log_pos instead of + // rli->get_event_relay_log_pos() because rli is only updated in + // do_update_pos() which is called after applying the event and we might + // need to use this pos during application (e.g. during commit) + Slave_job_group *ptr_g = w->c_rli->gaq->get_job_group(mts_group_idx); + thd->set_trans_relay_log_pos(ptr_g && ptr_g->group_relay_log_name + ? ptr_g->group_relay_log_name + : w->get_group_relay_log_name(), + future_event_relay_log_pos); return do_apply_event(w, query, q_len); }