Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pskBO-00052U-GO for pgsql-bugs@arkaria.postgresql.org; Sat, 29 Apr 2023 13:00:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pskBL-0007Fo-Nh for pgsql-bugs@arkaria.postgresql.org; Sat, 29 Apr 2023 13:00:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pskBL-0007Fe-Cb for pgsql-bugs@lists.postgresql.org; Sat, 29 Apr 2023 13:00:07 +0000 Received: from mail-lj1-x22f.google.com ([2a00:1450:4864:20::22f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1pskBH-005lxd-WC for pgsql-bugs@lists.postgresql.org; Sat, 29 Apr 2023 13:00:06 +0000 Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-2a8bb726210so7241231fa.1 for ; Sat, 29 Apr 2023 06:00:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682773202; x=1685365202; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=esvk0XeLjy9H4rd2o42j7/cIXwXDJcdoQXLBUfp056Y=; b=ZBXxPgKgsEAixGvlPod7sZN8O2apoQM21tPUl2SE9U0t4y0w0W29zQxNaS7t6dMv8z 0n1BKTJNCaHYEc2Ow8LJd5Vvup7AO6UeU21Bf8Jp1uNHIr8QOJY5MRQwbbd6nwls3f2z sAWHx4Xz9kemOu8ZceUoYzaEDh2Nvt4ySNP7jW+yGTCeixPU2biQtVgNfoS7zXmeAF9E QuQLPzQpMmyASLQVmHXAhaVBd3Q2ceZPLdaNTSaQbTAcr+Vfr8R5KUKTdGivoyYjaq17 hhyTiiBRH3cASOSQNfnsHEt+d47abDeOk3ss2/gnoZr6bAwdQSJCudhln3EuHC8s+2Sg 1Ttg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682773202; x=1685365202; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=esvk0XeLjy9H4rd2o42j7/cIXwXDJcdoQXLBUfp056Y=; b=EB+5Ag8aL2L4pu8Tf+ub/LeZNLOAc1CzTuqipFt9AnMJOBi8foh7Nq5fUh784WkiPU c3G7wRCqypwa7a2GH944QN8EvKagvUYuQeFHV0u0XKtEdHcgzL6oi9cGmFoUabNvHzTi qK/JjoIchx4WC2QiJePyIzhx91k2EJVt9k4A7QMdiz8+jKak+zT/HNSbcWVmRTTCs3db P4oFkQ7Ho8D68aJCDBBfebR+ll40bCXttnUZtw4CnDpTsJ5OLM0dZukX+I2o10G4LuJh W+GLrnzDPnt5wLG7fF10/4zaAMQKgb2mF6CC/8bQbFzpG1bxIKlENoOaWH5Ne0lIjcvR a7zw== X-Gm-Message-State: AC+VfDxPLDocVch9ka5H8wc7SyD/2SKHnn6v4vCS4fSpyTWoz2CviYHS vt3PS2vRvAbTHS3kXywCa1WOf6tIm+0= X-Google-Smtp-Source: ACHHUZ5DtWemO6zPHWtzQfGbMI2jHJVd7q2WMbrJ0x9Jkgg/eQbqKrDj/gXQMt4U/L9wSEzt6GEkXQ== X-Received: by 2002:a2e:7c17:0:b0:2a8:bca1:bfa6 with SMTP id x23-20020a2e7c17000000b002a8bca1bfa6mr2247620ljc.24.1682773202190; Sat, 29 Apr 2023 06:00:02 -0700 (PDT) Received: from [1.0.0.7] ([178.155.28.115]) by smtp.gmail.com with ESMTPSA id i18-20020a056512007200b004db4936c866sm3753005lfo.38.2023.04.29.06.00.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 29 Apr 2023 06:00:01 -0700 (PDT) Message-ID: <5ebae5e4-d401-fadf-8585-ac3eaf53219c@gmail.com> Date: Sat, 29 Apr 2023 16:00:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: BUG #17912: Invalid memory access when converting plpython' array containing empty array Content-Language: en-US To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org References: <17912-82ceed78731d9cdc@postgresql.org> <1392974.1682694846@sss.pgh.pa.us> <1440309.1682705916@sss.pgh.pa.us> <1483429.1682709429@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: <1483429.1682709429@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 28.04.2023 22:17, Tom Lane wrote: > The real problem here is that we don't check that the list nesting > depth is the same throughout the array: if lists are more deeply > nested in later elements, we'll treat those sub-lists as scalars, > leading to inconsistent results. Conversely, a less-deeply-nested > list structure in a later element might still work, if it can be > treated as a sequence. I think the second and third examples > I gave should both throw errors. > > I also notice that the error messages in this area speak of "sequences", > but it is more correct to call them "lists", because Python draws a > distinction. (All lists are sequences, but not vice versa, eg a > string is a sequence but not a list.) > > So I'm thinking about the attached. Thanks for fixing this! Now python's handling of arrays is much nicer and is aligned with the plperl's behaviour: CREATE FUNCTION test_pl() RETURNS text[] AS $$ return [1, [2, 3]]; $$ LANGUAGE plperl; SELECT * FROM test_pl(); ERROR:  multidimensional arrays must have array expressions with matching dimensions CONTEXT:  PL/Perl function "test_pl" I observed another light-hearted case (without the patch, of course): CREATE FUNCTION test_py() RETURNS text[] AS $$ return [1, [2, 3]]; $$ LANGUAGE plpython3u; SELECT * FROM test_py();  {1,"[2, 3]"} So the patch looks more like a bug fix. Though I still see some discrepancy between plperl and plpython: CREATE OR REPLACE FUNCTION test_pl() RETURNS text[] AS $$ return [[],1]; $$ LANGUAGE plperl; SELECT * FROM test_pl(); ERROR:  multidimensional arrays must have array expressions with matching dimensions CONTEXT:  PL/Perl function "test_pl" vs CREATE OR REPLACE FUNCTION test_py() RETURNS text[] AS $$ return [[],1]; $$ LANGUAGE plpython3u; SELECT * FROM test_py();  {[],1} It seems that [] was recognized as "[]" here. While playing with plperl, I found that it handles arrays not perfectly too: CREATE OR REPLACE FUNCTION test_pl() RETURNS text[] AS $$return [1, []];$$ LANGUAGE plperl; SELECT * FROM test_pl(); triggers: ==00:00:08:45.537 2325687== Conditional jump or move depends on uninitialised value(s) ==00:00:08:45.537 2325687==    at 0x61FEDC: construct_md_array (arrayfuncs.c:3500) ==00:00:08:45.537 2325687==    by 0x625917: makeMdArrayResult (arrayfuncs.c:5428) ==00:00:08:45.537 2325687==    by 0x486DEF3: plperl_array_to_datum (plperl.c:1278) ==00:00:08:45.537 2325687==    by 0x486D8E8: plperl_sv_to_datum (plperl.c:1347) ==00:00:08:45.537 2325687==    by 0x4872FA6: plperl_func_handler (plperl.c:2483) ==00:00:08:45.537 2325687==    by 0x4873CE5: plperl_call_handler (plperl.c:1858) ==00:00:08:45.537 2325687==    by 0x41406F: ExecMakeTableFunctionResult (execSRF.c:235) ==00:00:08:45.538 2325687==    by 0x426F1C: FunctionNext (nodeFunctionscan.c:95) ==00:00:08:45.538 2325687==    by 0x414AA7: ExecScanFetch (execScan.c:133) ==00:00:08:45.538 2325687==    by 0x414B42: ExecScan (execScan.c:182) ==00:00:08:45.538 2325687==    by 0x426E2E: ExecFunctionScan (nodeFunctionscan.c:270) ==00:00:08:45.538 2325687==    by 0x4117F1: ExecProcNodeFirst (execProcnode.c:464) ==00:00:08:45.538 2325687== Here, nelems = 2 (from ArrayGetNItems(ndims, dims)), but array_to_datum_internal() generated only one datum. And yet another case: CREATE OR REPLACE FUNCTION test_py() RETURNS text[] AS $$return [[1],[[]]];$$ LANGUAGE plpython3u; regression=# SELECT * FROM test_py();  {{1},{[]}} vs CREATE OR REPLACE FUNCTION test_pl() RETURNS text[] AS $$return [[1],[[]]];$$ LANGUAGE plperl; SELECT * FROM test_pl();  {} > I do not propose this for > back-patching, because it could break applications that work today. > But it seems like good tightening-up for HEAD, or maybe we should > wait for v17 at this point? I suppose that waiting for v17 is preferable if the patch is considered as bringing a new feature (it's not the case) or could require extra time to stabilize. But I'm afraid that anomalies, that would require additional fixes for the change stabilization, could be related to the existing code, and thus that extra time will be invested in improving v16- too. Best regards, Alexander