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 1w2uut-000jBa-0f for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Mar 2026 17:42:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w2uur-00D0wb-1e for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Mar 2026 17:42:45 +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.96) (envelope-from ) id 1w2uur-00D0wT-0i for pgsql-hackers@lists.postgresql.org; Wed, 18 Mar 2026 17:42:45 +0000 Received: from mail-dl1-x1233.google.com ([2607:f8b0:4864:20::1233]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w2uun-00000000yOd-2NHH for pgsql-hackers@postgresql.org; Wed, 18 Mar 2026 17:42:44 +0000 Received: by mail-dl1-x1233.google.com with SMTP id a92af1059eb24-1271195d2a7so412731c88.0 for ; Wed, 18 Mar 2026 10:42:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773855759; x=1774460559; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:fcc:content-language :references:user-agent:mime-version:subject:cc:to:from:message-id :date:from:to:cc:subject:date:message-id:reply-to; bh=bFpdrFKUqUU+GoWRbwEIsnnRxEAt6TQC7hEIY/+/lAY=; b=jB2P+E3nBsXbW5ajEUg+RwRhBWsTZ8ZPRS1QflkCASOn+3dBu3TSn4kDlxzHLoo648 etKmPU4p9oUGnia7s52mfNWJzfEnTg4fi8immzVLRPtoypoKU3hxro13qZi0/MWmCT3F ouHPlHexR2RrsXCcDHV0sX+98tw9AInRZTBpPGRGZXob8UH2uUhnxRsbOQUV6DoavFwr mFNtQ1yty2RK3qm4kmGBwVm7a4IRCGbFr4rubP4V1VSqs29BFW/3PmsOxzzNaZauVFqh 0ziCyLkxKf9/29NauN/bH5M80pYKPzVeERekzGg35RmUVfptRXCBN8U426l5VDgCFZlE ZmJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773855759; x=1774460559; h=content-transfer-encoding:in-reply-to:fcc:content-language :references:user-agent:mime-version:subject:cc:to:from:message-id :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bFpdrFKUqUU+GoWRbwEIsnnRxEAt6TQC7hEIY/+/lAY=; b=k1ZSLJIF6IQ7Le4wno/B+2bHf1ONSzgb2bmZ6J7ovb6eZPQevpiDhRpkrCSPkGcqmI xxfm5P7NIATBCTAWFtsKtVHgIlDB5CjUv8M5em2GIDTnahK2I6xleIehd0jZVKgSJcYf GY5GIvHhnlZO7uKt5rodvYl4A0LBzzSndSXnJ46A3JK3gTpN98Zdh7AIGvEJO6BlHYr/ 6aIJfG/mPGYGzDcjuvmnkDJeNhs4KvDia54e/iiiWrdguiubZ1CXxp+PeADED+iRyOjq CDrK3LkYoIrQgKtaAKhkQAskBrKVU0qz9BHhTCA0IxUy0GL4BqTdPMXEFAjl6bvprYSE hmBg== X-Gm-Message-State: AOJu0YzI/gOwBP6yQ55vkyVkRhvoVwQrZBXzQcRwLCFcYsplGwuII6mt HhzCKCOvhPDApyyAFPqOi2yHEouT/JAT0jUgIF9T/4CEmqfsvJRW3F2w X-Gm-Gg: ATEYQzyuzKvw4gDKznvnyZWb2ULN70Gn1Tnb8Nh5Q59FtkNxUWnxzFjIHE8XSYt81IB QqJMT5UKGXmlNX5TrZAC63PRWnED/7tyLMZKKC33UeXYBnwCV4VD9TX4E57LLsSdy8q8TXWSlyl OTGXhk3uxaDJPwvNd7D4IfU0CK/4GdWkv9mN27D0JbwZahtklUWdeJuYQhwh0ikwYrB+RI2K9cr wgKB1hi//mvvkMwABOniV0FIXSBFsEZsPcnDN3/bZBEWJztFGqM0t2Q6QYd6aWr6eTOo4oD2fhA CZ+007PFnL78K62oQG9svRG4A1mGv2sC36kaTmhYQNYXMmLYsTcevIQm63BmL+AhTlCLVQxQFXS PVbgW+CLGPXQ/35z4qAo3Ca/WOYTSZmq/5aN20Ktr4P8aXgSgzgEQp0s3vop8TARsY1gtI3cRl5 If+4/e8NEpkA5cJbUd3Lvc4qHcvW6i4Ph/sgeGPuAnDlrFh0Ixet4/nNBG X-Received: by 2002:a05:7300:cd90:b0:2ae:55ac:3ff6 with SMTP id 5a478bee46e88-2c0e4fea972mr1961484eec.1.1773855759127; Wed, 18 Mar 2026 10:42:39 -0700 (PDT) Received: from localhost ([2804:14d:328a:a59c:cd24:6dc5:cbc0:83c5]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2c0e5368a7fsm6877739eec.1.2026.03.18.10.42.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Mar 2026 10:42:38 -0700 (PDT) Content-Type: text/plain; charset=UTF-8 Date: Wed, 18 Mar 2026 14:42:35 -0300 Message-Id: From: "Matheus Alcantara" To: "Fujii Masao" , "Ranier Vilela" Cc: "Pg Hackers" Subject: Re: Avoid resource leak (contrib/postgres_fdw/connection.c) MIME-Version: 1.0 User-Agent: Mozilla Thunderbird References: Content-Language: en-US X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; DSN=0; uuencode=0; attachmentreminder=0; deliveryformat=1 X-Identity-Key: id3 Fcc: imap://matheusssilv97%40gmail.com@imap.gmail.com/[Gmail]/Sent Mail In-Reply-To: Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 16/03/26 20:07, Fujii Masao wrote: > On Mon, Mar 16, 2026 at 8:46 PM Ranier Vilela wrote: >> >> Hi. >> >> Per Coverity. >> >> CID 1645716: (#1 of 1): Resource leak (RESOURCE_LEAK) >> 8. leaked_storage: Variable str going out of scope leaks the storage str.data points to. >> >> The function *postgres_fdw_connection* leaks the contents of >> var str.data >> Once that function *cstring_to_text* palloc the contents >> must be necessary to free the var str.data. > > It seems that postgres_fdw_connection() is expected to be called by > ForeignServerConnectionString() via OidFunctionCall3(), > and to allocate memory (including str.data) in the FDWConnectionContext > memory context created by ForeignServerConnectionString(). > > After calling postgres_fdw_connection(), ForeignServerConnectionString() > deletes FDWConnectionContext. So it seems to me that any memory > allocated in that context, including str.data, would not leak. No? > Yeah, this sounds correct, but I'm wondering if the current pfree calls on postgres_fdw_connection() is needed? My previous review was assuming that any allocated memory on postgres_fdw_connection() should be free at the end because of these calls, but ForeignServerConnectionString() already assume that this function leak memory: /* * GetForeignServer, GetForeignDataWrapper, and the connection function * itself all leak memory into CurrentMemoryContext. Switch to a temporary * context for easy cleanup. */ tempContext = AllocSetContextCreate(CurrentMemoryContext, "FDWConnectionContext", ALLOCSET_SMALL_SIZES); -- Matheus Alcantara EDB: https://www.enterprisedb.com