pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #17514: Application with embedded SQL crashes when executing EXEC SQL PREPARE
4+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2022-06-14 ba412c905a 9a3aab0f2a f7797747fc 86258f083a 12b8fb34a9 7bc21ed8c8  Avoid ecpglib core dump with out-of-order operations.
* BUG #17514: Application with embedded SQL crashes when executing EXEC SQL PREPARE
@ 2022-06-09 06:40  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 4+ messages in thread

From: PG Bug reporting form @ 2022-06-09 06:40 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: okano.naoki@jp.fujitsu.com

The following bug has been logged on the website:

Bug reference:      17514
Logged by:          Okano Naoki
Email address:      okano.naoki@jp.fujitsu.com
PostgreSQL version: 12.9
Operating system:   RHEL 7
Description:        

Hi, 

Application with embedded SQL crashes with segmentation fault if I execute
EXEC SQL PREPARE
command before executing EXEC SQL CONNECT command (*1).
I know that it is the correct operation to execute EXEC SQL CONNECT command
first 
and then execute EXEC SQL PREPARE command.
But I expect this wrong operation to result in an error, not a crash.
Is it correct that this operation causes the application to crash? Or is it
a bug?

[Environment]
* OS version: RHEL 7
* PostgreSQL version: 12
  I checked that PostgreSQL 10 and 14 also occurred a same issue.

(*1) Here is an exapmle.
int
main(void)
{
  EXEC SQL BEGIN DECLARE SECTION;
    char *connection = "tcp:postgresql://localhost/postgres";
    char *user = "user_name";
  EXEC SQL END DECLARE SECTION;

  ECPGdebug(1, stderr);

  /* Skip a CONNECT command deliberately. */
  /* EXEC SQL CONNECT TO :connection USER :user; */

  EXEC SQL PREPARE stmt1 FROM "SELECT * FROM pg_database WHERE oid = ?";
  return (0);
}


The following is a backtrace of the coredump.
(gdb) bt
#0  0x00007f9b4d4da150 in parseInput () from
/opt/rh/rh-postgresql12/root/usr//lib64/libpq.so.rh-postgresql12-5
#1  0x00007f9b4d4dc60d in PQgetResult () from
/opt/rh/rh-postgresql12/root/usr//lib64/libpq.so.rh-postgresql12-5
#2  0x00007f9b4d4dc8cf in PQexecStart () from
/opt/rh/rh-postgresql12/root/usr//lib64/libpq.so.rh-postgresql12-5
#3  0x00007f9b4d4dcb6c in PQprepare () from
/opt/rh/rh-postgresql12/root/usr//lib64/libpq.so.rh-postgresql12-5
#4  0x00007f9b4dd01a61 in prepare_common () from
/opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6
#5  0x00007f9b4dd01fb5 in ECPGprepare () from
/opt/rh/rh-postgresql12/root/usr//lib64/libecpg.so.rh-postgresql12-6
#6  0x0000000000400a79 in main ()

Thanks in advance.

Regards,
Naoki Okano



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #17514: Application with embedded SQL crashes when executing EXEC SQL PREPARE
@ 2022-06-09 17:01  Tom Lane <tgl@sss.pgh.pa.us>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2022-06-09 17:01 UTC (permalink / raw)
  To: okano.naoki@jp.fujitsu.com; +Cc: pgsql-bugs@lists.postgresql.org

PG Bug reporting form <noreply@postgresql.org> writes:
> Application with embedded SQL crashes with segmentation fault if I execute
> EXEC SQL PREPARE
> command before executing EXEC SQL CONNECT command (*1).
> I know that it is the correct operation to execute EXEC SQL CONNECT command
> first 
> and then execute EXEC SQL PREPARE command.
> But I expect this wrong operation to result in an error, not a crash.

We could do something like the attached, perhaps.  Having said that,
an application that includes no error checks is pretty unlikely to
fail gracefully :-(.

			regards, tom lane

Attachments:

ensure-TSD-key-is-valid-before-use.patch (text/x-diff, 837B) download diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c index 8dfcabe3b5..90f2ba9701 100644 --- a/src/interfaces/ecpg/ecpglib/connect.c +++ b/src/interfaces/ecpg/ecpglib/connect.c @@ -40,6 +40,8 @@ ecpg_get_connection_nr(const char *connection_name) if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) { #ifdef ENABLE_THREAD_SAFETY + ecpg_pthreads_init(); /* ensure actual_connection_key is valid */ + ret = pthread_getspecific(actual_connection_key); /* @@ -78,6 +78,8 @@ ecpg_get_connection(const char *connection_name) if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) { #ifdef ENABLE_THREAD_SAFETY + ecpg_pthreads_init(); /* ensure actual_connection_key is valid */ + ret = pthread_getspecific(actual_connection_key); /*
^ permalink raw reply [nested|flat] 4+ messages in thread

* RE: BUG #17514: Application with embedded SQL crashes when executing EXEC SQL PREPARE
@ 2022-06-10 05:09  okano.naoki@fujitsu.com <okano.naoki@fujitsu.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: okano.naoki@fujitsu.com @ 2022-06-10 05:09 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

Tom Lane wrote:
> We could do something like the attached, perhaps.  Having said that,
> an application that includes no error checks is pretty unlikely to
> fail gracefully :-(.

Thank you for replying and creating the patch!

I applied this patch to the master branch, and confirmed that the 
application resulted in an error instead of a crash.
As with PREPARE, executing DISCONNECT CURRENT also caused the 
application to crash, but this patch has fixed that.
The patch looks good to me.
I agree that applications should consider EXEC SQL PREPARE 
returning errors.

Regards,
Naoki Okano





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #17514: Application with embedded SQL crashes when executing EXEC SQL PREPARE
@ 2022-06-14 22:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: okano.naoki@fujitsu.com <okano.naoki@fujitsu.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2022-06-14 22:18 UTC (permalink / raw)
  To: okano.naoki@fujitsu.com <okano.naoki@fujitsu.com>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

"okano.naoki@fujitsu.com" <okano.naoki@fujitsu.com> writes:
> I applied this patch to the master branch, and confirmed that the 
> application resulted in an error instead of a crash.
> As with PREPARE, executing DISCONNECT CURRENT also caused the 
> application to crash, but this patch has fixed that.
> The patch looks good to me.

Pushed then, thanks for testing!

(Note that this will appear in the August releases, not in this
week's 14.4 release.)

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2022-06-14 22:18 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-06-09 06:40 BUG #17514: Application with embedded SQL crashes when executing EXEC SQL PREPARE PG Bug reporting form <noreply@postgresql.org>
2022-06-09 17:01 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-06-10 05:09   ` okano.naoki@fujitsu.com <okano.naoki@fujitsu.com>
2022-06-14 22:18     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox