From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id A396C3858D28 for ; Fri, 10 Feb 2023 10:05:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A396C3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x630.google.com with SMTP id v23so5973069plo.1 for ; Fri, 10 Feb 2023 02:05:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=gVCQ2EmZzMRq6n1XXmicavIRIO0eGjkhVH+M1DrO6KA=; b=d0D0ANnIqZdXwnCaJofGQGW7SdUXtIkpAlFvKKjzPiCS/Zohzk0IX2NQwEvptDLaO9 WlrPnV9CByWGUtxFqe2rFXHiwyqVFmlw5oFmnTQqG47IGJbUx1RKrpsmCsvppE8Xg+Ve 8lH4tokweujWlWhLpah03CXw1Z1IQ/dlCt0Jas36LwEbckwNp6ofozEy/izCG1Nns5iZ fFHPTpACvEamgF3yMpppEgdjusCyeHmhtbAXEl/Eqxc2dxLHUxpzPXha/aEJq8Zlrid3 9cYNsZWfH+KM3DvGRPYRqciZ4doQJSBuXOR70c1lez7kLAuD0LRtgRzJw4L5vzxB3cKm h0ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gVCQ2EmZzMRq6n1XXmicavIRIO0eGjkhVH+M1DrO6KA=; b=gq/GjogqMi0Aa8lQig0kBk0/VlYYA1dNKo2pXIsKniiMtHIjxf2o4nuCJwPsFkOg+y idlmyv5fySxrhoMhG0vPlfsdIDUKgqUK4t0TgwVllBLMTCkph5Dk1xU59CkOlrB2/IdN SXXjAIDH/VRyFiclaoGHeu2xQuPABg+ANBsliG6SaYoZFWytMxGLBnOA3M8xvaJ40REE aXEtY/z89+3/k3LWG3LRXxIUhFNJfUbu4E2ZzqCiTG5l+coSaHXPPCJiSN/I/e85H7fs aUsYnWJItYFbPw7xC6w09F9NMNFm+x/NAJrwQVgKu3sjUjYSO6EBcOobGGZqqBaUU/R3 npaQ== X-Gm-Message-State: AO0yUKV8lUWDFL4Ahwxm6UVxCo1mGFhrYzBJLHW7SI6UBG1ddJ5C7bUn 3WIdTd+txvMxK/HFAhvw/CYP6s3OqtY= X-Google-Smtp-Source: AK7set+NPUF/bDdhIOfqP8CownGR+7F0xwuqF0xgNKhTMSoTfvTMaxm0Q2Q9tuPPGaiYC40ue7VwRQ== X-Received: by 2002:a17:903:2451:b0:195:f06f:84ff with SMTP id l17-20020a170903245100b00195f06f84ffmr16291243pls.50.1676023526524; Fri, 10 Feb 2023 02:05:26 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:2038:210a:86cd:55ab]) by smtp.gmail.com with ESMTPSA id 2-20020a170902e9c200b0019a6f32e6c7sm1304694plk.150.2023.02.10.02.05.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 02:05:26 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id A76301142769; Fri, 10 Feb 2023 20:35:23 +1030 (ACDT) Date: Fri, 10 Feb 2023 20:35:23 +1030 From: Alan Modra To: binutils@sourceware.org Subject: Fix mmo memory leaks Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3034.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,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 List-Id: The main one here is the section buffer, which can be quite large. By using alloc rather than malloc we can leave tidying memory to the generic bfd code when the bfd is closed. bfd_check_format also releases memory when object_p fails, so while it wouldn't be wrong to bfd_release at bad_format_free in mmo_object_p, it's a little extra code and work for no gain. * mmo.c (mmo_object_p): bfd_alloc rather than bfd_malloc lop_stab_symbol. Don't free/release on error. (mmo_get_spec_section): bfd_zalloc rather than bfd_zmalloc section buffer. (mmo_scan): Free fname on another error path. diff --git a/bfd/mmo.c b/bfd/mmo.c index 6e218345243..bbdaad31e6e 100644 --- a/bfd/mmo.c +++ b/bfd/mmo.c @@ -526,7 +526,7 @@ mmo_object_p (bfd *abfd) important as all of the symbol information can only be 256k. */ abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4; abfd->tdata.mmo_data->lop_stab_symbol - = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1); + = bfd_alloc (abfd, abfd->tdata.mmo_data->max_symbol_length + 1); if (abfd->tdata.mmo_data->lop_stab_symbol == NULL) { @@ -539,7 +539,7 @@ mmo_object_p (bfd *abfd) /* Read in everything. */ if (! mmo_scan (abfd)) - goto bad_format_free; + goto bad_format; if (abfd->symcount > 0) abfd->flags |= HAS_SYMS; @@ -548,12 +548,10 @@ mmo_object_p (bfd *abfd) arches (not recommended due to its small-size limitations). Look at the ELF format for how to make it target-generic. */ if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0)) - goto bad_format_free; + goto bad_format; return _bfd_no_cleanup; - bad_format_free: - free (abfd->tdata.mmo_data->lop_stab_symbol); bad_format: bfd_set_error (bfd_error_wrong_format); bad_final: @@ -1128,8 +1126,8 @@ mmo_get_spec_section (bfd *abfd, int spec_data_number) /* We allocate a buffer here for the advertised size, with head room for tetrabyte alignment. */ - loc = bfd_zmalloc (section_length + 3 - + sizeof (struct mmo_data_list_struct)); + loc = bfd_zalloc (abfd, (section_length + 3 + + sizeof (struct mmo_data_list_struct))); if (loc == NULL) goto format_error; @@ -1888,6 +1886,7 @@ mmo_scan (bfd *abfd) " was already entered as `%s'\n"), abfd, y, fname, file_names[y]); bfd_set_error (bfd_error_bad_value); + free (fname); goto error_return; } -- Alan Modra Australia Development Lab, IBM