Re: pltcl crashes due to a syntax error - Mailing list pgsql-hackers

From Pierre Forstmann
Subject Re: pltcl crashes due to a syntax error
Date
Msg-id CAM-sOH9zC5VeycS0BFCV4CAEfDL4OLEh_41oWtVLfbOZCtxNFw@mail.gmail.com
Whole thread Raw
In response to pltcl crashes due to a syntax error  ("a.kozhemyakin" <a.kozhemyakin@postgrespro.ru>)
Responses Re: pltcl crashes due to a syntax error
List pgsql-hackers
I can also reproduce in Alma Linux 8.10 with tcl-devel 8.6.8

gdb says:


(gdb) p interp
$1 = (Tcl_Interp *) 0x288a500
(gdb) p *interp
$2 = {resultDontUse = 0x288a6d8 "", freeProcDontUse = 0x0,
  errorLineDontUse = 1}
(gdb) p msg
No symbol "msg" in current context.
(gdb) p emsg
$3 = 0x27ebe48 "list element in braces followed by \")\" instead of space"


Involved PG source code is:

**********************************************************************
  * throw_tcl_error - ereport an error returned from the Tcl interpreter
  **********************************************************************/
 static void
 throw_tcl_error(Tcl_Interp *interp, const char *proname)
 {
  /*
  * Caution is needed here because Tcl_GetVar could overwrite the
  * interpreter result (even though it's not really supposed to), and we
  * can't control the order of evaluation of ereport arguments. Hence, make
  * real sure we have our own copy of the result string before invoking
  * Tcl_GetVar.
  */
  char *emsg;
  char *econtext;
 
  emsg = pstrdup(utf_u2e(Tcl_GetStringResult(interp)));
  econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
  errmsg("%s", emsg),
  errcontext("%s\nin PL/Tcl function \"%s\"",
  econtext, proname)));
 }

I understand that Tcl_GetVar should not be used any more and should be replaced by Tcl_GetStringResult
(but I know nothing about Tcl internals)

Following patch :
diff postgres/src/pl/tcl/pltcl.c.orig postgres/src/pl/tcl/pltcl.c
1373c1373,1376
<       econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
---
>       /*
>        * econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
>        */
>       econtext = utf_u2e(Tcl_GetStringResult(interp));

gives:

pierre=# CREATE OR REPLACE PROCEDURE test_proc(INOUT a text)
 AS $$
 set aa [concat $1 "+" $1]
 return [list $aa $aa])
 $$
 LANGUAGE pltcl;
CREATE PROCEDURE
pierre=# CALL test_proc('abc');
2024-06-02 14:22:45.223 CEST [61649] ERROR:  list element in braces followed by ")" instead of space
2024-06-02 14:22:45.223 CEST [61649] CONTEXT:  list element in braces followed by ")" instead of space
in PL/Tcl function "test_proc"
2024-06-02 14:22:45.223 CEST [61649] STATEMENT:  CALL test_proc('abc');
ERROR:  list element in braces followed by ")" instead of space
CONTEXT:  list element in braces followed by ")" instead of space
in PL/Tcl function "test_proc"


PF





Le sam. 1 juin 2024 à 06:36, a.kozhemyakin <a.kozhemyakin@postgrespro.ru> a écrit :
Hello hackers,

When executing the following query on master branch:

CREATE EXTENSION pltcl;
CREATE or replace PROCEDURE test_proc5(INOUT a text)
         LANGUAGE pltcl
         AS $$
         set aa [concat $1 "+" $1]
         return [list $aa $aa])
         $$;

CALL test_proc5('abc');
CREATE EXTENSION
CREATE PROCEDURE
server closed the connection unexpectedly
         This probably means the server terminated abnormally
         before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

The connection to the server was lost. Attempting reset: Failed.


Core was generated by `postgres: postgres postgres [loca'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __strlen_sse2 () at ../sysdeps/x86_64/multiarch/strlen-sse2.S:142
142     ../sysdeps/x86_64/multiarch/strlen-sse2.S: No such file or
directory.
(gdb) bt
#0  __strlen_sse2 () at ../sysdeps/x86_64/multiarch/strlen-sse2.S:142
#1  0x00007f5f1353ba6a in utf_u2e (src=0x0) at pltcl.c:77
#2  0x00007f5f1353c9f7 in throw_tcl_error (interp=0x55ec24bdaf70,
proname=0x55ec24b6b140 "test_proc5") at pltcl.c:1373
#3  0x00007f5f1353ed64 in pltcl_func_handler
(fcinfo=fcinfo@entry=0x7ffdbfb407a0,
call_state=call_state@entry=0x7ffdbfb405d0,
pltrusted=pltrusted@entry=true) at pltcl.c:1029
#4  0x00007f5f1353ee8d in pltcl_handler (fcinfo=0x7ffdbfb407a0,
pltrusted=pltrusted@entry=true) at pltcl.c:765
#5  0x00007f5f1353f1ef in pltcl_call_handler (fcinfo=<optimized out>) at
pltcl.c:698
#6  0x000055ec239ec64a in ExecuteCallStmt
(stmt=stmt@entry=0x55ec24a9a940, params=params@entry=0x0,
atomic=atomic@entry=false, dest=dest@entry=0x55ec24a6ea18) at
functioncmds.c:2285
#7  0x000055ec23c103a7 in standard_ProcessUtility (pstmt=0x55ec24a9a9d8,
queryString=0x55ec24a99e68 "CALL test_proc5('abc');",
readOnlyTree=<optimized out>, context=PROCESS_UTILITY_TOPLEVEL,
params=0x0, queryEnv=0x0, dest=0x55ec24a6ea18,
     qc=0x7ffdbfb40f40) at utility.c:851
#8  0x000055ec23c1081b in ProcessUtility
(pstmt=pstmt@entry=0x55ec24a9a9d8, queryString=<optimized out>,
readOnlyTree=<optimized out>,
context=context@entry=PROCESS_UTILITY_TOPLEVEL, params=<optimized out>,
queryEnv=<optimized out>,
     dest=0x55ec24a6ea18, qc=0x7ffdbfb40f40) at utility.c:523
#9  0x000055ec23c0e04e in PortalRunUtility
(portal=portal@entry=0x55ec24b18108, pstmt=0x55ec24a9a9d8,
isTopLevel=isTopLevel@entry=true,
setHoldSnapshot=setHoldSnapshot@entry=true,
dest=dest@entry=0x55ec24a6ea18, qc=qc@entry=0x7ffdbfb40f40)
     at pquery.c:1158
#10 0x000055ec23c0e3b7 in FillPortalStore
(portal=portal@entry=0x55ec24b18108, isTopLevel=isTopLevel@entry=true)
at pquery.c:1031
#11 0x000055ec23c0e6ee in PortalRun (portal=portal@entry=0x55ec24b18108,
count=count@entry=9223372036854775807, isTopLevel=isTopLevel@entry=true,
run_once=run_once@entry=true, dest=dest@entry=0x55ec24a9aec8,
altdest=altdest@entry=0x55ec24a9aec8,
     qc=0x7ffdbfb41130) at pquery.c:763
#12 0x000055ec23c0acca in exec_simple_query
(query_string=query_string@entry=0x55ec24a99e68 "CALL
test_proc5('abc');") at postgres.c:1274
#13 0x000055ec23c0caad in PostgresMain (dbname=<optimized out>,
username=<optimized out>) at postgres.c:4680
#14 0x000055ec23c0687a in BackendMain (startup_data=<optimized out>,
startup_data_len=<optimized out>) at backend_startup.c:105
#15 0x000055ec23b766bf in postmaster_child_launch
(child_type=child_type@entry=B_BACKEND,
startup_data=startup_data@entry=0x7ffdbfb41354 "",
startup_data_len=startup_data_len@entry=4,
client_sock=client_sock@entry=0x7ffdbfb41390)
     at launch_backend.c:265
#16 0x000055ec23b7ab36 in BackendStartup
(client_sock=client_sock@entry=0x7ffdbfb41390) at postmaster.c:3593
#17 0x000055ec23b7adb0 in ServerLoop () at postmaster.c:1674
#18 0x000055ec23b7c20c in PostmasterMain (argc=argc@entry=3,
argv=argv@entry=0x55ec24a54030) at postmaster.c:1372
#19 0x000055ec23aacf9f in main (argc=3, argv=0x55ec24a54030) at main.c:197



pgsql-hackers by date:

Previous
From: jian he
Date:
Subject: Re: POC: GROUP BY optimization
Next
From: Alexander Korotkov
Date:
Subject: Re: POC: GROUP BY optimization