From patchwork Wed Mar 1 18:07:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Costas Argyris X-Patchwork-Id: 65858 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A28FE384D168 for ; Wed, 1 Mar 2023 18:10:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A28FE384D168 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677694238; bh=JP/jaaSZH37QswfMCqgnyXfwLwBqCj0KRAjXI8n1QpE=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=MX9jXx/93N26BG8FStLYktnMiYavbwpsX/qnXbLu+t5Z2jjPTlj+AIIN5Pk/iHK1h 41qBfwnja76/CDzZJwkW1rHyTSfX9CppIst8QcF1lB8pzFbPP+kaRPnRJ2tuYMWNsu 4quRZ1uzZ+qwU2E/MV06v4NZAh6ayhdr1fAx31Do= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by sourceware.org (Postfix) with ESMTPS id 89E953858031 for ; Wed, 1 Mar 2023 18:07:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 89E953858031 Received: by mail-ed1-x52c.google.com with SMTP id u9so7918112edd.2 for ; Wed, 01 Mar 2023 10:07:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677694075; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WJWhWaM8DKan+24SDTN1beu1optqYwKtFmEkImVhPBo=; b=EoPeenlSs2t08berqCl3ntn6dOq/QjikgbWT22l/AGYulqwJosyn54h6TozwJDdT20 FvE+EwZXzEYQ5DKMcUxw9pBRBLFY55cxvDaJYN7lNK65OpFymz9zsKlG/kjbDDdR2CfA dmhopfvw6C1F0WpA/XkH1JvelEJkFMA3C+JSx4I+FCfyBsU7dN3rLhiBvQLANF6xcAw0 dWYanC+HCFjf03/bsZ2r2E/PFjBqOda6+bjxwCpOJEX0owpDnUVvMXgEHRlCBUCn0zQT VrHl1F0U2bQ22zf5HA6hTYA5zNyBTNd0HWgrOSCKM83sKJ/SNPWTy9lxwlZksB+7Gs37 1nyQ== X-Gm-Message-State: AO0yUKXk7+pxGy9pFDYOS4vV1hkHEK342LvXVTj3Q1yeXyHm+bijLfke F08/K2j2AEUPmjW4L+rhZ9JMpk7p2uNRIw7I6KG6ZyVCACp86g== X-Google-Smtp-Source: AK7set9F9Z12TWdCrzlZWYI0TOP2v8NlqogZ2xuW6vy53ueDrwH8YKzYjkJ8xEXhWtJzqC6CiX8v8Nkr9F5L+LLHivk= X-Received: by 2002:a50:c3cd:0:b0:4bc:136a:e25 with SMTP id i13-20020a50c3cd000000b004bc136a0e25mr1609087edf.3.1677694074722; Wed, 01 Mar 2023 10:07:54 -0800 (PST) MIME-Version: 1.0 Date: Wed, 1 Mar 2023 18:07:43 +0000 Message-ID: Subject: [PATCH] libiberty: fix memory leak in pex-win32.c and refactor To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Costas Argyris via Gcc-patches From: Costas Argyris Reply-To: Costas Argyris Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi It seems that the win32_spawn function in libiberty/pex-win32.c is leaking the cmdline buffer in 2/3 exit scenarios (it is only free'd in 1/3). The problem here is that the cleanup code is written 3 times, one at each exit scenario. The proposed attached refactoring has the cleanup code appearing just once and is executed for all exit scenarios, reducing the likelihood of such leaks in the future. Thanks, Costas From ca1ff7b48db2e30963bd4c0e08ecd6653214a5db Mon Sep 17 00:00:00 2001 From: Costas Argyris Date: Sun, 26 Feb 2023 16:34:11 +0000 Subject: [PATCH] libiberty: fix memory leak in pex-win32.c and refactor Fix memory leak of cmdline buffer and refactor to have cleanup code appear once for all exit cases. --- libiberty/pex-win32.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/libiberty/pex-win32.c b/libiberty/pex-win32.c index 02d3a3e839b..82303947de4 100644 --- a/libiberty/pex-win32.c +++ b/libiberty/pex-win32.c @@ -577,14 +577,12 @@ win32_spawn (const char *executable, LPSTARTUPINFO si, LPPROCESS_INFORMATION pi) { - char *full_executable; - char *cmdline; + char *full_executable = NULL; + char *cmdline = NULL; + pid_t pid = (pid_t) -1; char **env_copy; char *env_block = NULL; - full_executable = NULL; - cmdline = NULL; - if (env) { int env_size; @@ -622,13 +620,13 @@ win32_spawn (const char *executable, full_executable = find_executable (executable, search); if (!full_executable) - goto error; + goto exit; cmdline = argv_to_cmdline (argv); if (!cmdline) - goto error; + goto exit; /* Create the child process. */ - if (!CreateProcess (full_executable, cmdline, + if (CreateProcess (full_executable, cmdline, /*lpProcessAttributes=*/NULL, /*lpThreadAttributes=*/NULL, /*bInheritHandles=*/TRUE, @@ -638,26 +636,17 @@ win32_spawn (const char *executable, si, pi)) { - free (env_block); - - free (full_executable); - - return (pid_t) -1; + CloseHandle (pi->hThread); + pid = (pid_t) pi->hProcess; } - + + exit: /* Clean up. */ - CloseHandle (pi->hThread); - free (full_executable); - free (env_block); - - return (pid_t) pi->hProcess; - - error: free (env_block); free (cmdline); free (full_executable); - return (pid_t) -1; + return pid; } /* Spawn a script. This simulates the Unix script execution mechanism. -- 2.30.2