Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4130b2c

Browse files
author
Michael Meskes
committed
Check for out of memory when allocating sqlca.
Patch by Michael Paquier
1 parent 3e2a17e commit 4130b2c

File tree

7 files changed

+104
-1
lines changed

7 files changed

+104
-1
lines changed

src/interfaces/ecpg/compatlib/informix.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,8 @@ void
10321032
ECPG_informix_reset_sqlca(void)
10331033
{
10341034
struct sqlca_t *sqlca = ECPGget_sqlca();
1035+
if (sqlca == NULL)
1036+
return;
10351037

10361038
memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
10371039
}

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ ECPGnoticeReceiver(void *arg, const PGresult *result)
223223
struct sqlca_t *sqlca = ECPGget_sqlca();
224224
int sqlcode;
225225

226+
if (sqlca == NULL)
227+
{
228+
ecpg_log("out of memory");
229+
return;
230+
}
231+
226232
(void) arg; /* keep the compiler quiet */
227233
if (sqlstate == NULL)
228234
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
@@ -278,6 +284,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
278284
const char **conn_keywords;
279285
const char **conn_values;
280286

287+
if (sqlca == NULL)
288+
{
289+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
290+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
291+
ecpg_free(dbname);
292+
return false;
293+
}
294+
281295
ecpg_init_sqlca(sqlca);
282296

283297
/*
@@ -657,6 +671,13 @@ ECPGdisconnect(int lineno, const char *connection_name)
657671
struct sqlca_t *sqlca = ECPGget_sqlca();
658672
struct connection *con;
659673

674+
if (sqlca == NULL)
675+
{
676+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
677+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
678+
return (false);
679+
}
680+
660681
#ifdef ENABLE_THREAD_SAFETY
661682
pthread_mutex_lock(&connections_mutex);
662683
#endif

src/interfaces/ecpg/ecpglib/data.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno,
132132
int value_for_indicator = 0;
133133
long log_offset;
134134

135+
if (sqlca == NULL)
136+
{
137+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
138+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
139+
return (false);
140+
}
141+
135142
/*
136143
* If we are running in a regression test, do not log the offset variable,
137144
* it depends on the machine's alignment.

src/interfaces/ecpg/ecpglib/descriptor.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ ECPGget_desc_header(int lineno, const char *desc_name, int *count)
9393
PGresult *ECPGresult;
9494
struct sqlca_t *sqlca = ECPGget_sqlca();
9595

96+
if (sqlca == NULL)
97+
{
98+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
99+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
100+
return false;
101+
}
102+
96103
ecpg_init_sqlca(sqlca);
97104
ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
98105
if (!ECPGresult)
@@ -245,6 +252,13 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
245252
struct variable data_var;
246253
struct sqlca_t *sqlca = ECPGget_sqlca();
247254

255+
if (sqlca == NULL)
256+
{
257+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
258+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
259+
return false;
260+
}
261+
248262
va_start(args, index);
249263
ecpg_init_sqlca(sqlca);
250264
ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
@@ -703,6 +717,13 @@ ECPGdeallocate_desc(int line, const char *name)
703717
struct descriptor *prev;
704718
struct sqlca_t *sqlca = ECPGget_sqlca();
705719

720+
if (sqlca == NULL)
721+
{
722+
ecpg_raise(line, ECPG_OUT_OF_MEMORY,
723+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
724+
return false;
725+
}
726+
706727
ecpg_init_sqlca(sqlca);
707728
for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
708729
{
@@ -742,6 +763,13 @@ ECPGallocate_desc(int line, const char *name)
742763
struct descriptor *new;
743764
struct sqlca_t *sqlca = ECPGget_sqlca();
744765

766+
if (sqlca == NULL)
767+
{
768+
ecpg_raise(line, ECPG_OUT_OF_MEMORY,
769+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
770+
return false;
771+
}
772+
745773
ecpg_init_sqlca(sqlca);
746774
new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
747775
if (!new)

src/interfaces/ecpg/ecpglib/error.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ ecpg_raise(int line, int code, const char *sqlstate, const char *str)
1414
{
1515
struct sqlca_t *sqlca = ECPGget_sqlca();
1616

17+
if (sqlca == NULL)
18+
{
19+
ecpg_log("out of memory");
20+
ECPGfree_auto_mem();
21+
return;
22+
}
23+
1724
sqlca->sqlcode = code;
1825
strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
1926

@@ -293,6 +300,13 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
293300
char *sqlstate;
294301
char *message;
295302

303+
if (sqlca == NULL)
304+
{
305+
ecpg_log("out of memory");
306+
ECPGfree_auto_mem();
307+
return;
308+
}
309+
296310
if (result)
297311
{
298312
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
@@ -401,6 +415,12 @@ sqlprint(void)
401415
{
402416
struct sqlca_t *sqlca = ECPGget_sqlca();
403417

418+
if (sqlca == NULL)
419+
{
420+
ecpg_log("out of memory");
421+
return;
422+
}
423+
404424
sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
405425
fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
406426
}

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,13 @@ ecpg_execute(struct statement * stmt)
14401440
if (!ecpg_check_PQresult(results, stmt->lineno, stmt->connection->connection, stmt->compat))
14411441
return (false);
14421442

1443+
if (sqlca == NULL)
1444+
{
1445+
ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
1446+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
1447+
return (false);
1448+
}
1449+
14431450
var = stmt->outlist;
14441451
switch (PQresultStatus(results))
14451452
{

src/interfaces/ecpg/ecpglib/misc.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ ecpg_init(const struct connection * con, const char *connection_name, const int
106106
{
107107
struct sqlca_t *sqlca = ECPGget_sqlca();
108108

109+
if (sqlca == NULL)
110+
{
111+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY,
112+
NULL);
113+
return (false);
114+
}
115+
109116
ecpg_init_sqlca(sqlca);
110117
if (con == NULL)
111118
{
@@ -143,6 +150,8 @@ ECPGget_sqlca(void)
143150
if (sqlca == NULL)
144151
{
145152
sqlca = malloc(sizeof(struct sqlca_t));
153+
if (sqlca == NULL)
154+
return NULL;
146155
ecpg_init_sqlca(sqlca);
147156
pthread_setspecific(sqlca_key, sqlca);
148157
}
@@ -286,9 +295,11 @@ ecpg_log(const char *format,...)
286295
va_end(ap);
287296

288297
/* dump out internal sqlca variables */
289-
if (ecpg_internal_regression_mode)
298+
if (ecpg_internal_regression_mode && sqlca != NULL)
299+
{
290300
fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
291301
sqlca->sqlcode, sqlca->sqlstate);
302+
}
292303

293304
fflush(debugstream);
294305

@@ -524,6 +535,13 @@ ECPGset_var(int number, void *pointer, int lineno)
524535
{
525536
struct sqlca_t *sqlca = ECPGget_sqlca();
526537

538+
if (sqlca == NULL)
539+
{
540+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
541+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
542+
return;
543+
}
544+
527545
sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
528546
strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
529547
snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);

0 commit comments

Comments
 (0)