From de2fd24e776b2d290a86d7e560082e4c223cbf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Fri, 30 Jul 2021 13:33:24 +0200 Subject: [PATCH] Fix leak closing 'query.stmt' in case of 'async_free_result' called for 'ASYNC_STMT_PREPARE_FAILED' state #3525 --- lib/mysql_connection.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/mysql_connection.cpp b/lib/mysql_connection.cpp index 7589798e7..3a5b12659 100644 --- a/lib/mysql_connection.cpp +++ b/lib/mysql_connection.cpp @@ -2145,6 +2145,15 @@ void MySQL_Connection::async_free_result() { mysql_stmt_free_result(query.stmt); } } + // If we reached here from 'ASYNC_STMT_PREPARE_FAILED', the + // prepared statement was never added to 'local_stmts', thus + // it will never be freed when 'local_stmts' are purged. If + // initialized, it must be freed. For more context see #3525. + if (this->async_state_machine == ASYNC_STMT_PREPARE_FAILED) { + if (query.stmt != NULL) { + mysql_stmt_close(query.stmt); + } + } query.stmt=NULL; } if (mysql_result) {