From 95dbb0b43c3bec967e0f21ef2866ef5a7c379d33 Mon Sep 17 00:00:00 2001 From: Tanzeel Khan <140405735+tanscorpio7@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:24:43 +0530 Subject: [PATCH] fix valgrind warning in pltsql_store_view_definition (#3019) Reset static variable original_query_string back to NULL after use otherwise next queries which do not set original_query_string in ANLTR will read already freed memory. --------- Signed-off-by: Tanzeel Khan --- contrib/babelfishpg_tsql/src/pl_exec.c | 36 +++++++++----------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/contrib/babelfishpg_tsql/src/pl_exec.c b/contrib/babelfishpg_tsql/src/pl_exec.c index d0a0673112..f541701c6d 100644 --- a/contrib/babelfishpg_tsql/src/pl_exec.c +++ b/contrib/babelfishpg_tsql/src/pl_exec.c @@ -4633,8 +4633,7 @@ exec_stmt_execsql(PLtsql_execstate *estate, (estate->func->fn_is_trigger == PLTSQL_NOT_TRIGGER) && (strcmp(estate->func->fn_signature, "inline_code_block") != 0); - if (stmt->original_query) - original_query_string = stmt->original_query; + original_query_string = stmt->original_query ? stmt->original_query : NULL; if (stmt->is_cross_db) { @@ -4668,15 +4667,15 @@ exec_stmt_execsql(PLtsql_execstate *estate, { int ret = exec_stmt_insert_execute_select(estate, expr); - if (stmt->is_cross_db) - { - if (stmt->schema_name != NULL && (strcmp(stmt->schema_name, "sys") == 0 || strcmp(stmt->schema_name, "information_schema") == 0)) - set_session_properties(cur_dbname); - } if (reset_session_properties) { set_session_properties(cur_dbname); } + else if (stmt->is_cross_db) + { + if (stmt->schema_name != NULL && (strcmp(stmt->schema_name, "sys") == 0 || strcmp(stmt->schema_name, "information_schema") == 0)) + set_session_properties(cur_dbname); + } return ret; } @@ -5070,8 +5069,10 @@ exec_stmt_execsql(PLtsql_execstate *estate, restore_session_properties(); } } - PG_CATCH(); + PG_FINALLY(); { + original_query_string = NULL; + if (need_path_reset) (void) set_config_option("search_path", old_search_path, PGC_USERSET, PGC_S_SESSION, @@ -5080,28 +5081,15 @@ exec_stmt_execsql(PLtsql_execstate *estate, { set_session_properties(cur_dbname); } - if (stmt->is_cross_db) + else if (stmt->is_cross_db) { if (stmt->schema_name != NULL && (strcmp(stmt->schema_name, "sys") == 0 || strcmp(stmt->schema_name, "information_schema") == 0)) set_session_properties(cur_dbname); } - PG_RE_THROW(); - } - PG_END_TRY(); - if (need_path_reset) - (void) set_config_option("search_path", old_search_path, - PGC_USERSET, PGC_S_SESSION, - GUC_ACTION_SAVE, true, 0, false); - if (reset_session_properties) - { - set_session_properties(cur_dbname); - } - if (stmt->is_cross_db) - { - if (stmt->schema_name != NULL && (strcmp(stmt->schema_name, "sys") == 0 || strcmp(stmt->schema_name, "information_schema") == 0)) - set_session_properties(cur_dbname); + pfree(cur_dbname); } + PG_END_TRY(); return PLTSQL_RC_OK; }