@@ -1452,7 +1452,7 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s
14521452 PLtsql_stmt_execsql * parse_result ;
14531453 PLtsql_function * func ;
14541454 char * stmt_copy ;
1455- const char * tsql_stmt = stmt ; /* Use this for potentially modified statement */
1455+ char * tsql_stmt = NULL ; /* Use this for potentially modified statement */
14561456
14571457 /*
14581458 * Connect to SPI manager. should be handled in the same way with
@@ -1493,7 +1493,7 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s
14931493 * and PLTSQL_STMT_RETURN being last. In case of empty statement only PLTSQL_STMT_RETURN node will be present.
14941494 * So total number of nodes should be 3 for valid cursor execution. Actual query statement will be at second
14951495 * position of type PLTSQL_STMT_EXECSQL.
1496-
1496+ *
14971497 * This is defensive code, where we only assign the tsql_stmt variable to parsed query,
14981498 * if the cmd_type is PLTSQL_STMT_EXECSQL. There might be other types of cmd_type like
14991499 * PLTSQL_STMT_EXECSQL (for procedures), for them we will keep the old behavior.
@@ -1513,14 +1513,14 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s
15131513 }
15141514
15151515 /* prepare plan and insert a cursor entry */
1516- plan = SPI_prepare_cursor (tsql_stmt , nBindParams , boundParamsOidList , cursor_options );
1516+ plan = SPI_prepare_cursor (tsql_stmt ? tsql_stmt : stmt , nBindParams , boundParamsOidList , cursor_options );
15171517 if (plan == NULL )
15181518 {
1519- /* Free memory if tsql_stmt is different from stmt */
1520- if (tsql_stmt != stmt )
1521- pfree ((void * )tsql_stmt );
15221519 return 1 ; /* procedure failed */
15231520 }
1521+ /* Free memory if tsql_stmt is not null */
1522+ if (tsql_stmt )
1523+ pfree ((void * )tsql_stmt );
15241524
15251525 if (save_plan )
15261526 {
@@ -1535,10 +1535,6 @@ execute_sp_cursoropen_common(int *stmt_handle, int *cursor_handle, const char *s
15351535
15361536 SPI_keepplan (plan );
15371537 }
1538-
1539- /* Free memory if tsql_stmt is different from stmt */
1540- if (tsql_stmt != stmt )
1541- pfree ((void * )tsql_stmt );
15421538 }
15431539 else /* !prepare */
15441540 {
0 commit comments