Skip to content

Commit 2a781b5

Browse files
author
Michael Meskes
committed
Check for out of memory when allocating sqlca.
Patch by Michael Paquier
1 parent 853222c commit 2a781b5

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

@@ -215,6 +222,13 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
215222
char *sqlstate;
216223
char *message;
217224

225+
if (sqlca == NULL)
226+
{
227+
ecpg_log("out of memory");
228+
ECPGfree_auto_mem();
229+
return;
230+
}
231+
218232
if (result)
219233
{
220234
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
@@ -323,6 +337,12 @@ sqlprint(void)
323337
{
324338
struct sqlca_t *sqlca = ECPGget_sqlca();
325339

340+
if (sqlca == NULL)
341+
{
342+
ecpg_log("out of memory");
343+
return;
344+
}
345+
326346
sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
327347
fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
328348
}

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,13 @@ ecpg_process_output(struct statement * stmt, bool clear_result)
14891489
ntuples,
14901490
act_field;
14911491

1492+
if (sqlca == NULL)
1493+
{
1494+
ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
1495+
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
1496+
return (false);
1497+
}
1498+
14921499
var = stmt->outlist;
14931500
switch (PQresultStatus(stmt->results))
14941501
{

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)
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