]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Check for out of memory when allocating sqlca.
authorMichael Meskes <meskes@postgresql.org>
Mon, 15 Jun 2015 12:21:03 +0000 (14:21 +0200)
committerMichael Meskes <meskes@postgresql.org>
Mon, 15 Jun 2015 12:22:54 +0000 (14:22 +0200)
Patch by Michael Paquier

src/interfaces/ecpg/compatlib/informix.c
src/interfaces/ecpg/ecpglib/connect.c
src/interfaces/ecpg/ecpglib/data.c
src/interfaces/ecpg/ecpglib/descriptor.c
src/interfaces/ecpg/ecpglib/error.c
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/ecpg/ecpglib/misc.c

index 8d81c83deddd120256ac3539bff1c3440aadb96d..9f7776ee91958e2c9f0c3dae9632b335e97f1514 100644 (file)
@@ -1032,6 +1032,8 @@ void
 ECPG_informix_reset_sqlca(void)
 {
        struct sqlca_t *sqlca = ECPGget_sqlca();
+       if (sqlca == NULL)
+               return;
 
        memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
 }
index e45d17fcc576ae500b8df197f29d523a2b542b97..c90f13dc6c1588aa7c91b67f11cff05967745b61 100644 (file)
@@ -223,6 +223,12 @@ ECPGnoticeReceiver(void *arg, const PGresult *result)
        struct sqlca_t *sqlca = ECPGget_sqlca();
        int                     sqlcode;
 
+       if (sqlca == NULL)
+       {
+               ecpg_log("out of memory");
+               return;
+       }
+
        (void) arg;                                     /* keep the compiler quiet */
        if (sqlstate == NULL)
                sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
@@ -278,6 +284,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
        const char **conn_keywords;
        const char **conn_values;
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               ecpg_free(dbname);
+               return false;
+       }
+
        ecpg_init_sqlca(sqlca);
 
        /*
@@ -657,6 +671,13 @@ ECPGdisconnect(int lineno, const char *connection_name)
        struct sqlca_t *sqlca = ECPGget_sqlca();
        struct connection *con;
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return (false);
+       }
+
 #ifdef ENABLE_THREAD_SAFETY
        pthread_mutex_lock(&connections_mutex);
 #endif
index f2dbf6687a795c9d533b61c28156e8d89c684b44..bee7a8dd03cf15469e315ee5381db65df1e325e1 100644 (file)
@@ -132,6 +132,13 @@ ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno,
        int                     value_for_indicator = 0;
        long            log_offset;
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return (false);
+       }
+
        /*
         * If we are running in a regression test, do not log the offset variable,
         * it depends on the machine's alignment.
index b2990cab289d19b0c50a19c7fbc4ca946212f22e..ff011bd81654d350ca67708497ad842ae4432d17 100644 (file)
@@ -93,6 +93,13 @@ ECPGget_desc_header(int lineno, const char *desc_name, int *count)
        PGresult   *ECPGresult;
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return false;
+       }
+
        ecpg_init_sqlca(sqlca);
        ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
        if (!ECPGresult)
@@ -245,6 +252,13 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
        struct variable data_var;
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return false;
+       }
+
        va_start(args, index);
        ecpg_init_sqlca(sqlca);
        ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
@@ -703,6 +717,13 @@ ECPGdeallocate_desc(int line, const char *name)
        struct descriptor *prev;
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(line, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return false;
+       }
+
        ecpg_init_sqlca(sqlca);
        for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
        {
@@ -742,6 +763,13 @@ ECPGallocate_desc(int line, const char *name)
        struct descriptor *new;
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(line, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return false;
+       }
+
        ecpg_init_sqlca(sqlca);
        new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
        if (!new)
index ee553fdca32cea07a0c3644950e2325ef12e3e97..0c60b6ad4c069f1b7916151b54fb485d5ab54f33 100644 (file)
@@ -14,6 +14,13 @@ ecpg_raise(int line, int code, const char *sqlstate, const char *str)
 {
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_log("out of memory");
+               ECPGfree_auto_mem();
+               return;
+       }
+
        sqlca->sqlcode = code;
        strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
 
@@ -293,6 +300,13 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
        char       *sqlstate;
        char       *message;
 
+       if (sqlca == NULL)
+       {
+               ecpg_log("out of memory");
+               ECPGfree_auto_mem();
+               return;
+       }
+
        if (result)
        {
                sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
@@ -401,6 +415,12 @@ sqlprint(void)
 {
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_log("out of memory");
+               return;
+       }
+
        sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
        fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
 }
index 9854a57ed3e9ed318d3c84b5a203002a4760e5cd..1f62b693e3b076a6330e6febbe0ce673226919ba 100644 (file)
@@ -1440,6 +1440,13 @@ ecpg_execute(struct statement * stmt)
        if (!ecpg_check_PQresult(results, stmt->lineno, stmt->connection->connection, stmt->compat))
                return (false);
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
+                                  ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+               return (false);
+       }
+
        var = stmt->outlist;
        switch (PQresultStatus(results))
        {
index 9371f7140f446df1dc28cf1d63f630dc472a3fdf..69b688b8a955169edf4281720b2d883867310c24 100644 (file)
@@ -106,6 +106,13 @@ ecpg_init(const struct connection * con, const char *connection_name, const int
 {
        struct sqlca_t *sqlca = ECPGget_sqlca();
 
+       if (sqlca == NULL)
+       {
+               ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY,
+                                  NULL);
+               return (false);
+       }
+
        ecpg_init_sqlca(sqlca);
        if (con == NULL)
        {
@@ -143,6 +150,8 @@ ECPGget_sqlca(void)
        if (sqlca == NULL)
        {
                sqlca = malloc(sizeof(struct sqlca_t));
+               if (sqlca == NULL)
+                       return NULL;
                ecpg_init_sqlca(sqlca);
                pthread_setspecific(sqlca_key, sqlca);
        }
@@ -286,9 +295,11 @@ ecpg_log(const char *format,...)
        va_end(ap);
 
        /* dump out internal sqlca variables */
-       if (ecpg_internal_regression_mode)
+       if (ecpg_internal_regression_mode && sqlca != NULL)
+       {
                fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
                                sqlca->sqlcode, sqlca->sqlstate);
+       }
 
        fflush(debugstream);
 
@@ -524,6 +535,13 @@ ECPGset_var(int number, void *pointer, int lineno)
        {
                struct sqlca_t *sqlca = ECPGget_sqlca();
 
+               if (sqlca == NULL)
+               {
+                       ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+                                          ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+                       return;
+               }
+
                sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
                strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
                snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);