Skip to content

Commit 157d406

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 a35a527 commit 157d406

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
@@ -398,23 +398,21 @@ ecpg_store_result(const PGresult *results, int act_field,
398398
}
399399

400400
ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
401-
var->value = (char *) ecpg_alloc(len, stmt->lineno);
401+
var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
402402
if (!var->value)
403403
return false;
404404
*((char **) var->pointer) = var->value;
405-
ecpg_add_mem(var->value, stmt->lineno);
406405
}
407406

408407
/* allocate indicator variable if needed */
409408
if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
410409
{
411410
int len = var->ind_offset * ntuples;
412411

413-
var->ind_value = (char *) ecpg_alloc(len, stmt->lineno);
412+
var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
414413
if (!var->ind_value)
415414
return false;
416415
*((char **) var->ind_pointer) = var->ind_value;
417-
ecpg_add_mem(var->ind_value, stmt->lineno);
418416
}
419417

420418
/* 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
@@ -136,8 +136,7 @@ extern struct var_list *ivlist;
136136

137137
/* Here are some methods used by the lib. */
138138

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

142141
bool ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
143142
enum ECPGttype, char *, char *, long, long, long,
@@ -148,6 +147,7 @@ void ecpg_pthreads_init(void);
148147
#endif
149148
struct connection *ecpg_get_connection(const char *);
150149
char *ecpg_alloc(long, int);
150+
char *ecpg_auto_alloc(long, int);
151151
char *ecpg_realloc(void *, long, int);
152152
void ecpg_free(void *);
153153
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy