Skip to content

Commit 89ef2ae

Browse files
committed
Fix pl/tcl's handling of errors from Tcl_ListObjGetElements().
In a procedure or function returning tuple, we use that function to parse the Tcl script's result, which is supposed to be a Tcl list. If it isn't, you get an error. Commit 26abb50 incautiously supposed that we could use throw_tcl_error() to report such an error. That doesn't actually work, because low-level functions like Tcl_ListObjGetElements() don't fill Tcl's errorInfo variable. The result is either a null-pointer-dereference crash or emission of misleading context information describing the previous Tcl error. Back off to just reporting the interpreter's result string, and improve throw_tcl_error()'s comment to explain when to use it. Also, although the similar code in pltcl_trigger_handler() avoided this mistake, it was using a fairly confusing wording of the error message. Improve that while we're here. Per report from A. Kozhemyakin. Back-patch to all supported branches. Erik Wienhold and Tom Lane Discussion: https://postgr.es/m/6a2a1c40-2b2c-4a33-8b72-243c0766fcda@postgrespro.ru
1 parent 6b52e22 commit 89ef2ae

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/pl/tcl/expected/pltcl_call.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ END
6666
$$;
6767
NOTICE: a: 10
6868
NOTICE: _a: 10, _b: 20
69+
-- syntax error in result tuple
70+
CREATE PROCEDURE test_proc10(INOUT a text)
71+
LANGUAGE pltcl
72+
AS $$
73+
return [list a {$a + $a}])
74+
$$;
75+
CALL test_proc10('abc');
76+
ERROR: could not parse function return value: list element in braces followed by ")" instead of space
6977
DROP PROCEDURE test_proc1;
7078
DROP PROCEDURE test_proc2;
7179
DROP PROCEDURE test_proc3;

src/pl/tcl/pltcl.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,10 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
10271027
/* Convert function result to tuple */
10281028
resultObj = Tcl_GetObjResult(interp);
10291029
if (Tcl_ListObjGetElements(interp, resultObj, &resultObjc, &resultObjv) == TCL_ERROR)
1030-
throw_tcl_error(interp, prodesc->user_proname);
1030+
ereport(ERROR,
1031+
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1032+
errmsg("could not parse function return value: %s",
1033+
utf_u2e(Tcl_GetStringResult(interp)))));
10311034

10321035
tup = pltcl_build_tuple_result(interp, resultObjv, resultObjc,
10331036
call_state);
@@ -1293,7 +1296,7 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
12931296
&result_Objc, &result_Objv) != TCL_OK)
12941297
ereport(ERROR,
12951298
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1296-
errmsg("could not split return value from trigger: %s",
1299+
errmsg("could not parse trigger return value: %s",
12971300
utf_u2e(Tcl_GetStringResult(interp)))));
12981301

12991302
/* Convert function result to tuple */
@@ -1356,6 +1359,10 @@ pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
13561359

13571360
/**********************************************************************
13581361
* throw_tcl_error - ereport an error returned from the Tcl interpreter
1362+
*
1363+
* Caution: use this only to report errors returned by Tcl_EvalObjEx() or
1364+
* other variants of Tcl_Eval(). Other functions may not fill "errorInfo",
1365+
* so it could be unset or even contain details from some previous error.
13591366
**********************************************************************/
13601367
static void
13611368
throw_tcl_error(Tcl_Interp *interp, const char *proname)

src/pl/tcl/sql/pltcl_call.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ END
7171
$$;
7272

7373

74+
-- syntax error in result tuple
75+
76+
CREATE PROCEDURE test_proc10(INOUT a text)
77+
LANGUAGE pltcl
78+
AS $$
79+
return [list a {$a + $a}])
80+
$$;
81+
82+
CALL test_proc10('abc');
83+
84+
7485
DROP PROCEDURE test_proc1;
7586
DROP PROCEDURE test_proc2;
7687
DROP PROCEDURE test_proc3;

0 commit comments

Comments
 (0)