diff options
author | Michael Paquier <michael@paquier.xyz> | 2021-09-13 13:24:27 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2021-09-13 13:24:27 +0900 |
commit | b34dcf87f685ab6c7705d4dd504f23025bfc75b0 (patch) | |
tree | 8d4b3ab9ba4e034cec6a4faab6d226302cb864fb /src | |
parent | 3adde7eb6633bea734c4513ebd96adca78b7737c (diff) | |
download | postgresql-b34dcf87f685ab6c7705d4dd504f23025bfc75b0.tar.gz postgresql-b34dcf87f685ab6c7705d4dd504f23025bfc75b0.zip |
Fix error handling with threads on OOM in ECPG connection logic
An out-of-memory failure happening when allocating the structures to
store the connection parameter keywords and values would mess up with
the set of connections saved, as on failure the pthread mutex would
still be hold with the new connection object listed but free()'d.
Rather than just unlocking the mutex, which would leave the static list
of connections into an inconsistent state, move the allocation for the
structures of the connection parameters before beginning the test
manipulation. This ensures that the list of connections and the
connection mutex remain consistent all the time in this code path.
This error is unlikely going to happen, but this could mess up badly
with ECPG clients in surprising ways, so backpatch all the way down.
Reported-by: ryancaicse
Discussion: https://postgr.es/m/17186-b4cfd8f0eb4d1dee@postgresql.org
Backpatch-through: 9.6
Diffstat (limited to 'src')
-rw-r--r-- | src/interfaces/ecpg/ecpglib/connect.c | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c index 5f8455020e5..cecbc3a06c7 100644 --- a/src/interfaces/ecpg/ecpglib/connect.c +++ b/src/interfaces/ecpg/ecpglib/connect.c @@ -484,39 +484,12 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p else realname = NULL; - /* add connection to our list */ -#ifdef ENABLE_THREAD_SAFETY - pthread_mutex_lock(&connections_mutex); -#endif - if (connection_name != NULL) - this->name = ecpg_strdup(connection_name, lineno); - else - this->name = ecpg_strdup(realname, lineno); - - this->cache_head = NULL; - this->prep_stmts = NULL; - - if (all_connections == NULL) - this->next = NULL; - else - this->next = all_connections; - - all_connections = this; -#ifdef ENABLE_THREAD_SAFETY - pthread_setspecific(actual_connection_key, all_connections); -#endif - actual_connection = all_connections; - - ecpg_log("ECPGconnect: opening database %s on %s port %s %s%s %s%s\n", - realname ? realname : "<DEFAULT>", - host ? host : "<DEFAULT>", - port ? (ecpg_internal_regression_mode ? "<REGRESSION_PORT>" : port) : "<DEFAULT>", - options ? "with options " : "", options ? options : "", - (user && strlen(user) > 0) ? "for user " : "", user ? user : ""); - + /* + * Count options for the allocation done below (this may produce an + * overestimate, it's ok). + */ if (options) for (i = 0; options[i]; i++) - /* count options */ if (options[i] == '=') connect_params++; @@ -525,7 +498,11 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p if (passwd && strlen(passwd) > 0) connect_params++; - /* allocate enough space for all connection parameters */ + /* + * Allocate enough space for all connection parameters. These allocations + * are done before manipulating the list of connections to ease the error + * handling on failure. + */ conn_keywords = (const char **) ecpg_alloc((connect_params + 1) * sizeof(char *), lineno); conn_values = (const char **) ecpg_alloc(connect_params * sizeof(char *), lineno); if (conn_keywords == NULL || conn_values == NULL) @@ -548,6 +525,36 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p return false; } + /* add connection to our list */ +#ifdef ENABLE_THREAD_SAFETY + pthread_mutex_lock(&connections_mutex); +#endif + if (connection_name != NULL) + this->name = ecpg_strdup(connection_name, lineno); + else + this->name = ecpg_strdup(realname, lineno); + + this->cache_head = NULL; + this->prep_stmts = NULL; + + if (all_connections == NULL) + this->next = NULL; + else + this->next = all_connections; + + all_connections = this; +#ifdef ENABLE_THREAD_SAFETY + pthread_setspecific(actual_connection_key, all_connections); +#endif + actual_connection = all_connections; + + ecpg_log("ECPGconnect: opening database %s on %s port %s %s%s %s%s\n", + realname ? realname : "<DEFAULT>", + host ? host : "<DEFAULT>", + port ? (ecpg_internal_regression_mode ? "<REGRESSION_PORT>" : port) : "<DEFAULT>", + options ? "with options " : "", options ? options : "", + (user && strlen(user) > 0) ? "for user " : "", user ? user : ""); + i = 0; if (realname) { |