Skip to content

Commit d278ff3

Browse files
author
Michael Meskes
committed
Check for out of memory when allocating sqlca.
Patch by Michael Paquier Conflicts: src/interfaces/ecpg/ecpglib/connect.c
1 parent abf9284 commit d278ff3

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

src/interfaces/ecpg/compatlib/informix.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,8 @@ void
10241024
ECPG_informix_reset_sqlca(void)
10251025
{
10261026
struct sqlca_t *sqlca = ECPGget_sqlca();
1027+
if (sqlca == NULL)
1028+
return;
10271029

10281030
memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
10291031
}

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ ECPGnoticeReceiver(void *arg, const PGresult *result)
219219

220220
int sqlcode;
221221

222+
if (sqlca == NULL)
223+
{
224+
ecpg_log("out of memory");
225+
return;
226+
}
227+
228+
(void) arg; /* keep the compiler quiet */
229+
222230
if (sqlstate == NULL)
223231
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
224232

@@ -279,6 +287,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
279287
*options = NULL,
280288
*connect_string = NULL;
281289

290+
if (sqlca == NULL)
291+
{
292+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
293+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
294+
ecpg_free(dbname);
295+
return false;
296+
}
297+
282298
ecpg_init_sqlca(sqlca);
283299

284300
/*
@@ -559,6 +575,13 @@ ECPGdisconnect(int lineno, const char *connection_name)
559575
struct sqlca_t *sqlca = ECPGget_sqlca();
560576
struct connection *con;
561577

578+
if (sqlca == NULL)
579+
{
580+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
581+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
582+
return (false);
583+
}
584+
562585
#ifdef ENABLE_THREAD_SAFETY
563586
pthread_mutex_lock(&connections_mutex);
564587
#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)
@@ -244,6 +251,13 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
244251
struct variable data_var;
245252
struct sqlca_t *sqlca = ECPGget_sqlca();
246253

254+
if (sqlca == NULL)
255+
{
256+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
257+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
258+
return false;
259+
}
260+
247261
va_start(args, index);
248262
ecpg_init_sqlca(sqlca);
249263
ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
@@ -649,6 +663,13 @@ ECPGdeallocate_desc(int line, const char *name)
649663
struct descriptor *prev;
650664
struct sqlca_t *sqlca = ECPGget_sqlca();
651665

666+
if (sqlca == NULL)
667+
{
668+
ecpg_raise(line, ECPG_OUT_OF_MEMORY,
669+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
670+
return false;
671+
}
672+
652673
ecpg_init_sqlca(sqlca);
653674
for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
654675
{
@@ -688,6 +709,13 @@ ECPGallocate_desc(int line, const char *name)
688709
struct descriptor *new;
689710
struct sqlca_t *sqlca = ECPGget_sqlca();
690711

712+
if (sqlca == NULL)
713+
{
714+
ecpg_raise(line, ECPG_OUT_OF_MEMORY,
715+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
716+
return false;
717+
}
718+
691719
ecpg_init_sqlca(sqlca);
692720
new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
693721
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
@@ -1441,6 +1441,13 @@ ecpg_execute(struct statement * stmt)
14411441
if (!ecpg_check_PQresult(results, stmt->lineno, stmt->connection->connection, stmt->compat))
14421442
return (false);
14431443

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

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
}
@@ -288,9 +297,11 @@ ecpg_log(const char *format,...)
288297
va_end(ap);
289298

290299
/* dump out internal sqlca variables */
291-
if (ecpg_internal_regression_mode)
300+
if (ecpg_internal_regression_mode && sqlca != NULL)
301+
{
292302
fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
293303
sqlca->sqlcode, sqlca->sqlstate);
304+
}
294305

295306
fflush(debugstream);
296307

@@ -531,6 +542,13 @@ ECPGset_var(int number, void *pointer, int lineno)
531542
{
532543
struct sqlca_t *sqlca = ECPGget_sqlca();
533544

545+
if (sqlca == NULL)
546+
{
547+
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
548+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
549+
return;
550+
}
551+
534552
sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
535553
strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
536554
snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);

0 commit comments

Comments
 (0)