This routine was calling ecpg_alloc to allocate to memory but did not
authorMichael Meskes <meskes@postgresql.org>
Thu, 5 Feb 2015 14:12:34 +0000 (15:12 +0100)
committerMichael Meskes <meskes@postgresql.org>
Thu, 5 Feb 2015 14:12:34 +0000 (15:12 +0100)
actually check the returned pointer allocated, potentially NULL which
could be the result of a malloc call.

Issue noted by Coverity, fixed by Michael Paquier <michael@otacoo.com>

src/interfaces/ecpg/ecpglib/descriptor.c
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/ecpg/ecpglib/extern.h
src/interfaces/ecpg/ecpglib/memory.c

index b2990cab289d19b0c50a19c7fbc4ca946212f22e..956c035be7b5ad193d45d348b805b85e5cdda8e9 100644 (file)
@@ -432,7 +432,7 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
                                /* allocate storage if needed */
                                if (arrsize == 0 && *(void **) var == NULL)
                                {
-                                       void       *mem = (void *) ecpg_alloc(offset * ntuples, lineno);
+                                       void       *mem = (void *) ecpg_auto_alloc(offset * ntuples, lineno);
 
                                        if (!mem)
                                        {
@@ -440,7 +440,6 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
                                                return false;
                                        }
                                        *(void **) var = mem;
-                                       ecpg_add_mem(mem, lineno);
                                        var = mem;
                                }
 
@@ -510,7 +509,7 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
                /* allocate storage if needed */
                if (data_var.ind_arrsize == 0 && data_var.ind_value == NULL)
                {
-                       void       *mem = (void *) ecpg_alloc(data_var.ind_offset * ntuples, lineno);
+                       void       *mem = (void *) ecpg_auto_alloc(data_var.ind_offset * ntuples, lineno);
 
                        if (!mem)
                        {
@@ -518,7 +517,6 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
                                return false;
                        }
                        *(void **) data_var.ind_pointer = mem;
-                       ecpg_add_mem(mem, lineno);
                        data_var.ind_value = mem;
                }
 
index 5305735f9d1d8cd4426428fab1d68789994008f4..c2b96f7f16e587ef2128e6747351f37e0f0c4d02 100644 (file)
@@ -398,11 +398,10 @@ ecpg_store_result(const PGresult *results, int act_field,
                }
 
                ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
-               var->value = (char *) ecpg_alloc(len, stmt->lineno);
+               var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
                if (!var->value)
                        return false;
                *((char **) var->pointer) = var->value;
-               ecpg_add_mem(var->value, stmt->lineno);
        }
 
        /* allocate indicator variable if needed */
@@ -410,11 +409,10 @@ ecpg_store_result(const PGresult *results, int act_field,
        {
                int                     len = var->ind_offset * ntuples;
 
-               var->ind_value = (char *) ecpg_alloc(len, stmt->lineno);
+               var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
                if (!var->ind_value)
                        return false;
                *((char **) var->ind_pointer) = var->ind_value;
-               ecpg_add_mem(var->ind_value, stmt->lineno);
        }
 
        /* fill the variable with the tuple(s) */
index 38360072eda027d5ed66f7fb48881abfd16f169b..2b670e0d00426a012aef666deed66900e1df5929 100644 (file)
@@ -136,8 +136,7 @@ extern struct var_list *ivlist;
 
 /* Here are some methods used by the lib. */
 
-/* Returns a pointer to a string containing a simple type name. */
-void           ecpg_add_mem(void *ptr, int lineno);
+bool           ecpg_add_mem(void *ptr, int lineno);
 
 bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
                          enum ECPGttype, char *, char *, long, long, long,
@@ -148,6 +147,7 @@ void                ecpg_pthreads_init(void);
 #endif
 struct connection *ecpg_get_connection(const char *);
 char      *ecpg_alloc(long, int);
+char      *ecpg_auto_alloc(long, int);
 char      *ecpg_realloc(void *, long, int);
 void           ecpg_free(void *);
 bool           ecpg_init(const struct connection *, const char *, const int);
index a09cd26a542e4ce250f549b428ac74164d11593a..dffc3a76187ff28b298b07eace460b74e637146c 100644 (file)
@@ -104,14 +104,34 @@ static struct auto_mem *auto_allocs = NULL;
 #define set_auto_allocs(am)            do { auto_allocs = (am); } while(0)
 #endif
 
-void
+char *
+ecpg_auto_alloc(long size, int lineno)
+{
+       void    *ptr = (void *) ecpg_alloc(size, lineno);
+
+       if (!ptr)
+               return NULL;
+
+       if (!ecpg_add_mem(ptr, lineno))
+       {
+               ecpg_free(ptr);
+               return NULL;
+       }
+       return ptr;
+}
+
+bool
 ecpg_add_mem(void *ptr, int lineno)
 {
        struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
 
+       if (!am)
+               return false;
+
        am->pointer = ptr;
        am->next = get_auto_allocs();
        set_auto_allocs(am);
+       return true;
 }
 
 void