public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-4718] c++tools: Fix memory leak
@ 2021-10-26 17:17 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-10-26 17:17 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:c9bf4d4354b942af00193924cb59c4c6ab9cc4b5
commit r12-4718-gc9bf4d4354b942af00193924cb59c4c6ab9cc4b5
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu Oct 21 22:32:23 2021 +0100
c++tools: Fix memory leak
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.
Diff:
---
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..a1837b3ee10 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 <algorithm>
+#include <memory>
// C
#include <cstring>
// 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<void, Deleter> 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<void, Deleter> guard(buffer);
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;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-10-26 17:17 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-26 17:17 [gcc r12-4718] c++tools: Fix memory leak Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).