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 1vP6JC-004VFD-1B for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Nov 2025 21:47:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vP6JA-00DlNr-2o for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Nov 2025 21:47:17 +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 1vP6JA-00DlNg-1T for pgsql-hackers@lists.postgresql.org; Fri, 28 Nov 2025 21:47:16 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vP6J8-0020kV-1O for pgsql-hackers@postgresql.org; Fri, 28 Nov 2025 21:47:15 +0000 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-6418738efa0so3835614a12.1 for ; Fri, 28 Nov 2025 13:47:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764366432; x=1764971232; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=HkGiulYyYD0NkMA+ZGjEAyefvtmMt1w+BsKnUfjl83w=; b=cVZuRpsjCmOIo9MHDCjV4xP6Oaq9letzzkjphTOdS0p6QV06xLTK1Cne7zeXFL6PAS P25lcLolaOday7dnm1guRqLY8tRudFmmSdvD/FSxxw4VbyJY5MnokZ2VLXqxkTNt0qi0 W5ZRX8d3Audb9LH6pHPm4yI5wieQKbr4W+g6afdANtHyUZaTyqZR+YD+Unu+/nP9iJlc kUdSXlmUkI9yRS8RLMMoQvA8gw70t+QRH4z6HVIH3h/QJrfuxvd112ufE+SimAAM2zta 5Uj9Qhu2HBjdvLfJ96DRUhrFPHmsQqcHpX+tuxnbhQ4IE+1r7EdeCqzUr26nTtGdOZwH oScQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764366432; x=1764971232; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HkGiulYyYD0NkMA+ZGjEAyefvtmMt1w+BsKnUfjl83w=; b=P2QG0pYbiI0FRyIaxa8ZWBelmvLjVtGxh4wMsrlQ6RV/Qz7P8IQhBS5rOlH4FiypzL fpTDChAo11ACPWrKmcx5K7B64Q+kigpisO1Cz6yWsVu3GFbL8tjfcXGgeGsj0fSEtak8 60DF8pf2VI0bu+R+ey7LHZLlT9IFcIWZFQaIuqKkEQY66Z5GetN3DZrDasTdnksM6JY+ mpz/ot4Q4JJ/AeScrXRXe5WPvfJVUME+FqDhAgcIaWNoWcSBxD4VKSo41k/q4sqrmWrX 5wm+EnbinjfLMVkqDzlI96KqIarzKD/lWCZ0IkeKjsvf+9IKF0z8s2YMnyqHUDf7wxXy u+lQ== X-Forwarded-Encrypted: i=1; AJvYcCUMqa+wk8C8bfcLEyqoWcMqRXhXfbNelQ6MZ/EPFoFi9XOuFoiGLToTZ7/xHsyV+P8Dw+I/i6nrrphUutZu@postgresql.org X-Gm-Message-State: AOJu0Yz9ea39jXb+85J1CDH3Uo9N8hPsv0LwBvZCvr4lsc+vJ9oezaQL /zlCVPWnIsfx+1e7oqQvFsF9m97x2xH2J1dCVDrreI8QJE/GMZu7Oeca X-Gm-Gg: ASbGncvMclwikns4SpCoYnACoZAmpqAroe+e6jhXHQ5BbG+v52FRT7AeyTAoFt4fQzp qucXvo3L/F/YCKjpTifmhsMLw9ARvL2t+C4BMXH1QYNd3ygXiDBLk+KFjqGO5OxrBxZsR6D2fF/ 7yDO63dqMyTVr6ayIgc5idn1tVKH+rBTI5RwKpVJ5pJTTZiCIqs0hQahnGGA1N3w7/LylFm/KjK 2J7dpjNTRHqMdlGJ5SCloCaWujCQmKi1K7V08554ZgyL8zy13DdyZjRpiczhBcpy9C4Z808rDa+ zt7cO+pwi3Pp9LWe4HwZMfUPmjQYBsQDpH41l5+Nv6go0ArWBRK2Iu7s/SVn2l6wkKIXJQq+nIr /6eD4xqoRaM1SCKD7EuZ47/iDIryT9qGQhGciaNqDkToiCxz6YDAaQjv3cyQGJW637WTt07PYOm nVaGBko1J/okytgS8= X-Google-Smtp-Source: AGHT+IFCsc5I7SgliLr+SIOiLmcIG+uHROCdyM+6IDm9qSgohcnXGhgFXNKlL4x1ijrLfnbquLaI/g== X-Received: by 2002:a05:6402:234c:b0:63c:66b5:bbbf with SMTP id 4fb4d7f45d1cf-64555be1b78mr27483281a12.20.1764366432221; Fri, 28 Nov 2025 13:47:12 -0800 (PST) Received: from [192.168.2.32] ([165.225.27.7]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-64750a6ea6esm5366610a12.6.2025.11.28.13.47.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Nov 2025 13:47:10 -0800 (PST) Message-ID: <1e7f0c17-a965-4d5b-ad88-b435a4d97490@gmail.com> Date: Fri, 28 Nov 2025 22:47:08 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Consistently use palloc_object() and palloc_array() To: Tom Lane Cc: Chao Li , pgsql-hackers References: <524587.1764365323@sss.pgh.pa.us> Content-Language: en-US From: David Geier In-Reply-To: <524587.1764365323@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 28.11.2025 22:28, Tom Lane wrote: > 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 = palloc(sizeof(char) * strlen(word) + 1); >>> + *phoned_word = palloc_array(char, strlen(word) + 1); >>> And >>> - params = (const char **) palloc(sizeof(char *)); >>> + params = palloc_object(const char *); >>> Applying palloc_array and palloc_object to char type doesn’t seem to improve anything. > >> You mean because sizeof(char) is always 1 and hence we could instead >> simply write: >> *phoned_word = palloc(strlen(word) + 1); >> params = 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) == 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 = palloc((sizeof(char) * strlen(word)) + 1); > + *phoned_word = 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. Oh right. Good catch! > 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... I intentionally tried to avoid any semantic changes but it's of course possible something slipped through by accident. It's some ~1700 occurrences in the patch. If it takes ~10 seconds to check a single, it would take ~4.7h to review the entire patch. If no one is willing to take this on, I could split up the patch in 4 to 5 that each can be reviewed by someone else. -- David Geier