From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 064943858025 for ; Thu, 21 Oct 2021 13:28:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 064943858025 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-252-krtoGLiDM2-b-hc8G12cWA-1; Thu, 21 Oct 2021 09:28:50 -0400 X-MC-Unique: krtoGLiDM2-b-hc8G12cWA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5A4B8814250; Thu, 21 Oct 2021 13:28:35 +0000 (UTC) Received: from localhost (unknown [10.33.36.194]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A9095D740; Thu, 21 Oct 2021 13:28:34 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH] c++tools: Fix memory leak Date: Thu, 21 Oct 2021 14:28:34 +0100 Message-Id: <20211021132834.636383-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Oct 2021 13:28:56 -0000 The allocated memory is not freed when returning early due to an error. c++tools/ChangeLog: * resolver.cc (module_resolver::read_tuple_file): Use unique_ptr to ensure memory is freed before returning. --- c++tools/resolver.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/c++tools/resolver.cc b/c++tools/resolver.cc index 421fdaa55fe..d1b73a47778 100644 --- a/c++tools/resolver.cc +++ b/c++tools/resolver.cc @@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see #include "resolver.h" // C++ #include +#include // C #include // OS @@ -114,10 +115,17 @@ module_resolver::read_tuple_file (int fd, char const *prefix, bool force) buffer = mmap (nullptr, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (buffer == MAP_FAILED) return -errno; + struct Deleter { + void operator()(void* p) const { munmap(p, size); } + size_t size; + }; + std::unique_ptr guard(buffer, Deleter{(size_t)stat.st_size}); #else buffer = xmalloc (stat.st_size); if (!buffer) return -errno; + struct Deleter { void operator()(void* p) const { free(p); } }; + std::unique_ptr guard; if (read (fd, buffer, stat.st_size) != stat.st_size) return -errno; #endif @@ -179,12 +187,6 @@ module_resolver::read_tuple_file (int fd, char const *prefix, bool force) } } -#if MAPPED_READING - munmap (buffer, stat.st_size); -#else - free (buffer); -#endif - return 0; } -- 2.31.1