]> granicus.if.org Git - postgresql/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:21:03 +0000 (14:21 +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 c3cd94682dd5c773329077b2c9d4a382b0273c1c..8d36484f73aa280d07a822126bf14715220ee00d 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 956c035be7b5ad193d45d348b805b85e5cdda8e9..15fd7a08a53108102a45fc2dc9dd50a54e96fa12 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);
@@ -701,6 +715,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)
        {
@@ -740,6 +761,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 715bedba80f0da83854804cfe6438f7f4ac3535e..656b2c420ac983e4db265455925f099a6666ba02 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));
 
@@ -215,6 +222,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);
@@ -323,6 +337,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 bcb38d25f8770c4c53a0547583aa43a40f9cd20d..9a56a5cd4acaeb88e2e7c3c06376e6f40a82ad01 100644 (file)
@@ -1493,6 +1493,13 @@ ecpg_process_output(struct statement * stmt, bool clear_result)
                                ntuples,
                                act_field;
 
+       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(stmt->results))
        {
index 2e622607477732c5c86f2364bc77f709a57e4ad0..a13aa0a9da820838e43a3909d047153f97975bcb 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);