Skip to content

Commit d421595

Browse files
author
Michael Meskes
committed
This routine was calling ecpg_alloc to allocate to memory but did not
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>
1 parent 7e23b63 commit d421595

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/interfaces/ecpg/ecpglib/descriptor.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,11 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
410410
/* allocate storage if needed */
411411
if (arrsize == 0 && *(void **) var == NULL)
412412
{
413-
void *mem = (void *) ecpg_alloc(offset * ntuples, lineno);
413+
void *mem = (void *) ecpg_auto_alloc(offset * ntuples, lineno);
414414

415415
if (!mem)
416416
return false;
417417
*(void **) var = mem;
418-
ecpg_add_mem(mem, lineno);
419418
var = mem;
420419
}
421420

@@ -480,12 +479,11 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
480479
/* allocate storage if needed */
481480
if (data_var.ind_arrsize == 0 && data_var.ind_value == NULL)
482481
{
483-
void *mem = (void *) ecpg_alloc(data_var.ind_offset * ntuples, lineno);
482+
void *mem = (void *) ecpg_auto_alloc(data_var.ind_offset * ntuples, lineno);
484483

485484
if (!mem)
486485
return false;
487486
*(void **) data_var.ind_pointer = mem;
488-
ecpg_add_mem(mem, lineno);
489487
data_var.ind_value = mem;
490488
}
491489

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,23 +402,21 @@ ecpg_store_result(const PGresult *results, int act_field,
402402
}
403403

404404
ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
405-
var->value = (char *) ecpg_alloc(len, stmt->lineno);
405+
var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
406406
if (!var->value)
407407
return false;
408408
*((char **) var->pointer) = var->value;
409-
ecpg_add_mem(var->value, stmt->lineno);
410409
}
411410

412411
/* allocate indicator variable if needed */
413412
if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
414413
{
415414
int len = var->ind_offset * ntuples;
416415

417-
var->ind_value = (char *) ecpg_alloc(len, stmt->lineno);
416+
var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
418417
if (!var->ind_value)
419418
return false;
420419
*((char **) var->ind_pointer) = var->ind_value;
421-
ecpg_add_mem(var->ind_value, stmt->lineno);
422420
}
423421

424422
/* fill the variable with the tuple(s) */

src/interfaces/ecpg/ecpglib/extern.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ struct variable
124124

125125
/* Here are some methods used by the lib. */
126126

127-
/* Returns a pointer to a string containing a simple type name. */
128-
void ecpg_add_mem(void *ptr, int lineno);
127+
bool ecpg_add_mem(void *ptr, int lineno);
129128

130129
bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
131130
enum ECPGttype, char *, char *, long, long, long,
@@ -136,6 +135,7 @@ void ecpg_pthreads_init(void);
136135
#endif
137136
struct connection *ecpg_get_connection(const char *);
138137
char *ecpg_alloc(long, int);
138+
char *ecpg_auto_alloc(long, int);
139139
char *ecpg_realloc(void *, long, int);
140140
void ecpg_free(void *);
141141
bool ecpg_init(const struct connection *, const char *, const int);

src/interfaces/ecpg/ecpglib/memory.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,34 @@ static struct auto_mem *auto_allocs = NULL;
103103
#define set_auto_allocs(am) do { auto_allocs = (am); } while(0)
104104
#endif
105105

106-
void
106+
char *
107+
ecpg_auto_alloc(long size, int lineno)
108+
{
109+
void *ptr = (void *) ecpg_alloc(size, lineno);
110+
111+
if (!ptr)
112+
return NULL;
113+
114+
if (!ecpg_add_mem(ptr, lineno))
115+
{
116+
ecpg_free(ptr);
117+
return NULL;
118+
}
119+
return ptr;
120+
}
121+
122+
bool
107123
ecpg_add_mem(void *ptr, int lineno)
108124
{
109125
struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
110126

127+
if (!am)
128+
return false;
129+
111130
am->pointer = ptr;
112131
am->next = get_auto_allocs();
113132
set_auto_allocs(am);
133+
return true;
114134
}
115135

116136
void

0 commit comments

Comments
 (0)