commit e7913101ec11db17ac2be7d997edc9b7610f0f1c
Author: kuroda.hayato%40jp.fujitsu.com <kuroda.hayato@jp.fujitsu.com>
Date:   Tue Aug 10 07:31:25 2021 +0000

    move free() to toplevel

diff --git a/src/interfaces/ecpg/preproc/ecpg.addons b/src/interfaces/ecpg/preproc/ecpg.addons
index b6e3412cef..1d38660fd7 100644
--- a/src/interfaces/ecpg/preproc/ecpg.addons
+++ b/src/interfaces/ecpg/preproc/ecpg.addons
@@ -397,7 +397,7 @@ ECPG: DeclareCursorStmtDECLAREcursor_namecursor_optionsCURSORopt_holdFORSelectSt
 		this->next = cur;
 		this->name = $2;
 		this->function = (current_function ? mm_strdup(current_function) : NULL);
-		this->connection = connection;
+		this->connection = connection ? mm_strdup(connection) : NULL;
 		this->opened = false;
 		this->command =  cat_str(7, mm_strdup("declare"), cursor_marker, $3, mm_strdup("cursor"), $5, mm_strdup("for"), $7);
 		this->argsinsert = argsinsert;
diff --git a/src/interfaces/ecpg/preproc/ecpg.trailer b/src/interfaces/ecpg/preproc/ecpg.trailer
index 96c55349e8..fd82aad7bc 100644
--- a/src/interfaces/ecpg/preproc/ecpg.trailer
+++ b/src/interfaces/ecpg/preproc/ecpg.trailer
@@ -4,8 +4,18 @@ statements: /*EMPTY*/
 				| statements statement
 		;
 
-statement: ecpgstart at toplevel_stmt ';' { connection = NULL; }
+statement: ecpgstart at toplevel_stmt ';'
+				{
+					if (connection)
+						free(connection);
+					connection = NULL;
+				}
 				| ecpgstart toplevel_stmt ';'
+				{
+					if (connection)
+						free(connection);
+					connection = NULL;
+				}
 				| ecpgstart ECPGVarDeclaration
 				{
 					fprintf(base_yyout, "%s", $2);
diff --git a/src/interfaces/ecpg/preproc/output.c b/src/interfaces/ecpg/preproc/output.c
index 65d06d5794..cf8aadd0b1 100644
--- a/src/interfaces/ecpg/preproc/output.c
+++ b/src/interfaces/ecpg/preproc/output.c
@@ -164,9 +164,6 @@ output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
 
 	whenever_action(whenever_mode | 2);
 	free(stmt);
-	if (connection != NULL)
-		free(connection);
-	connection = NULL;
 }
 
 void
@@ -179,9 +176,6 @@ output_prepare_statement(char *name, char *stmt)
 	fputs(");", base_yyout);
 	whenever_action(2);
 	free(name);
-	if (connection != NULL)
-		free(connection);
-	connection = NULL;
 }
 
 void
@@ -200,9 +194,6 @@ output_deallocate_prepare_statement(char *name)
 
 	whenever_action(2);
 	free(name);
-	if (connection != NULL)
-		free(connection);
-	connection = NULL;
 }
 
 static void
