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.96) (envelope-from ) id 1vzOgO-000xyl-2K for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Mar 2026 00:41:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vzOgK-00CngX-2W for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Mar 2026 00:41:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vzOgK-00CngP-0d for pgsql-hackers@lists.postgresql.org; Mon, 09 Mar 2026 00:41:12 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vzOgI-00000001Ckd-1CFk for pgsql-hackers@lists.postgresql.org; Mon, 09 Mar 2026 00:41:11 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-82995242934so1200126b3a.0 for ; Sun, 08 Mar 2026 17:41:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773016869; x=1773621669; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=id69kYGN7dfgESvYa+snOup1WU+/MM1g9+c8PbfNqXY=; b=NYTT0Ydap9xfkX6gOia5vA/rtC+96PfkjdmPOUtX2JRsVIkIDS3q1PFljq5rlYRs9o 9fcyzjeUlnAijDezczdVID6bRZNI0DdCbpSkfNj7XEMi/4ZafPMk50hQ7xwQPDYrvSN1 OJOOxEwFMKkRw+aXZOczBDJCKnyGLH7V3cvP64e1AOFx99VP67+2Eerbw0jafDNyy7Jp unipiDCKxoYxwhfu4++UM/n494qhxKjh5o+P/RtuOLN7VwvfTJ7ZcsS+K1Ch3vAxTGdr GQ3KTv1ox/XUmMIpJyjB52y1NKxO+esPhoAXVnneHAdBju3tM3yRJWl7Yg74fA5gwRuc ow9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773016869; x=1773621669; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=id69kYGN7dfgESvYa+snOup1WU+/MM1g9+c8PbfNqXY=; b=ttdkjfS2rG7W9pm4b9Mskct0KDUz/ElynHjx3dkW9LBC+GtENLjj+oyncrckVC1Zk9 jE0owKEA/Xa71NqiX3V0PWyZoNWia3rgcUaFInp753IoY8LafSyciO42OT+/aSbSB86j 6zwuiy7ApVhuQC88PCLuQqsz5StXoh9D0mD15coDvg6hcCXasfKQhdk8JedWLNcQfitC YqYWpE/0DUDrmVmwELp3ugCfP8PbN9sP951yZeSgKZucpx/+MB6cG7wyrJAp+tnk72CO UKla2+cJYbCo9+v5P0LJgtIEYUgeg98VXcmLyJg+KmzJ6UfkYNio5f7jd2r5hxskfgoX Nz7A== X-Gm-Message-State: AOJu0YyoSRLAVFtJeveXMPJxEj/UKiAJOu9/ohdSJu+e1ISM+ztws4Ei 77tFb0TSG//kKEcNnbTSGLqnd5upASxxsQYnfno01pwRK3Lpe6Teexe/ X-Gm-Gg: ATEYQzwRu03gEP0Eq+U5rssGEu72Xva7CXMwwHsFrr2W7LNGMd1MelvWWRnZ+pmTazu UV3qXKNn1GiyYngCCoYTaH6v/II/Fx3MBgNvYYJ8Baacq+WJ6+Mqdj7jeQTfOxZRzwK5XiQBJPW nGVrJ4tLgD0HDUywpqkOF2uiEN9iJDlLUCPIDIv5T5xwQiLtF8iT5C6HIyz+7wuklwi4BGp4UhI urqmcebxar0oYAHD4iWTIEg6vA8JclAz+9ucPZyDe3fQQHdQ/+lbbY5Lr7vrhZRVoFOm1Noo9L1 Z/qpOJEW/+b/gYnjVXcYgCjWnVU3zsVQGLZ4N65PX7wUCWnZSIqMVB08mYxV+/BLS0Tzvh9pAdh oYHTfoxdxdrGyOQlnYEtDMGqi1Vb+m/v2iv+58Afb9gMNbpQn5WCHcR0HUYV4A41WnKkyAFcAAl pVT+8bt04F9pBDcA7IHrptNA6+rEi4/F4= X-Received: by 2002:a05:6a00:ac8f:b0:81f:ac1c:709e with SMTP id d2e1a72fcca58-829a2af4f30mr7491041b3a.31.1773016869192; Sun, 08 Mar 2026 17:41:09 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-829a48d3621sm9055378b3a.62.2026.03.08.17.41.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Mar 2026 17:41:08 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: Trivial Fix: use palloc_array/repalloc_array for BufFile file arrays From: Chao Li In-Reply-To: Date: Mon, 9 Mar 2026 08:40:29 +0800 Cc: Postgres hackers Content-Transfer-Encoding: quoted-printable Message-Id: References: <1D199FA4-C146-493B-B29A-A1BF1084DBBC@gmail.com> <5218079D-02C5-484C-922D-BCF908A55F91@gmail.com> To: Masahiko Sawada X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Mar 7, 2026, at 03:33, Masahiko Sawada = wrote: >=20 > Hi, >=20 > On Tue, Mar 3, 2026 at 5:31=E2=80=AFPM Chao Li = wrote: >>=20 >>=20 >>=20 >>> On Dec 25, 2025, at 11:34, Chao Li wrote: >>>=20 >>>=20 >>>=20 >>>> On Dec 25, 2025, at 11:12, Chao Li wrote: >>>>=20 >>>> Hi Hackers, >>>>=20 >>>> I noticed this error while working on [1]. >>>>=20 >>>> In BufFile, the fields is claimed as an array: >>>> ``` >>>> struct BufFile >>>> { >>>> File *files; /* palloc'd array with numFiles entries */ >>>> ``` >>>>=20 >>>> However, it=E2=80=99s allocated by palloc_object(): >>>> ``` >>>> file->files =3D palloc_object(File); >>>> ``` >>>>=20 >>>> And reallocated by repalloc(): >>>> ``` >>>> file->files =3D (File *) repalloc(file->files, >>>> (file->numFiles + 1) * sizeof(File)); >>>> ``` >>>>=20 >>>> This trivial patch just changes to use palloc_array/repalloc_array, = which makes the intent clearer. >>>>=20 >>>> Best regards, >>>> -- >>>> Chao Li (Evan) >>>> HighGo Software Co., Ltd. >>>> https://www.highgo.com/ >>>>=20 >>>>=20 >>>>=20 >>>>=20 >>>> >>>=20 >>>=20 >>> Sorry for missing the reference of [1]: >>>=20 >>> [1] https://postgr.es/m/aUStrqoOCDRFAq1M@paquier.xyz >>>=20 >>> Best regards, >>> -- >>> Chao Li (Evan) >>> HighGo Software Co., Ltd. >>> https://www.highgo.com/ >>>=20 >>=20 >> PFA v2: >> * Rebased >> * Updated the commit message >=20 > I've reviewed the v2 patch and here is a comment: >=20 > - file->files =3D palloc_object(File); > + file->files =3D palloc_array(File, 1); >=20 > I'm not a fan of this change. This change looks like trying to > distinguish allocated memory by palloc_object() and palloc_array() > even though underlying memory is identical. I'm concerned about this > change creating unnecessary coding conventions. >=20 Hi Masahiho-san, Thank you so much for taking the time to review this patch. Actually, this change was my original motivation for creating the patch. = When I first read the code, I was confused why the field was named with = the plural =E2=80=9Cfiles" even though it was allocated with = palloc_object(). After reading further, I saw that the memory is later = enlarged with repalloc, so it became clear that file->files is really = meant to be an array. To me, the allocation method should reflect the actual meaning of the = object. Here, file->files is an array, no matter how many elements it = currently contains. Even if it only has one element at the moment, = semantically it is still an array. By =E2=80=9Ccreating unnecessary coding conventions=E2=80=9D, are you = concerned that this change might encourage people to always use = palloc_array(type, 1) when allocating a single object? That concern is = understandable, but I think it should depend on the real semantics. If = the memory is for a single object, then of course palloc_array() should = be wrong. Given the nature of C, a "File *" can point either to a single File = object or to a File array. In that sense, palloc_array() makes it = explicit that this pointer is intended to represent an array, while = palloc_object() suggests a single object. So I think changing this to = palloc_array() improves readability in this case. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/