Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sDs4H-00FiK9-PT for pgsql-hackers@arkaria.postgresql.org; Sun, 02 Jun 2024 20:44:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sDs4G-007mkR-B2 for pgsql-hackers@arkaria.postgresql.org; Sun, 02 Jun 2024 20:44:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sDs4F-007mkJ-Ph for pgsql-hackers@lists.postgresql.org; Sun, 02 Jun 2024 20:44:40 +0000 Received: from mout-u-107.mailbox.org ([80.241.59.207]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sDs48-0021i1-5T for pgsql-hackers@lists.postgresql.org; Sun, 02 Jun 2024 20:44:38 +0000 Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4VspkM3bLhz9sRW; Sun, 2 Jun 2024 22:44:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1717361067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=FkOKuIoi1sun3x02v1NAi1oTIgQ8Z33tXxlHTjLPeb8=; b=GdpOaaxmJoDMCh5eBV+OoxF4W+67uDN3IgVbgmF5jo2srwyqze6swPraOT/44EAxldNVjM CeK/WKkPXKNIxeXfBvYSTIpYzrQQW0q3288Pb4ytWj352vBBqnzu3B9+lNZWoevpegK2yK Q1fZCKYovsXVeaSdoTH8e7g+GR6JAhwJLyoukPVAbO5wvykvRzvgUkhVLsVHIpLIzMA889 3P5Eji2eOk3bh8bhXIm5L19DTuK0BE1hvykq5KxZ0+inJKjs97as9Nb0wTYw609QFqK7S2 sYinmmvDXNNOvVruNi7dZurQSGdtmUIR8VN85517aRtEA/ILnx/BsAo0dqtTRQ== Date: Sun, 2 Jun 2024 22:44:25 +0200 From: Erik Wienhold To: Pierre Forstmann Cc: "a.kozhemyakin" , pgsql-hackers@lists.postgresql.org Subject: Re: pltcl crashes due to a syntax error Message-ID: <5af13487-7722-4bdd-bad3-939598c8dd4e@ewie.name> References: <6a2a1c40-2b2c-4a33-8b72-243c0766fcda@postgrespro.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 4VspkM3bLhz9sRW List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-06-02 14:32 +0200, Pierre Forstmann wrote: > 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" Tcl_GetStringResult is already used for emsg. Setting econtext to same string is rather pointless. The problem is that Tcl_ListObjGetElements does not set errorInfo if conversion fails. From the manpage: "If listPtr is not already a list value, Tcl_ListObjGetElements will attempt to convert it to one; if the conversion fails, it returns TCL_ERROR and leaves an error message in the interpreter's result value if interp is not NULL." Tcl_GetVar returns null if errorInfo does not exist. Omitting econtext from errcontext in that case looks like the proper fix to me. Or just do away with throw_tcl_error and call ereport directly. Compare that to pltcl_trigger_handler where the same case is handled like this: /************************************************************ * Otherwise, the return value should be a column name/value list * specifying the modified tuple to return. ************************************************************/ if (Tcl_ListObjGetElements(interp, Tcl_GetObjResult(interp), &result_Objc, &result_Objv) != TCL_OK) ereport(ERROR, (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED), errmsg("could not split return value from trigger: %s", utf_u2e(Tcl_GetStringResult(interp))))); -- Erik