Skip to content

Commit dda633a

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 1d5c5ae commit dda633a

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
@@ -49,6 +49,14 @@ CALL test_proc6(2, 3, 4);
4949
6 | 8
5050
(1 row)
5151

52+
-- syntax error in result tuple
53+
CREATE PROCEDURE test_proc10(INOUT a text)
54+
LANGUAGE pltcl
55+
AS $$
56+
return [list a {$a + $a}])
57+
$$;
58+
CALL test_proc10('abc');
59+
ERROR: could not parse function return value: list element in braces followed by ")" instead of space
5260
DROP PROCEDURE test_proc1;
5361
DROP PROCEDURE test_proc2;
5462
DROP PROCEDURE test_proc3;

src/pl/tcl/pltcl.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,10 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
10221022
/* Convert function result to tuple */
10231023
resultObj = Tcl_GetObjResult(interp);
10241024
if (Tcl_ListObjGetElements(interp, resultObj, &resultObjc, &resultObjv) == TCL_ERROR)
1025-
throw_tcl_error(interp, prodesc->user_proname);
1025+
ereport(ERROR,
1026+
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1027+
errmsg("could not parse function return value: %s",
1028+
utf_u2e(Tcl_GetStringResult(interp)))));
10261029

10271030
tup = pltcl_build_tuple_result(interp, resultObjv, resultObjc,
10281031
call_state);
@@ -1289,7 +1292,7 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
12891292
&result_Objc, &result_Objv) != TCL_OK)
12901293
ereport(ERROR,
12911294
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1292-
errmsg("could not split return value from trigger: %s",
1295+
errmsg("could not parse trigger return value: %s",
12931296
utf_u2e(Tcl_GetStringResult(interp)))));
12941297

12951298
/* Convert function result to tuple */
@@ -1352,6 +1355,10 @@ pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
13521355

13531356
/**********************************************************************
13541357
* throw_tcl_error - ereport an error returned from the Tcl interpreter
1358+
*
1359+
* Caution: use this only to report errors returned by Tcl_EvalObjEx() or
1360+
* other variants of Tcl_Eval(). Other functions may not fill "errorInfo",
1361+
* so it could be unset or even contain details from some previous error.
13551362
**********************************************************************/
13561363
static void
13571364
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
@@ -52,6 +52,17 @@ $$;
5252
CALL test_proc6(2, 3, 4);
5353

5454

55+
-- syntax error in result tuple
56+
57+
CREATE PROCEDURE test_proc10(INOUT a text)
58+
LANGUAGE pltcl
59+
AS $$
60+
return [list a {$a + $a}])
61+
$$;
62+
63+
CALL test_proc10('abc');
64+
65+
5566
DROP PROCEDURE test_proc1;
5667
DROP PROCEDURE test_proc2;
5768
DROP PROCEDURE test_proc3;

0 commit comments

Comments
 (0)