From 6f4b01f763b23e7c5da2e73f90e6968bb5e315f3 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Thu, 3 Dec 2026 00:29:33 +0900 Subject: [PATCH v1] ecpg: Fix crashes on FETCH and MOVE of undeclared cursors Previously, ecpg crashed after reporting "cursor does not exist" when processing FETCH or MOVE for an undeclared cursor. The cause was a missing NULL check: add_additional_variables() returned NULL after reporting the error, but the caller dereferenced that pointer to access the cursor's connection. Fix this by checking that the pointer returned by add_additional_variables() is not NULL before dereferencing it. Backpatch to all supported versions. #Bug: 19967 Reported-by: Aman Ganapathy Author: Fujii Masao Discussion: https://postgr.es/m/19967-20b057703e28f235@postgresql.org Backpatch-through: 15 --- src/interfaces/ecpg/preproc/ecpg.addons | 42 ++++++++++++++++--------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/interfaces/ecpg/preproc/ecpg.addons b/src/interfaces/ecpg/preproc/ecpg.addons index b732f1a3551..69941700563 100644 --- a/src/interfaces/ecpg/preproc/ecpg.addons +++ b/src/interfaces/ecpg/preproc/ecpg.addons @@ -249,13 +249,15 @@ ECPG: addon var_value NumericOnly ECPG: addon fetch_args cursor_name struct cursor *ptr = add_additional_variables(@1, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); if (@1[0] == ':') @$ = "$0"; ECPG: addon fetch_args from_in cursor_name struct cursor *ptr = add_additional_variables(@2, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); if (@2[0] == ':') @$ = cat2_str(@1, "$0"); ECPG: addon fetch_args NEXT opt_from_in cursor_name @@ -265,14 +267,16 @@ ECPG: addon fetch_args LAST_P opt_from_in cursor_name ECPG: addon fetch_args ALL opt_from_in cursor_name struct cursor *ptr = add_additional_variables(@3, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); if (@3[0] == ':') @$ = cat_str(3, @1, @2, "$0"); ECPG: addon fetch_args SignedIconst opt_from_in cursor_name struct cursor *ptr = add_additional_variables(@3, false); bool replace = false; - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); if (@3[0] == ':') { @3 = "$0"; @@ -289,7 +293,8 @@ ECPG: addon fetch_args FORWARD ALL opt_from_in cursor_name ECPG: addon fetch_args BACKWARD ALL opt_from_in cursor_name struct cursor *ptr = add_additional_variables(@4, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); if (@4[0] == ':') @$ = cat_str(4, @1, @2, @3, "$0"); ECPG: addon fetch_args ABSOLUTE_P SignedIconst opt_from_in cursor_name @@ -299,7 +304,8 @@ ECPG: addon fetch_args BACKWARD SignedIconst opt_from_in cursor_name struct cursor *ptr = add_additional_variables(@4, false); bool replace = false; - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); if (@4[0] == ':') { @4 = "$0"; @@ -478,7 +484,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @3[0] == ':' ? "$0" : @3; struct cursor *ptr = add_additional_variables(@3, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "fetch forward", cursor_marker); } @@ -487,7 +494,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @4[0] == ':' ? "$0" : @4; struct cursor *ptr = add_additional_variables(@4, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "fetch forward from", cursor_marker); } @@ -496,7 +504,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @3[0] == ':' ? "$0" : @3; struct cursor *ptr = add_additional_variables(@3, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "fetch backward", cursor_marker); } @@ -505,7 +514,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @4[0] == ':' ? "$0" : @4; struct cursor *ptr = add_additional_variables(@4, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "fetch backward from", cursor_marker); } @@ -514,7 +524,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @3[0] == ':' ? "$0" : @3; struct cursor *ptr = add_additional_variables(@3, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "move forward", cursor_marker); } @@ -523,7 +534,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @4[0] == ':' ? "$0" : @4; struct cursor *ptr = add_additional_variables(@4, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "move forward from", cursor_marker); } @@ -532,7 +544,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @3[0] == ':' ? "$0" : @3; struct cursor *ptr = add_additional_variables(@3, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "move backward", cursor_marker); } @@ -541,7 +554,8 @@ ECPG: rule FetchStmt MOVE fetch_args const char *cursor_marker = @4[0] == ':' ? "$0" : @4; struct cursor *ptr = add_additional_variables(@4, false); - update_connection(ptr->connection); + if (ptr != NULL) + update_connection(ptr->connection); @$ = cat_str(2, "move backward from", cursor_marker); } -- 2.56.1