提交 f72429a7 编写于 作者: P Pengzhou Tang 提交者: Tang Pengzhou

Fix memory bug within cdbdisp_get_PQerror

cdbdisp_get_PQerror create a new error data object and initialize it
with filename, function values from QE, errdata need a const filename,
function, it does not copy it in ErrorContext. The problem is filename
and function was point to a unstable memory, so when edata is used later,
it may report a SIGSEGV. To resolve this, copy them in the transaction
context because this error data can only be used inside current transaction.
上级 f445f830
......@@ -569,6 +569,7 @@ cdbdisp_dumpDispatchResult(CdbDispatchResult *dispatchResult)
ErrorData *
cdbdisp_get_PQerror(PGresult *pgresult)
{
MemoryContext oldcontext;
ExecStatusType resultStatus = PQresultStatus(pgresult);
/*
......@@ -596,9 +597,14 @@ cdbdisp_get_PQerror(PGresult *pgresult)
char *whoami;
char *fld;
/*
* errstart need a const filename and funcname, make sure they
* are at least const in this transaction.
*/
oldcontext = MemoryContextSwitchTo(TopTransactionContext);
fld = PQresultErrorField(pgresult, PG_DIAG_SOURCE_FILE);
if (fld)
filename = fld;
filename = pstrdup(fld);
fld = PQresultErrorField(pgresult, PG_DIAG_SOURCE_LINE);
if (fld)
......@@ -606,7 +612,8 @@ cdbdisp_get_PQerror(PGresult *pgresult)
fld = PQresultErrorField(pgresult, PG_DIAG_SOURCE_FUNCTION);
if (fld)
funcname = fld;
funcname = pstrdup(fld);
MemoryContextSwitchTo(oldcontext);
/*
* We should only get errors with ERROR level or above, if the
......@@ -643,7 +650,7 @@ cdbdisp_get_PQerror(PGresult *pgresult)
errcontext("%s", fld);
Assert(TopTransactionContext);
MemoryContext oldcontext = MemoryContextSwitchTo(TopTransactionContext);
oldcontext = MemoryContextSwitchTo(TopTransactionContext);
ErrorData *edata = errfinish_and_return(0);
MemoryContextSwitchTo(oldcontext);
return edata;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册