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 1psSOB-00010D-17 for pgsql-bugs@arkaria.postgresql.org; Fri, 28 Apr 2023 18:00:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1psSO8-0004v5-05 for pgsql-bugs@arkaria.postgresql.org; Fri, 28 Apr 2023 18:00:08 +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 1psSO7-0004uw-Lq for pgsql-bugs@lists.postgresql.org; Fri, 28 Apr 2023 18:00:07 +0000 Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1psSO4-005cw3-7P for pgsql-bugs@lists.postgresql.org; Fri, 28 Apr 2023 18:00:06 +0000 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-4f0037de1d1so241458e87.0 for ; Fri, 28 Apr 2023 11:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682704803; x=1685296803; 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=cWse0tsuvum9OzC4VSn8W6VLWaFffsFPPJPnCEOgk7U=; b=B83snlCoEeIlfK50lMZhDpr9Con8dgZLSWqiRe52NliUoVypiJKsM6Wu/iOwmkdaDm zhl+pPeOfVVSgMFCR6TfAd3JTpUDK31YaVc8uj5RiPq8oq2nb2+mxz3JIECQ9rLGGt7v bjdoeQDDpJypbagDmGD9S8GxQF602+6sVZdRc1vnMU83Ol/Rmui0pfslkC2S2z2t42Jf DnlMg9UwgdbVhtZkdgutUbG8DFZDzfLzhTCWrfqXXrLP22IhsF2hmYiSnS+hBiRYOsIH xZDvHj2zp0z3qNyXziGBQlFYoYm1fZh+J38Fem8f3bZnJqWjn3tkGwdmfLrwMG/AQX7i F9vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682704803; x=1685296803; 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=cWse0tsuvum9OzC4VSn8W6VLWaFffsFPPJPnCEOgk7U=; b=VwVZfE2UFlBt9Zu6RQ7cSxqLeGZtsFLmTBv/STepQodYCFTYpwk5g3m+tCKyvF64aN BvOXwAkqdFx33fb9rCB2f3FlvEdHy3/0+8R/k4skFWusBLr40nC/M80+tDY8E7yNQAkN SD0avF1XFpcwDDfBWieahfdpfwcw/bHade44OQQABr8wuohPnzcm8kVBrTrkouqnd16s C7AyotswpeadBEqSWJZ2yNE8pw+y96dSqJ+Zc/R5ZpOSwIHjGGkyohDzeQAgWWnXkaQP zgr1prbNq6jw9LwRf3IY2vpBpIrI/ijz8taBGfgPe+5y3P46CFxY+uArP1aVYaS4XRXc RiRA== X-Gm-Message-State: AC+VfDxFID+MUcLnI6O1OjQXhFTRLa13bOz6DwQSTPDay83geWib5yzt lC0+Lmbp1uzBjHbdLsK7c7+ZI/OmDwqISA== X-Google-Smtp-Source: ACHHUZ6nk1SCq8H2Fk3Wg7ojcBILnP5HlhHzuCZkv99Q4hTzE8nDLttpV7shfGRs0zZ8Zm3whsP13A== X-Received: by 2002:ac2:51ae:0:b0:4ea:fabb:4db1 with SMTP id f14-20020ac251ae000000b004eafabb4db1mr1586728lfk.1.1682704802917; Fri, 28 Apr 2023 11:00:02 -0700 (PDT) Received: from [1.0.0.7] ([178.155.28.115]) by smtp.gmail.com with ESMTPSA id h19-20020ac250d3000000b004db0d26adb4sm3452406lfm.182.2023.04.28.11.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Apr 2023 11:00:02 -0700 (PDT) Message-ID: Date: Fri, 28 Apr 2023 21: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> From: Alexander Lakhin In-Reply-To: <1392974.1682694846@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 Hello Tom, 28.04.2023 18:14, Tom Lane wrote: > PG Bug reporting form writes: >> CREATE EXTENSION plpython3u; >> CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ >> return [[], "a"] >> $$ LANGUAGE plpython3u; >> SELECT test(); >> As I can see, for the first case we get len = 0 in PLySequence_ToArray(); >> elems, nulls palloc'ed with zero elements, but PLyObject_ToScalar() tries >> to write a value into nulls[0]... > Yeah. The calculation of the array size is being done in the wrong > place, so that we may update len to zero before breaking out of the > loop. But really it's poor coding for this function to be doing > its own calculation of the array size at all, rather than consulting > the authoritative ArrayGetNItems(). I think we need something like > the attached. Thank you! I've read the message of the commit you just pushed, and I confirm that there are another oddities in that area. For example: CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ return [1, [2]] $$ LANGUAGE plpython3u; SELECT test(); ---------  {1,[2]} But: CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ return [[1], 2] $$ LANGUAGE plpython3u; SELECT test(); ERROR:  wrong length of inner sequence: has length -1, but 1 was expected DETAIL:  To construct a multidimensional array, the inner sequences must all have the same length. CONTEXT:  while creating return value PL/Python function "test" Or: CREATE OR REPLACE FUNCTION test() RETURNS text[] AS $$ return [["1"], "abc"] $$ LANGUAGE plpython3u; SELECT test(); ERROR:  wrong length of inner sequence: has length 3, but 1 was expected DETAIL:  To construct a multidimensional array, the inner sequences must all have the same length. CONTEXT:  while creating return value PL/Python function "test" Though may be it's okay, considering python's concepts of strings/arrays/sequences, or at least deserves a separate discussion. Best regards, Alexander