Commit 7bc21ed8 authored by Tom Lane's avatar Tom Lane

Avoid ecpglib core dump with out-of-order operations.

If an application executed operations like EXEC SQL PREPARE
without having first established a database connection, it could
get a core dump instead of the expected clean failure.  This
occurred because we did "pthread_getspecific(actual_connection_key)"
without ever having initialized the TSD key actual_connection_key.
The results of that are probably platform-specific, but at least
on Linux it often leads to a crash.

To fix, add calls to ecpg_pthreads_init() in the code paths that
might use actual_connection_key uninitialized.  It's harmless
(and hopefully inexpensive) to do that more than once.

Per bug #17514 from Okano Naoki.  The problem's ancient, so
back-patch to all supported branches.

Discussion: https://postgr.es/m/17514-edd4fad547c5692c@postgresql.org
parent be35a645
...@@ -40,6 +40,8 @@ ecpg_get_connection_nr(const char *connection_name) ...@@ -40,6 +40,8 @@ ecpg_get_connection_nr(const char *connection_name)
if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
{ {
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
ecpg_pthreads_init(); /* ensure actual_connection_key is valid */
ret = pthread_getspecific(actual_connection_key); ret = pthread_getspecific(actual_connection_key);
/* /*
...@@ -47,8 +49,7 @@ ecpg_get_connection_nr(const char *connection_name) ...@@ -47,8 +49,7 @@ ecpg_get_connection_nr(const char *connection_name)
* connection and hope the user knows what they're doing (i.e. using * connection and hope the user knows what they're doing (i.e. using
* their own mutex to protect that connection from concurrent accesses * their own mutex to protect that connection from concurrent accesses
*/ */
/* if !ret then we got the connection from TSD */ if (ret == NULL)
if (NULL == ret)
/* no TSD connection, going for global */ /* no TSD connection, going for global */
ret = actual_connection; ret = actual_connection;
#else #else
...@@ -78,6 +79,8 @@ ecpg_get_connection(const char *connection_name) ...@@ -78,6 +79,8 @@ ecpg_get_connection(const char *connection_name)
if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
{ {
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
ecpg_pthreads_init(); /* ensure actual_connection_key is valid */
ret = pthread_getspecific(actual_connection_key); ret = pthread_getspecific(actual_connection_key);
/* /*
...@@ -85,8 +88,7 @@ ecpg_get_connection(const char *connection_name) ...@@ -85,8 +88,7 @@ ecpg_get_connection(const char *connection_name)
* connection and hope the user knows what they're doing (i.e. using * connection and hope the user knows what they're doing (i.e. using
* their own mutex to protect that connection from concurrent accesses * their own mutex to protect that connection from concurrent accesses
*/ */
/* if !ret then we got the connection from TSD */ if (ret == NULL)
if (NULL == ret)
/* no TSD connection here either, using global */ /* no TSD connection here either, using global */
ret = actual_connection; ret = actual_connection;
#else #else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment