Hi,

While i'm working on a ecpg patch I found a bug in ecpg code. The simple program above could reproduce it. But basically it crashes (segfault) because it's trying to use a inexistent connection when we're preparing a statement. Don't know if it deserves a ecpg_log() message. A possible fix is attached.

#include <stdio.h>

exec sql whenever sqlerror sqlprint;

int main(void)
{
        exec sql begin declare section;
        char cmd[128];
        exec sql end declare section;

        exec sql connect to euler as main;
        sprintf(cmd, "select * from foo");
        exec sql prepare f from :cmd;
        exec sql disconnect;

        return 0;
}


(gdb) bt
#0 0xb7ebd9cd in find_prepared_statement (name=0x8048819 "f", con=0x0, prev_=0xbfccc6dc) at /a/pgsql/dev/pgsql/src/interfaces/ecpg/ecpglib/prepare.c:187 #1 0xb7ebd79f in ECPGprepare (lineno=15, connection_name=0x0, questionmarks=0, name=0x8048819 "f", variable=0xbfccc72c "select * from foo limit 1,2")
    at /a/pgsql/dev/pgsql/src/interfaces/ecpg/ecpglib/prepare.c:128
#2  0x0804865d in main ()


--
  Euler Taveira de Oliveira
  http://www.timbira.com/
Index: src/interfaces/ecpg/ecpglib/prepare.c
===================================================================
RCS file: /a/pgsql/dev/anoncvs/pgsql/src/interfaces/ecpg/ecpglib/prepare.c,v
retrieving revision 1.26
diff -c -r1.26 prepare.c
*** src/interfaces/ecpg/ecpglib/prepare.c	7 Feb 2008 11:09:13 -0000	1.26
--- src/interfaces/ecpg/ecpglib/prepare.c	10 May 2008 02:58:36 -0000
***************
*** 124,129 ****
--- 124,132 ----
  
  	con = ecpg_get_connection(connection_name);
  
+ 	if (!con)
+ 		return false;
+ 
  	/* check if we already have prepared this statement */
  	this = find_prepared_statement(name, con, &prev);
  	if (this && !deallocate_one(lineno, ECPG_COMPAT_PGSQL, con, prev, this))
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to