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 1vP61K-004HyX-39 for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Nov 2025 21:28:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vP61I-00Dbwn-2X for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Nov 2025 21:28:49 +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 1vP61I-00Dbwf-1b for pgsql-hackers@lists.postgresql.org; Fri, 28 Nov 2025 21:28:48 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vP61G-0020bo-1Y for pgsql-hackers@postgresql.org; Fri, 28 Nov 2025 21:28:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 5ASLShAP524588; Fri, 28 Nov 2025 16:28:43 -0500 From: Tom Lane To: David Geier cc: Chao Li , pgsql-hackers Subject: Re: Consistently use palloc_object() and palloc_array() In-reply-to: References: Comments: In-reply-to David Geier message dated "Fri, 28 Nov 2025 22:06:34 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <524586.1764365323.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 28 Nov 2025 16:28:43 -0500 Message-ID: <524587.1764365323@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Geier writes: > On 27.11.2025 00:03, Chao Li wrote: >> This is a large patch, I just take a quick look, and found that: >> - *phoned_word =3D palloc(sizeof(char) * strlen(word) + 1); >> + *phoned_word =3D palloc_array(char, strlen(word) + 1); >> And >> - params =3D (const char **) palloc(sizeof(char *)); >> + params =3D palloc_object(const char *); >> Applying palloc_array and palloc_object to char type doesn=E2=80=99t se= em to improve anything. > You mean because sizeof(char) is always 1 and hence we could instead > simply write: > *phoned_word =3D palloc(strlen(word) + 1); > params =3D palloc(1); > I think the _array and _object variants are more expressive and for sure > don't make the code less readable. Yeah, I agree these particular changes seem fine. When you're doing address arithmetic for a memcpy or such, it may be fine to wire in an assumption that sizeof(char) =3D=3D 1, but I think doing that in other contexts is not particularly good style. Another thing to note is that the proposed patch effectively changes the expression evaluation order: - *phoned_word =3D palloc((sizeof(char) * strlen(word)) + 1); + *phoned_word =3D palloc(sizeof(char) * (strlen(word) + 1)); Now, there's not actually any difference because sizeof(char) is 1, but if it hypothetically weren't, the new version is likely more correct. Presumably the +1 is meant to allow room for a trailing \0, which is a char. It'd be a good idea to review the patch to see if there are any places where semantics are changed in a less benign fashion... regards, tom lane