From 42d32ba95fa4eea4631b746ee6af81cec48da2ae Mon Sep 17 00:00:00 2001 From: Georg Richter Date: Wed, 30 Sep 2020 16:19:31 +0200 Subject: [PATCH] Fix for mariadb_stmt_execute_direct(): - If prepare failed, we need to ignore errormessage from mysql_stmt_execute() (which is always an error packet, mentioning invalid sttmt_id) - Added additional check in mysql_stmt_reset for stmt_id == -1. --- libmariadb/mariadb_stmt.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libmariadb/mariadb_stmt.c b/libmariadb/mariadb_stmt.c index 8f5bddcf..f72c0a56 100644 --- a/libmariadb/mariadb_stmt.c +++ b/libmariadb/mariadb_stmt.c @@ -2228,7 +2228,10 @@ MYSQL_RES * STDCALL mysql_stmt_result_metadata(MYSQL_STMT *stmt) my_bool STDCALL mysql_stmt_reset(MYSQL_STMT *stmt) { - return mysql_stmt_internal_reset(stmt, 0); + if (stmt->stmt_id > 0 && + stmt->stmt_id != (unsigned long) -1) + return mysql_stmt_internal_reset(stmt, 0); + return 0; } const char * STDCALL mysql_stmt_sqlstate(MYSQL_STMT *stmt) @@ -2381,6 +2384,7 @@ int STDCALL mariadb_stmt_execute_direct(MYSQL_STMT *stmt, { MYSQL *mysql; my_bool emulate_cmd; + my_bool clear_result= 0; if (!stmt) return 1; @@ -2448,6 +2452,10 @@ int STDCALL mariadb_stmt_execute_direct(MYSQL_STMT *stmt, if (mysql->methods->db_command(mysql, COM_STMT_PREPARE, stmt_str, length, 1, stmt)) goto fail; + /* in case prepare fails, we need to clear the result package from execute, which + is always an error packet (invalid statement id) */ + clear_result= 1; + stmt->state= MYSQL_STMT_PREPARED; /* Since we can't determine stmt_id here, we need to set it to -1, so server will know that the * execute command belongs to previous prepare */ @@ -2464,6 +2472,8 @@ int STDCALL mariadb_stmt_execute_direct(MYSQL_STMT *stmt, mysql->methods->db_read_prepare_response(stmt)) goto fail; + clear_result= 0; + /* metadata not supported yet */ if (stmt->param_count && @@ -2498,9 +2508,11 @@ fail: /* check if we need to set error message */ if (!mysql_stmt_errno(stmt)) UPDATE_STMT_ERROR(stmt); - do { - stmt->mysql->methods->db_stmt_flush_unbuffered(stmt); - } while(mysql_stmt_more_results(stmt)); + if (clear_result) { + do { + stmt->mysql->methods->db_stmt_flush_unbuffered(stmt); + } while(mysql_stmt_more_results(stmt)); + } stmt->state= MYSQL_STMT_INITTED; return 1; }