Skip to content

Commit c7c2cc6

Browse files
committed
Fix out-of-memory handling in ecpglib.
ecpg_build_params() would crash on a null pointer dereference if realloc() failed, due to updating the persistent "stmt" struct too aggressively. (Even without the crash, this would've leaked the old storage that we were trying to realloc.) Per Coverity. This seems to have been broken in commit 0cc0507, so back-patch into v12.
1 parent ac2dcca commit c7c2cc6

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,26 +1502,37 @@ ecpg_build_params(struct statement *stmt)
15021502
}
15031503
else
15041504
{
1505-
if (!(stmt->paramvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1505+
bool realloc_failed = false;
1506+
char **newparamvalues;
1507+
int *newparamlengths;
1508+
int *newparamformats;
1509+
1510+
/* enlarge all the param arrays */
1511+
if ((newparamvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1512+
stmt->paramvalues = newparamvalues;
1513+
else
1514+
realloc_failed = true;
1515+
1516+
if ((newparamlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1517+
stmt->paramlengths = newparamlengths;
1518+
else
1519+
realloc_failed = true;
1520+
1521+
if ((newparamformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1522+
stmt->paramformats = newparamformats;
1523+
else
1524+
realloc_failed = true;
1525+
1526+
if (realloc_failed)
15061527
{
15071528
ecpg_free_params(stmt, false);
15081529
ecpg_free(tobeinserted);
15091530
return false;
15101531
}
1511-
stmt->paramvalues[stmt->nparams] = tobeinserted;
15121532

1513-
if (!(stmt->paramlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1514-
{
1515-
ecpg_free_params(stmt, false);
1516-
return false;
1517-
}
1533+
/* only now can we assign ownership of "tobeinserted" to stmt */
1534+
stmt->paramvalues[stmt->nparams] = tobeinserted;
15181535
stmt->paramlengths[stmt->nparams] = binary_length;
1519-
1520-
if (!(stmt->paramformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1521-
{
1522-
ecpg_free_params(stmt, false);
1523-
return false;
1524-
}
15251536
stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
15261537
stmt->nparams++;
15271538

0 commit comments

Comments
 (0)