Skip to content

Commit 51b5008

Browse files
committed
Fix assorted small bugs in ThrowErrorData().
Copy the palloc'd strings into the correct context, ie ErrorContext not wherever the source ErrorData is. This would be a large bug, except that it appears that all catchers of thrown errors do either EmitErrorReport or CopyErrorData before doing anything that would cause transient memory contexts to be cleaned up. Still, it's wrong and it will bite somebody someday. Fix failure to copy cursorpos and internalpos. Utter the appropriate incantations involving recursion_depth, so that we'll behave sanely if we get an error inside pstrdup. (In general, the body of this function ought to act like, eg, errdetail().) Per code reading induced by Jakob Egger's report.
1 parent 8c90704 commit 51b5008

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/backend/utils/error/elog.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,10 @@ FlushErrorState(void)
15851585
/*
15861586
* ThrowErrorData --- report an error described by an ErrorData structure
15871587
*
1588-
* This is intended to be used to re-report errors originally thrown by
1588+
* This is somewhat like ReThrowError, but it allows elevels besides ERROR,
1589+
* and the boolean flags such as output_to_server are computed via the
1590+
* default rules rather than being copied from the given ErrorData.
1591+
* This is primarily used to re-report errors originally reported by
15891592
* background worker processes and then propagated (with or without
15901593
* modification) to the backend responsible for them.
15911594
*/
@@ -1597,13 +1600,14 @@ ThrowErrorData(ErrorData *edata)
15971600

15981601
if (!errstart(edata->elevel, edata->filename, edata->lineno,
15991602
edata->funcname, NULL))
1600-
return;
1603+
return; /* error is not to be reported at all */
16011604

16021605
newedata = &errordata[errordata_stack_depth];
1603-
oldcontext = MemoryContextSwitchTo(edata->assoc_context);
1606+
recursion_depth++;
1607+
oldcontext = MemoryContextSwitchTo(newedata->assoc_context);
16041608

1605-
/* Copy the supplied fields to the error stack. */
1606-
if (edata->sqlerrcode > 0)
1609+
/* Copy the supplied fields to the error stack entry. */
1610+
if (edata->sqlerrcode != 0)
16071611
newedata->sqlerrcode = edata->sqlerrcode;
16081612
if (edata->message)
16091613
newedata->message = pstrdup(edata->message);
@@ -1615,6 +1619,7 @@ ThrowErrorData(ErrorData *edata)
16151619
newedata->hint = pstrdup(edata->hint);
16161620
if (edata->context)
16171621
newedata->context = pstrdup(edata->context);
1622+
/* assume message_id is not available */
16181623
if (edata->schema_name)
16191624
newedata->schema_name = pstrdup(edata->schema_name);
16201625
if (edata->table_name)
@@ -1625,11 +1630,15 @@ ThrowErrorData(ErrorData *edata)
16251630
newedata->datatype_name = pstrdup(edata->datatype_name);
16261631
if (edata->constraint_name)
16271632
newedata->constraint_name = pstrdup(edata->constraint_name);
1633+
newedata->cursorpos = edata->cursorpos;
1634+
newedata->internalpos = edata->internalpos;
16281635
if (edata->internalquery)
16291636
newedata->internalquery = pstrdup(edata->internalquery);
16301637

16311638
MemoryContextSwitchTo(oldcontext);
1639+
recursion_depth--;
16321640

1641+
/* Process the error. */
16331642
errfinish(0);
16341643
}
16351644

0 commit comments

Comments
 (0)