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.94.2) (envelope-from ) id 1vG163-005vl2-2J for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Nov 2025 20:24:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vG162-009JXp-1q for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Nov 2025 20:24:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vG12d-0099Q2-Gj for pgsql-hackers@lists.postgresql.org; Mon, 03 Nov 2025 20:20:38 +0000 Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vG12a-005o5q-0T for pgsql-hackers@lists.postgresql.org; Mon, 03 Nov 2025 20:20:38 +0000 Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-3737d0920e6so32953321fa.1 for ; Mon, 03 Nov 2025 12:20:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762201234; x=1762806034; darn=lists.postgresql.org; h=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=RoJLeH81D6MGpTGqwLF2VWtNMQ/Vpo0UZVeEzn2dnKY=; b=jkhxhmDoMdKH7YbiZ1SXh/+HT62jG1nipLXiFiAjEtibLHxzDlNxuTZtaHp9QS3O5c ro8tkQHjMyZXazAXxVZkREUjGSFiIzedXAG7mzQGvdZhKOw6j8HmRl2yOxjtwW67BdB5 LuPl0hzsT7EyeUP1kkaa/8GtT9Lf44QFq+z+enwl6QmXTEUWD9Wfxzk4Tjj/dEM5jp9e osY/6JZvt1nYbNLIzvgVKqU4oVWkDL8HTRobTlGtbHupWhB2alJOFsKhj96wzYCphxps 9mRCnINkMUMy8q1kFhg392VGHq+f2zdBCtM6seWQhEIf5biwEonKsnOGpz1pbnT44t7i 2new== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762201234; x=1762806034; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=RoJLeH81D6MGpTGqwLF2VWtNMQ/Vpo0UZVeEzn2dnKY=; b=uyQsGH2XSev7VaYfziGsJlcmonKmirG/+J+WBQeJzgRrgUBEbGRNtuo/ZRiMYI6RNT zZlj9qIiD0zOpwFzDCIPUwSIgJUFw1TagVW72dfDmJRQrnF7xqmheEUN/GfkZujA/Tp1 eLOJ5j9NIfiHj5/uWKg5OzgdX0OX8eSq2MNKqgeZptSXnjZxG3fnePVCl59QmLuNuej6 Io+Y8AFPcsmOz7fg2VS9L9qnTuDaQ0tlz8iq+8TG65jFhxclZjNIxlQ4DHm4eWPMgLgx ydJTzsdhEH4sL46QklvE36ULukEUkFJKtzq9fRGqeERtXa3mrWA81luruAiWQ0eFZOJq 2w/g== X-Gm-Message-State: AOJu0Ywob9qSpJ8OduCfCDqH1f0ixsDhifcCLkD9Ipn0jgfp5xqiy3xu GM/55ineQYxExh3fKaTvCF2c96QMQZ1NuzUmlKhs+8ADY0rT8lj0GnMl X-Gm-Gg: ASbGncva6ujJSkAzVgcZf64Uqp4L+EMbaye4ZL9mpUAxKiAasV+3ZbMVj2v1jydpZUl tFr790ASqKN4jf9dCud541yuJUlL0BoTNn/bG7WFz9/HYHMWK0NzO8a4PIDeSlAF4ok2/COPtit UawvON0cx1+/pmHBmYrANBFovEdHcIK2ZHucKEyB4PYXtacbf8xrmkltzM2d25OA2xNxh9+BnfU 8nwFxqOhT+wink9hPm8ftzunOdqgeydvcNQsarQCbHGgDAUoYW2U6J7nJYTw6Ves2YI0qSXTcyx uLFrkUpgJo92rEstWneMipnTcA2DkgR2urSn8XITrU+L0U0M7LtE934cgU3dmrjzofgZQcJvLJ4 0w1PmRkC7lEk//UU1T19U2APjn7T53wjWji0GEvqXGMXbnIpKAcLwgLX9dI5SAbu1SPHmBgB29I sBm8xNNNMTzSyVBU4OaJLlz+18kw+FOPUMw9G8XslI/z32s5mpvyQEGn0VuYhB973dCVujR/7I X-Google-Smtp-Source: AGHT+IFJGah8SIj7gMasAySnHhSrNsB4AikaipdQ7f6e5SDMhREe3rWtkO7cUJmKyd6Dlu/Wljj7Mw== X-Received: by 2002:ac2:4e0f:0:b0:592:fb5d:6300 with SMTP id 2adb3069b0e04-5941d4f543fmr4536484e87.10.1762201234058; Mon, 03 Nov 2025 12:20:34 -0800 (PST) Received: from ?IPV6:2a02:aa1:114a:fa1c:67d1:27c3:b72d:518c? ([2a02:aa1:114a:fa1c:67d1:27c3:b72d:518c]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5943437ed1csm165336e87.3.2025.11.03.12.20.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 Nov 2025 12:20:33 -0800 (PST) Content-Type: multipart/alternative; boundary="------------v7qSyEjVIb80eGlPRtKtZ8KR" Message-ID: <1865ee50-8290-4062-b062-5f5ab17364f2@gmail.com> Date: Mon, 3 Nov 2025 21:20:32 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Use stack-allocated StringInfoData To: David Rowley Cc: pgsql-hackers@lists.postgresql.org References: <4379aac8-26f1-42f2-a356-ff0e886228d3@gmail.com> Content-Language: en-US From: Mats Kindahl 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. --------------v7qSyEjVIb80eGlPRtKtZ8KR Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 11/3/25 11:27, David Rowley wrote: > On Mon, 3 Nov 2025 at 20:27, Mats Kindahl wrote: >> We can use >> >> StringInfoData info; >> initStringInfo(&info); >> ... >> appendStringInfo(&info, ...); >> ... >> return info.data; >> >> It was corrected in an earlier commit, but that seems to have been removed so we still have a lot of these cases. >> >> I created a semantic patch to capture most of these cases, which is present in [1], but this is a slightly modified version that might be interesting to include regardless of other changes. The patch is applied and one case that couldn't be matched is manually fixed. Hi David, and thanks for the review. > I think this is a worthwhile conversion. Are you able to create a more > complete version of this? The draft version does introduce quite a few > whitespace changes that aren't wanted. Absolutely, I'll look into it. > You've also introduced a memory > leak in check_publications(), fetch_relation_list(), jsonb_send() and > xml_errorHandler() (NB: destroyStringInfo() doesn't just pfree the > memory for the struct, it pfree's the data too). Ah, that was a mistake from my side. Will fix. > The patch shouldn't > be leaving any memory around that the current master is careful to > pfree. > > Do you have any semi-automated method to find these? Or is it a case > of manually reviewing code with a makeStringInfo() call? This is using Coccinelle to transform the text based on a semantic patch (which is included in the patch). Unfortunately it mess with the whitespace a bit so it is necessary to run pg_intent after.  I'm surprised that there are whitespace changes that should not be there, but I'll take a look. Thanks again, and best wishes, Mats Kindahl --------------v7qSyEjVIb80eGlPRtKtZ8KR Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


On 11/3/25 11:27, David Rowley wrote:
On Mon, 3 Nov 2025 at 20:27, Mats Kindahl <mats.kindahl@gmail.com> wrote:
We can use

       StringInfoData info;
       initStringInfo(&info);
       ...
       appendStringInfo(&info, ...);
       ...
       return info.data;

It was corrected in an earlier commit, but that seems to have been removed so we still have a lot of these cases.

I created a semantic patch to capture most of these cases, which is present in [1], but this is a slightly modified version that might be interesting to include regardless of other changes. The patch is applied and one case that couldn't be matched is manually fixed.

Hi David, and thanks for the review.

I think this is a worthwhile conversion. Are you able to create a more
complete version of this? The draft version does introduce quite a few
whitespace changes that aren't wanted.

Absolutely, I'll look into it.

You've also introduced a memory
leak in check_publications(), fetch_relation_list(), jsonb_send() and
xml_errorHandler() (NB: destroyStringInfo() doesn't just pfree the
memory for the struct, it pfree's the data too).

Ah, that was a mistake from my side. Will fix.

The patch shouldn't
be leaving any memory around that the current master is careful to
pfree.

Do you have any semi-automated method to find these? Or is it a case
of manually reviewing code with a makeStringInfo() call?

This is using Coccinelle to transform the text based on a semantic patch (which is included in the patch). Unfortunately it mess with the whitespace a bit so it is necessary to run pg_intent after.  I'm surprised that there are whitespace changes that should not be there, but I'll take a look.

Thanks again, and best wishes,
Mats Kindahl --------------v7qSyEjVIb80eGlPRtKtZ8KR--