Fix error in ECPG while connection handling

Started by Jeevan Ladhealmost 8 years ago3 messages
#1Jeevan Ladhe
jeevan.ladhe@enterprisedb.com
2 attachment(s)

Hi,

I came across following error while working on ecpg client program.

$ install/bin/ecpg ecpg_connection_ptr.pgc
ecpg_connection_ptr.pgc:26: ERROR: AT option not allowed in WHENEVER
statement

I have attached simple ecpg program 'ecpg_connection_ptr_issue.pgc' that
reproduces the above issue.

After doing some investigation I could see that, in preproc.y, in rule of
'ClosePortalStmt', function output_statement() is called, which in turn
frees
the connection pointer. Here is the relevant trailing snippet from the
output_statement() function.

whenever_action(whenever_mode | 2);
free(stmt);
if (connection != NULL)
free(connection);
}

Now, when the ecpg parses following statement using rule 'ECPGWhenever':
EXEC SQL WHENEVER SQLERROR CONTINUE;
it checks if the connection is set, if it is then throws an error as below:

if (connection)
mmerror(PARSE_ERROR, ET_ERROR, "AT option not allowed in WHENEVER
statement");

Now, ideally the connection would have been null here, but, as the
'ClosePortalStmt'
rule freed the connection but did not set it to NULL, it still sees that
there
is a connection(which is actually having garbage in it) and throws an error.

I see similarly there are other places, which are freeing this global
connection
but not setting to NULL, and all those should be fixed. I have attached a
patch
ecpg_connection_ptr_issue_fix.patch to fix these places.

Regards,
Jeevan Ladhe

Attachments:

ecpg_connection_ptr_issue_fix.patchapplication/octet-stream; name=ecpg_connection_ptr_issue_fix.patchDownload
diff --git a/src/interfaces/ecpg/preproc/output.c b/src/interfaces/ecpg/preproc/output.c
index a55bf2b..0465857 100644
--- a/src/interfaces/ecpg/preproc/output.c
+++ b/src/interfaces/ecpg/preproc/output.c
@@ -158,6 +158,7 @@ output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
 	free(stmt);
 	if (connection != NULL)
 		free(connection);
+	connection = NULL;
 }
 
 void
@@ -172,6 +173,7 @@ output_prepare_statement(char *name, char *stmt)
 	free(name);
 	if (connection != NULL)
 		free(connection);
+	connection = NULL;
 }
 
 void
@@ -192,6 +194,7 @@ output_deallocate_prepare_statement(char *name)
 	free(name);
 	if (connection != NULL)
 		free(connection);
+	connection = NULL;
 }
 
 static void
ecpg_connection_ptr_issue.pgcapplication/octet-stream; name=ecpg_connection_ptr_issue.pgcDownload
#2Michael Meskes
meskes@postgresql.org
In reply to: Jeevan Ladhe (#1)
Re: Fix error in ECPG while connection handling

Now, ideally the connection would have been null here, but, as the
'ClosePortalStmt'
rule freed the connection but did not set it to NULL, it still sees
that there
is a connection(which is actually having garbage in it) and throws an
error.

Thanks for spotting and fixing. I will push the patch as soon as I'm
online again.

Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Meskes at (Debian|Postgresql) dot Org
Jabber: michael at xmpp dot meskes dot org
VfL Borussia! Força Barça! SF 49ers! Use Debian GNU/Linux, PostgreSQL

#3Jeevan Ladhe
jeevan.ladhe@enterprisedb.com
In reply to: Michael Meskes (#2)
Re: Fix error in ECPG while connection handling

Thanks for spotting and fixing. I will push the patch as soon as I'm
online again.

Thanks Michael for taking care of this.

Regards,
Jeevan Ladhe.