Skip to content

Commit ed089d2

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 a548756 commit ed089d2

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
@@ -446,15 +446,14 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
446446
/* allocate storage if needed */
447447
if (arrsize == 0 && *(void **) var == NULL)
448448
{
449-
void *mem = (void *) ecpg_alloc(offset * ntuples, lineno);
449+
void *mem = (void *) ecpg_auto_alloc(offset * ntuples, lineno);
450450

451451
if (!mem)
452452
{
453453
va_end(args);
454454
return false;
455455
}
456456
*(void **) var = mem;
457-
ecpg_add_mem(mem, lineno);
458457
var = mem;
459458
}
460459

@@ -524,15 +523,14 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
524523
/* allocate storage if needed */
525524
if (data_var.ind_arrsize == 0 && data_var.ind_value == NULL)
526525
{
527-
void *mem = (void *) ecpg_alloc(data_var.ind_offset * ntuples, lineno);
526+
void *mem = (void *) ecpg_auto_alloc(data_var.ind_offset * ntuples, lineno);
528527

529528
if (!mem)
530529
{
531530
va_end(args);
532531
return false;
533532
}
534533
*(void **) data_var.ind_pointer = mem;
535-
ecpg_add_mem(mem, lineno);
536534
data_var.ind_value = mem;
537535
}
538536

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
@@ -132,8 +132,7 @@ extern struct var_list *ivlist;
132132

133133
/* Here are some methods used by the lib. */
134134

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

138137
bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
139138
enum ECPGttype, char *, char *, long, long, long,
@@ -144,6 +143,7 @@ void ecpg_pthreads_init(void);
144143
#endif
145144
struct connection *ecpg_get_connection(const char *);
146145
char *ecpg_alloc(long, int);
146+
char *ecpg_auto_alloc(long, int);
147147
char *ecpg_realloc(void *, long, int);
148148
void ecpg_free(void *);
149149
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
@@ -104,14 +104,34 @@ static struct auto_mem *auto_allocs = NULL;
104104
#define set_auto_allocs(am) do { auto_allocs = (am); } while(0)
105105
#endif
106106

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

128+
if (!am)
129+
return false;
130+
112131
am->pointer = ptr;
113132
am->next = get_auto_allocs();
114133
set_auto_allocs(am);
134+
return true;
115135
}
116136

117137
void

0 commit comments

Comments
 (0)