From patchwork Mon Jun 5 07:21:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 103138 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2505283vqr; Mon, 5 Jun 2023 00:22:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ55c0d2fH0LUhdcDVeooZ+pn2Oce57fXCglpZaHYg5+clD3tebyuHk3+C8pH8zD7bl88heE X-Received: by 2002:a17:907:368c:b0:96f:afe9:25c7 with SMTP id bi12-20020a170907368c00b0096fafe925c7mr5607951ejc.10.1685949750402; Mon, 05 Jun 2023 00:22:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685949750; cv=none; d=google.com; s=arc-20160816; b=KkqupMHO0iXCPsD+dgNi9rvge8nUpG1CZx/+GDMuw8Drt0UENRGKoQfy0M6l9N3T4N xU0kBO/ByeV/e//J/hjxm0cwLJ9jxoDEimiKRrHwKJjpTIDSmiLfyMk2Cs47uqDY0NzA 4ZaEJvC0cgh59aQaB3HvgGlK5+rsrTop6yCIMkEnH0zfx/SK1t2HgLrqRlbUoPQFD0Gt 9ZSz2E8QY7zNwcM+EeYbKhJNh1Mk+qRzjKX1kJ/EsLy6ACpEkCtwMmJYiK0gCZ18MgbS ntQWx6RuT9pMhRmX9OqQtadKMixymX486UZ8Tn+1a8/KMB9gFAKLhpLSWpW9DEYVld5H M8dw== 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=ccgH9y5C47S5BiR5ULbvY4RmGUnNmohHWcekpZAFFIc=; b=jDiPR2G7wchRVLudPClnyXi4ufCQak+KbKBvqDD2I3ElGEjSD69XJNRulb1gLU5wKO f5IzAe00vUFFDEnmauBfVw637MuYYx6Cq3fgyo7CFUQ6ZkUnz2FC6QlZHTW0tVCz8awZ mUK1kKSZVT8K8Ql8FqW1O4rOh94H/GxtrEtviuPlMDEFJX+2Rz1aAQaMlQqM5WJGh1ea 0ETaBDC3dd02j/yE1nWfBI58/gO9JfrddZRqZeR6l1C4WzCXKSBjNnjMvZruECTtNilT urHz4O+oKjyre5Fbpqq37e4gWy3CJnEIiTe6z+EGo1duiFfKi9iqk4SEs6esyNC9+uSp z/2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=oTw7XNRH; 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 ([8.43.85.97]) by mx.google.com with ESMTPS id he35-20020a1709073da300b00977ce0ef56fsi2133092ejc.90.2023.06.05.00.22.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 00:22:30 -0700 (PDT) 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=oTw7XNRH; 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 C1A25385B525 for ; Mon, 5 Jun 2023 07:22:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1A25385B525 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685949745; bh=ccgH9y5C47S5BiR5ULbvY4RmGUnNmohHWcekpZAFFIc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oTw7XNRHujLS3iSYmMtjYw73S5W4kqNEFhdmk363/if6iMeXrNjeQbxm0bmV8y7Xy AB56fIOBFr7Ie3QlolMTpEKRA+VRJzYstuT3F3ob5oCpqFyt1LhdARKZAOBPFUtPDn pf9RIx1MJPuokmNktqMGYZV5pz/gtyhtnRQK6lUU= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id A3F233855891 for ; Mon, 5 Jun 2023 07:21:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A3F233855891 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-64d44b198baso3198099b3a.0 for ; Mon, 05 Jun 2023 00:21:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685949712; x=1688541712; 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=ccgH9y5C47S5BiR5ULbvY4RmGUnNmohHWcekpZAFFIc=; b=OEArkLf2nf7UgJ8QQXr1xh+qJbpYTwDsA83XhOxJUKdeptNAMAM+BxyMC+OYDBRsZ2 eQR8OW8tVCPh2V1Bgbr0TjT8MXetleIatHzUuayPRjHbUco0PaB6Pzjvey89PNooKYPs 3iwPoCsfzHT9pH0saBZ7VirXAbuhz3MOcgkUNOWDBf5bFRDgQmHzKBntAHJ8byRVoP/S HhJXZhrt4jy7opHfp6RPX+yHdzSpXOzpYInUam5tyozvhdvKZ5wDWslajfzZF4ScSPOF Rm6/+PRT6pBU9ScY0gOJme4ZuFVaxHWb5xg077ihbFBfbGHQRx5AuSZ2SVifx7Hc9ThZ pDgw== X-Gm-Message-State: AC+VfDwx1Zzmiq9XwUvp/K8Vs7DjPRu777Uz+xkBtnVmSnsFrMBGKAm1 M1EH2Zx9rRpoT52y7DE2NJ8dXhSiLRI= X-Received: by 2002:a05:6a00:39a4:b0:652:e23b:a078 with SMTP id fi36-20020a056a0039a400b00652e23ba078mr9400749pfb.13.1685949711956; Mon, 05 Jun 2023 00:21:51 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id c6-20020aa781c6000000b0063aa1763146sm4769957pfn.17.2023.06.05.00.21.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 00:21:51 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id EA7981142CA1; Mon, 5 Jun 2023 16:51:48 +0930 (ACST) Date: Mon, 5 Jun 2023 16:51:48 +0930 To: binutils@sourceware.org Subject: bfd_error_on_input messages Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.7 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, T_SCC_BODY_TEXT_LINE 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?1767846445733097258?= X-GMAIL-MSGID: =?utf-8?q?1767846445733097258?= bfd_errmsg uses asprintf for bfd_error_on_input, which means we currently leak memory. Keep a static pointer to the message and free it in various places to minimise the leaks. bfd_set_input_error (NULL, bfd_error_no_error) is a way to free up the last string if that matters. * bfd.c (input_error_msg): New static var. (bfd_set_input_error): Free it here.. (bfd_init): ..and here.. (bfd_errmsg): ..and here. Use it for asprintf output. diff --git a/bfd/bfd.c b/bfd/bfd.c index 71debd73733..4ae73701ce1 100644 --- a/bfd/bfd.c +++ b/bfd/bfd.c @@ -703,8 +703,9 @@ CODE_FRAGMENT */ static bfd_error_type bfd_error; -static bfd *input_bfd; static bfd_error_type input_error; +static bfd *input_bfd; +static char *input_error_msg; const char *const bfd_errmsgs[] = { @@ -792,6 +793,8 @@ bfd_set_input_error (bfd *input, bfd_error_type error_tag) /* This is an error that occurred during bfd_close when writing an archive, but on one of the input files. */ bfd_error = bfd_error_on_input; + free (input_error_msg); + input_error_msg = NULL; input_bfd = input; input_error = error_tag; if (input_error >= bfd_error_on_input) @@ -818,12 +821,13 @@ bfd_errmsg (bfd_error_type error_tag) #endif if (error_tag == bfd_error_on_input) { - char *buf; const char *msg = bfd_errmsg (input_error); - if (asprintf (&buf, _(bfd_errmsgs [error_tag]), + free (input_error_msg); + input_error_msg = NULL; + if (asprintf (&input_error_msg, _(bfd_errmsgs [error_tag]), bfd_get_filename (input_bfd), msg) != -1) - return buf; + return input_error_msg; /* Ick, what to do on out of memory? */ return msg; @@ -1659,6 +1663,8 @@ bfd_init (void) { bfd_error = bfd_error_no_error; input_bfd = NULL; + free (input_error_msg); + input_error_msg = NULL; input_error = bfd_error_no_error; _bfd_error_program_name = NULL; _bfd_error_internal = error_handler_fprintf;