From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by sourceware.org (Postfix) with ESMTPS id 689CC3858C30 for ; Thu, 30 Mar 2023 04:50:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 689CC3858C30 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-pj1-x102d.google.com with SMTP id c4so147449pjs.4 for ; Wed, 29 Mar 2023 21:50:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680151813; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=7sdL4hiGT90+ket4CtbGTQSduevHBZG9QZCFxEOOP44=; b=DCjUz0/YdQxv1Xgztvl3Zq2EDYZ1GaDUgI0zlJwIFmHeQrSdJVbpfWIILFi9w4765Y Zpw/5Dr5mbHXIgVKEPSjCI/yDwwqmVn6hGuZPppoxB2laExN0+mZmL3lXAPfcKIFV7nC IPMp8X/2xfQLA1yImIogWPbAuEPX0RozXy52tRbQHWt+Bv2IHAtvuohk2cryEfzSIQYe eOT1ZYUaytkrUZv1CszGXhwd+ovK51uauTXvQ7OEUatIc5LDpyVSweByvLR+qCzXkEBf j6r5MqGedczx/yRTJxiCfLDCst6+huwtAfQBXVdwEyjgZU2WWWpa6rvVjF7TLpZ5H9Hu 1Bsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680151813; 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=7sdL4hiGT90+ket4CtbGTQSduevHBZG9QZCFxEOOP44=; b=FxoH0rCIMHtMjke5KPrRtC7oVEu+ESP4vBvgdS3FeTvKVCMlRXZFsZvFoUyhx/9ClF UHZLE4M9pIZeTFRVV6IumdJAk7JcOJ8QErtwqc9KA1yT4zc2iC62MZWTWYQ2M+F0mdw3 SPamE7zkxh5V45rvuXNLV726wMWHLPQRXSDNv9Uo12xu1glS7wtIawpnY+h55Agyl8kJ mBOiU1rwy6Hws1d4ochjMia1lDlgwh6jXcEosvzal7Arv4PViFBfIu+1gcKQzb2ZQV8R OCyuUVABndfzbzukTCK5uEumAW84u4Y6K+7LwPAQQ/KwcShCt1f+B5TVdMAo9/IbCveQ L6fw== X-Gm-Message-State: AAQBX9fZ4LiBKY4zybnyZM0X6Pu1FVkQM/C1WBD4RItkN9BEwGze0R8h NAxxgCWlNgrqUiOGZoBB88743YlVWm8= X-Google-Smtp-Source: AKy350aQ2uQZ6ODLfRkCDqc0ULgB91xCp3mNfejkBlEc1JXg0HDvtX+NLDiiPs/jqwuUwXYN//ki5Q== X-Received: by 2002:a17:902:f550:b0:1a1:d70f:7114 with SMTP id h16-20020a170902f55000b001a1d70f7114mr26859164plf.14.1680151813031; Wed, 29 Mar 2023 21:50:13 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:5286:655c:8aca:de7f]) by smtp.gmail.com with ESMTPSA id g6-20020a1709026b4600b001a19cf1b37esm23783412plt.40.2023.03.29.21.50.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:50:12 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 155711142E2D; Thu, 30 Mar 2023 15:20:10 +1030 (ACDT) Date: Thu, 30 Mar 2023 15:20:10 +1030 From: Alan Modra To: binutils@sourceware.org Subject: Fix memory leak in bfd_get_debug_link_info_1 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3034.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_NUMSUBJECT,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: * opncls.c (bfd_get_alt_debug_link_info): Don't bother freeing after bfd_malloc_and_get_section failure. (get_build_id): Likewise. (bfd_get_debug_link_info_1): Likewise. Free section contents when crc not present. * section.c (bfd_malloc_and_get_section): Document that the buffer is NULL on error return. diff --git a/bfd/opncls.c b/bfd/opncls.c index abea464baa4..81e124457e9 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -1211,10 +1211,7 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) return NULL; if (!bfd_malloc_and_get_section (abfd, sect, &contents)) - { - free (contents); - return NULL; - } + return NULL; /* CRC value is stored after the filename, aligned up to 4 bytes. */ name = (char *) contents; @@ -1222,7 +1219,10 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) crc_offset = strnlen (name, size) + 1; crc_offset = (crc_offset + 3) & ~3; if (crc_offset + 4 > size) - return NULL; + { + free (name); + return NULL; + } *crc32 = bfd_get_32 (abfd, contents + crc_offset); return name; @@ -1297,10 +1297,7 @@ bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len, return NULL; if (!bfd_malloc_and_get_section (abfd, sect, & contents)) - { - free (contents); - return NULL; - } + return NULL; /* BuildID value is stored after the filename. */ name = (char *) contents; @@ -1817,10 +1814,7 @@ get_build_id (bfd *abfd) } if (!bfd_malloc_and_get_section (abfd, sect, & contents)) - { - free (contents); - return NULL; - } + return NULL; /* FIXME: Paranoia - allow for compressed build-id sections. Maybe we should complain if this size is different from diff --git a/bfd/section.c b/bfd/section.c index 3b1993b60a5..1e7e8ac7522 100644 --- a/bfd/section.c +++ b/bfd/section.c @@ -1600,6 +1600,8 @@ SYNOPSIS DESCRIPTION Read all data from @var{section} in BFD @var{abfd} into a buffer, *@var{buf}, malloc'd by this function. + Return @code{true} on success, @code{false} on failure in which + case *@var{buf} will be NULL. */ bool -- Alan Modra Australia Development Lab, IBM