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 1pgHYA-00082k-JF for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Mar 2023 04: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 1pgHY8-00071K-Ip for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Mar 2023 04:00:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgHY8-00071B-47 for pgsql-bugs@lists.postgresql.org; Sun, 26 Mar 2023 04:00:08 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pgHY5-0006pP-Hq for pgsql-bugs@lists.postgresql.org; Sun, 26 Mar 2023 04:00:07 +0000 Received: by mail-lf1-x134.google.com with SMTP id c29so7203064lfv.3 for ; Sat, 25 Mar 2023 21:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679803203; h=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=NmIvvycJcWqtToJhdf4SMStaRaylLBN+JnKWV2EvJg4=; b=ppQvLt4xcT0zKGY2/NwHQwQYa6rMEcRdpVbdnJ/GYpK9/7/sTuGjUJ8932tZj1OGX8 m8/wLkf0MiK+Hu2GIAHU2eueq70/8VoFZd5OB1eXNQb0Sud23V7ZkC24ukTChyqBnX1Q JoYOxXhSALpPezIsNPS5sVpX7Ck3tO8fgF1EpUW9j8iFrU1N7kthFUnxe9AM38qSz2K7 ybfRLStTZSqVQE8V8WWhy84A7AcCDZZnI9RwbVeZpdXtYK6zBCYTGIuKTNFZdKW37t73 R8s/nurdjOTyFEnT4aJ5CiD91SClkJEbV9bSK7EbIwwH5+gI/0UrcdR8sOA8GfAzXgB6 SXew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679803203; h=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=NmIvvycJcWqtToJhdf4SMStaRaylLBN+JnKWV2EvJg4=; b=a5ERzJSL3FTZ2j85TXyCoUicU9e3lPC/gaJJFOHBNYmpN8xpFndjD9A6hpwR0F88Wk 4rOkHJSrnHNB7UYIqiR3LGLrO2WJVQXEz0rweImz3PsUMmMJHAky2ItWKAKZ1h3ro6ue 7AyjAmXKEoragdqVq3451zDZq+/hsfgWQdFMqFPHcKzbOGrobhI4tJcnrEeFyDz7jIRO CIFImmu7KZn5io8uDKkHWbSr5OAsV5iC2DAX2FEfDKjLL8ZJdsNzcTvJngQ6J9QtLKRr S7/vu6QNh9MbO0gVMPEz9LbgqcZVf19hl09Tl0l1eOsTKHmodptokPou88iE25LlczIi 4ovg== X-Gm-Message-State: AAQBX9eM5u4H5gjSLQIeKt7fl93tUtT28lQ8O6QnXUQUq33NCvTLY5CD ryRK0RXNQcp7TR8JgSp5P8g= X-Google-Smtp-Source: AKy350aUbRE8LFz4zsj7niVEDeCfFie3WfJOrymgBTYUVwdBnkuBxMy6V39S5oL579Vd+gZ8jV9Jbw== X-Received: by 2002:ac2:5d69:0:b0:4ea:f8cc:4c9c with SMTP id h9-20020ac25d69000000b004eaf8cc4c9cmr2186546lft.63.1679803202896; Sat, 25 Mar 2023 21:00:02 -0700 (PDT) Received: from [1.0.0.7] ([178.155.28.218]) by smtp.gmail.com with ESMTPSA id c3-20020ac24143000000b004dda87ecae3sm4110923lfi.246.2023.03.25.21.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Mar 2023 21:00:02 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------AxAjPcR3VZCg7La7QzCDOtrm" Message-ID: <87461d66-e96b-24f8-3e66-1daf937fff98@gmail.com> Date: Sun, 26 Mar 2023 07: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 #17858: ExecEvalArrayExpr() leaves uninitialised memory for multidim array with nulls Content-Language: en-US To: Richard Guo , Tom Lane Cc: pgsql-bugs@lists.postgresql.org References: <17858-8fd287fd3663d051@postgresql.org> <4240d86f-7e9f-388b-fd96-f8944860c0f5@gmail.com> <1216375.1679408668@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------AxAjPcR3VZCg7La7QzCDOtrm Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 22.03.2023 06:07, Richard Guo wrote: > > On Tue, Mar 21, 2023 at 10:24 PM Tom Lane wrote: > > Alexander Lakhin writes: > > I'm afraid that zeroing only bytes behind nitems bits is not enough, as outDatum() doesn't bother to calculate > the exact > > size of nulls bitmap, it just outputs all bytes of a datum (40 bytes in that case): > > In that case, won't padding bytes between array elements also create > issues?  Seems like we have to just zero the whole array area, like > those other functions do. > I came to a conclusion that the padding is not affected by palloc/palloc0 there. There we have:             subdata[outer_nelems] = ARR_DATA_PTR(array);             subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array); ...             memcpy(dat, subdata[i], subbytes[i]);             dat += subbytes[i]; So the result array elements come packed one after another from the input array and are not padded here. > > Yeah, this should be the right fix, to use palloc0 instead here.  FWIW > currently in the codes there are 14 places that explicitly allocate > ArrayType, 13 of them using palloc0, the only exception is the one > discussed here. > > BTW, in array_set_slice() and array_set_element() we explicitly zero the > null bitmap although the whole array area is allocated with palloc0.  Is > this necessary? Yeah, I see two instances of MemSet(newnullbitmap, 0, ...): 1) In array_set_element():         /* Zero the bitmap to take care of marking inserted positions null */         MemSet(newnullbitmap, 0, (newnitems + 7) / 8); It came with cecb60755 (2005-11-17). 2) In array_set_slice():             /* Zero the bitmap to handle marking inserted positions null */             MemSet(newnullbitmap, 0, (nitems + 7) / 8); It came with 352a56ba6 (2006-09-29). These MemSets were meaningful before 18c0b4ecc (2011-04-27). The commit 18c0b4ecc changed palloc() to palloc0() almost everywhere, so that the MemSets became redundant, but at the same time the discussed palloc() (residing in execQual.c:ExecEvalArray() back then) somehow evaded the change. So maybe the fix for the bug can be seen as a supplement for 18c0b4ecc. Best regards, Alexander --------------AxAjPcR3VZCg7La7QzCDOtrm Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
22.03.2023 06:07, Richard Guo wrote:

On Tue, Mar 21, 2023 at 10:24 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Alexander Lakhin <exclusion@gmail.com> writes:
> I'm afraid that zeroing only bytes behind nitems bits is not enough, as outDatum() doesn't bother to calculate the exact
> size of nulls bitmap, it just outputs all bytes of a datum (40 bytes in that case):

In that case, won't padding bytes between array elements also create
issues?  Seems like we have to just zero the whole array area, like
those other functions do.

I came to a conclusion that the padding is not affected by
palloc/palloc0 there.
There we have:
            subdata[outer_nelems] = ARR_DATA_PTR(array);
            subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
...
            memcpy(dat, subdata[i], subbytes[i]);
            dat += subbytes[i];

So the result array elements come packed one after another from the input
array and are not padded here.


Yeah, this should be the right fix, to use palloc0 instead here.  FWIW
currently in the codes there are 14 places that explicitly allocate
ArrayType, 13 of them using palloc0, the only exception is the one
discussed here.

BTW, in array_set_slice() and array_set_element() we explicitly zero the
null bitmap although the whole array area is allocated with palloc0.  Is
this necessary?

Yeah, I see two instances of MemSet(newnullbitmap, 0, ...):
1) In array_set_element():
        /* Zero the bitmap to take care of marking inserted positions null */
        MemSet(newnullbitmap, 0, (newnitems + 7) / 8);

It came with cecb60755 (2005-11-17).

2) In array_set_slice():
            /* Zero the bitmap to handle marking inserted positions null */
            MemSet(newnullbitmap, 0, (nitems + 7) / 8);

It came with 352a56ba6 (2006-09-29).

These MemSets were meaningful before 18c0b4ecc (2011-04-27).
The commit 18c0b4ecc changed palloc() to palloc0() almost everywhere, so
that the MemSets became redundant, but at the same time the discussed
palloc() (residing in execQual.c:ExecEvalArray() back then) somehow evaded
the change.

So maybe the fix for the bug can be seen as a supplement for 18c0b4ecc.

Best regards,
Alexander
--------------AxAjPcR3VZCg7La7QzCDOtrm--