From patchwork Fri Feb 10 10:05:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 55358 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp864698wrn; Fri, 10 Feb 2023 02:05:39 -0800 (PST) X-Google-Smtp-Source: AK7set/Q8uytuYCfr6J4sunNtZhCJePQnYp5hRNjnT8CDBnibze0B+MS9njXvl/jSzyUK58WJyIP X-Received: by 2002:a17:906:4913:b0:88c:9b39:23c6 with SMTP id b19-20020a170906491300b0088c9b3923c6mr15490214ejq.14.1676023539768; Fri, 10 Feb 2023 02:05:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676023539; cv=none; d=google.com; s=arc-20160816; b=Rg687+U7Yx1jSLN3oEMlg0Em1NuTsQN/MN6GeT/rpaxTMi3S7UDsTrD+ABTP89HPCg CvSKPfp+SxaqrxrFXpT8aqwVbxCTgxC2Z6uCeqB9300BcZGzskz6yvTzaudpuh81CrLY MjP8BrFL+LJzKlMUgl5b+0i+SrGYjVuv0aD8mdjM5Bqjjk3/+6eEn7NONwQHGRqhcbTO P2eLGbzEBlR8R96MuA2BN1hDe7DvAv6JH6qsFmRWeclOxj1LGjfAvq8YbnGa49PstoD8 +BASXU45DSx1/OY+0Q923IlmGmM/X5+QYvjotkbXT3Wo0a5JHz5g2vdkpaJAgBZNyE95 us9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=gVCQ2EmZzMRq6n1XXmicavIRIO0eGjkhVH+M1DrO6KA=; b=Q423xxHxoKd2/N6sqPxQDdvatvsmgU5b/JOVZ04H/BQNbQVlJG0zb7I6jf7xPbnc5e AJGYYFLxBhkEaO9uqPqUdFQqqK5m7DsfK4N7h5rKaU99EGwLvrdahhQIfgJMN+WEJR2N aMf2a5Sdza9g69WaJCSklJg4tnubbZno9PUaTlibKJ0qkijoFj5hlWIfH7Iz5O+ol5pt 6RjtQvpfZ1OR68+rbghUs+rprgr51cZ0CRH7761ioIQTaAidBjy9NA4sSSQuW0DMcFtn nSvdoddQOxFlRPpGfuXGUu7NgEwxvu/COugzdiY81Jkft4OmjGuI9ZTLZrj1GVukVysL UVSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=m+UuAXhB; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id we21-20020a170907235500b007330c08fe49si4326837ejb.206.2023.02.10.02.05.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 02:05:39 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=m+UuAXhB; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 99EDF385840D for ; Fri, 10 Feb 2023 10:05:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99EDF385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676023537; bh=gVCQ2EmZzMRq6n1XXmicavIRIO0eGjkhVH+M1DrO6KA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=m+UuAXhBqBpnB9mo7gVY2tf4VhC7ugUnIFdGDCiBWvEt4BmQQAOv6KVzN7azNAmcS +mY1/LlU83YiFQJc9PUmDISZ8bf1hUK3qm2G6ZkJvUOXT6x6YZ+e7mQ613MkwewvQN XN9i1WnG18iesLTfuglEOLJj/YJ2cWljoP0hAcZQ= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org 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 Received: by mail-pl1-x630.google.com with SMTP id v23so5973069plo.1 for ; Fri, 10 Feb 2023 02:05:27 -0800 (PST) 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-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 To: binutils@sourceware.org Subject: Fix mmo memory leaks Message-ID: MIME-Version: 1.0 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757438059612385230?= X-GMAIL-MSGID: =?utf-8?q?1757438059612385230?= 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; }