From aeb07a61cb846f691c0f57f6e81d45c2d68d3932 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 Discussion: https://postgr.es/m/19967-20b057703e28f235@postgresql.org Backpatch-through: 15 --- src/interfaces/ecpg/preproc/ecpg.addons | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/interfaces/ecpg/preproc/ecpg.addons b/src/interfaces/ecpg/preproc/ecpg.addons index 50601554763..be639a64fa3 100644 --- a/src/interfaces/ecpg/preproc/ecpg.addons +++ b/src/interfaces/ecpg/preproc/ecpg.addons @@ -259,7 +259,7 @@ ECPG: var_valueNumericOnly addon } ECPG: fetch_argscursor_name addon struct cursor *ptr = add_additional_variables($1, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); if ($1[0] == ':') @@ -269,7 +269,7 @@ ECPG: fetch_argscursor_name addon } ECPG: fetch_argsfrom_incursor_name addon struct cursor *ptr = add_additional_variables($2, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); if ($2[0] == ':') @@ -283,7 +283,7 @@ ECPG: fetch_argsFIRST_Popt_from_incursor_name addon ECPG: fetch_argsLAST_Popt_from_incursor_name addon ECPG: fetch_argsALLopt_from_incursor_name addon struct cursor *ptr = add_additional_variables($3, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); if ($3[0] == ':') @@ -293,7 +293,7 @@ ECPG: fetch_argsALLopt_from_incursor_name addon } ECPG: fetch_argsSignedIconstopt_from_incursor_name addon struct cursor *ptr = add_additional_variables($3, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); if ($3[0] == ':') @@ -309,7 +309,7 @@ ECPG: fetch_argsSignedIconstopt_from_incursor_name addon ECPG: fetch_argsFORWARDALLopt_from_incursor_name addon ECPG: fetch_argsBACKWARDALLopt_from_incursor_name addon struct cursor *ptr = add_additional_variables($4, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); if ($4[0] == ':') @@ -322,7 +322,7 @@ ECPG: fetch_argsRELATIVE_PSignedIconstopt_from_incursor_name addon ECPG: fetch_argsFORWARDSignedIconstopt_from_incursor_name addon ECPG: fetch_argsBACKWARDSignedIconstopt_from_incursor_name addon struct cursor *ptr = add_additional_variables($4, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); if ($4[0] == ':') @@ -507,7 +507,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $3[0] == ':' ? mm_strdup("$0") : $3; struct cursor *ptr = add_additional_variables($3, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("fetch forward"), cursor_marker); @@ -516,7 +516,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $4[0] == ':' ? mm_strdup("$0") : $4; struct cursor *ptr = add_additional_variables($4, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("fetch forward from"), cursor_marker); @@ -525,7 +525,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $3[0] == ':' ? mm_strdup("$0") : $3; struct cursor *ptr = add_additional_variables($3, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("fetch backward"), cursor_marker); @@ -534,7 +534,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $4[0] == ':' ? mm_strdup("$0") : $4; struct cursor *ptr = add_additional_variables($4, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("fetch backward from"), cursor_marker); @@ -543,7 +543,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $3[0] == ':' ? mm_strdup("$0") : $3; struct cursor *ptr = add_additional_variables($3, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("move forward"), cursor_marker); @@ -552,7 +552,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $4[0] == ':' ? mm_strdup("$0") : $4; struct cursor *ptr = add_additional_variables($4, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("move forward from"), cursor_marker); @@ -561,7 +561,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $3[0] == ':' ? mm_strdup("$0") : $3; struct cursor *ptr = add_additional_variables($3, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("move backward"), cursor_marker); @@ -570,7 +570,7 @@ ECPG: FetchStmtMOVEfetch_args rule { char *cursor_marker = $4[0] == ':' ? mm_strdup("$0") : $4; struct cursor *ptr = add_additional_variables($4, false); - if (ptr -> connection) + if (ptr != NULL && ptr -> connection) connection = mm_strdup(ptr -> connection); $$ = cat_str(2, mm_strdup("move backward from"), cursor_marker); -- 2.56.1