Skip to content

Commit 1f9bf05

Browse files
committed
Use correct text domain for errcontext() appearing within ereport().
The mechanism added in commit dbdf967 for associating the correct translation domain with errcontext strings potentially fails in cases where errcontext() is used within an ereport() macro. Such usage was not originally envisioned for errcontext(), but we do have a few places that do it. In this situation, the intended comma expression becomes just a couple of arguments to errfinish(), which the compiler might choose to evaluate right-to-left. Fortunately, in such cases the textdomain for the errcontext string must be the same as for the surrounding ereport. So we can fix this by letting errstart initialize context_domain along with domain; then it will have the correct value no matter which order the calls occur in. (Note that error stack callback functions are not invoked until errfinish, so normal usage of errcontext won't affect what happens for errcontext calls within the ereport macro.) In passing, make sure that errcontext calls within the main backend set context_domain to something non-NULL. This isn't a live bug because NULL would select the current textdomain() setting which should be the right thing anyway --- but it seems better to handle this completely consistently with the regular domain field. Per report from Dmitry Voronin. Backpatch to 9.3; before that, there wasn't any attempt to ensure that errcontext strings were translated in an appropriate domain.
1 parent 1bf4a84 commit 1f9bf05

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/backend/utils/error/elog.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ errstart(int elevel, const char *filename, int lineno,
378378
edata->funcname = funcname;
379379
/* the default text domain is the backend's */
380380
edata->domain = domain ? domain : PG_TEXTDOMAIN("postgres");
381+
/* initialize context_domain the same way (see set_errcontext_domain()) */
382+
edata->context_domain = edata->domain;
381383
/* Select default errcode based on elevel */
382384
if (elevel >= ERROR)
383385
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
@@ -728,7 +730,7 @@ errcode_for_socket_access(void)
728730
char *fmtbuf; \
729731
StringInfoData buf; \
730732
/* Internationalize the error format string */ \
731-
if (translateit && !in_error_recursion_trouble()) \
733+
if ((translateit) && !in_error_recursion_trouble()) \
732734
fmt = dgettext((domain), fmt); \
733735
/* Expand %m in format string */ \
734736
fmtbuf = expand_fmt_string(fmt, edata); \
@@ -1048,6 +1050,16 @@ errcontext_msg(const char *fmt,...)
10481050
* translate it. Instead, each errcontext_msg() call should be preceded by
10491051
* a set_errcontext_domain() call to specify the domain. This is usually
10501052
* done transparently by the errcontext() macro.
1053+
*
1054+
* Although errcontext is primarily meant for use at call sites distant from
1055+
* the original ereport call, there are a few places that invoke errcontext
1056+
* within ereport. The expansion of errcontext as a comma expression calling
1057+
* set_errcontext_domain then errcontext_msg is problematic in this case,
1058+
* because the intended comma expression becomes two arguments to errfinish,
1059+
* which the compiler is at liberty to evaluate in either order. But in
1060+
* such a case, the set_errcontext_domain calls must be selecting the same
1061+
* TEXTDOMAIN value that the errstart call did, so order does not matter
1062+
* so long as errstart initializes context_domain along with domain.
10511063
*/
10521064
int
10531065
set_errcontext_domain(const char *domain)
@@ -1057,7 +1069,8 @@ set_errcontext_domain(const char *domain)
10571069
/* we don't bother incrementing recursion_depth */
10581070
CHECK_STACK_DEPTH();
10591071

1060-
edata->context_domain = domain;
1072+
/* the default text domain is the backend's */
1073+
edata->context_domain = domain ? domain : PG_TEXTDOMAIN("postgres");
10611074

10621075
return 0; /* return value does not matter */
10631076
}

0 commit comments

Comments
 (0)