commit:     8f6a879b16fc664b03c361ae203def52220d0cb9
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Wed Dec 28 22:54:08 2016 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Wed Dec 28 22:54:08 2016 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=8f6a879b

add cleanup logic to handle specific leaks

There's a few code paths that we leak resources because we know
we're exiting (soon).  Add some logic to avoid false positives.

 libq/libq.h |  4 ++++
 main.c      |  6 ++++--
 main.h      | 13 +++++++++++++
 q.c         |  8 ++++----
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/libq/libq.h b/libq/libq.h
index 6a74a48..94254e1 100644
--- a/libq/libq.h
+++ b/libq/libq.h
@@ -12,6 +12,10 @@ FILE *warnout;
 #define warnfp(fmt, args...) warnf(fmt ": %s" , ## args , strerror(errno))
 #define _err(wfunc, fmt, args...) \
        do { \
+       if (USE_CLEANUP) { \
+               if (warnout != stderr) \
+                       fclose(warnout); \
+       } \
        warnout = stderr; \
        wfunc(fmt , ## args); \
        exit(EXIT_FAILURE); \

diff --git a/main.c b/main.c
index 543ac36..9921363 100644
--- a/main.c
+++ b/main.c
@@ -1102,13 +1102,15 @@ void reinitialize_as_needed(void)
        if (reinitialize)
                array_for_each(overlays, n, overlay) {
                        ret = initialize_flat(overlay, CACHE_EBUILD, true);
-                       IF_DEBUG(free((void *)ret));
+                       if (USE_CLEANUP)
+                               free((void *)ret);
                }
 
        if (reinitialize_metacache)
                array_for_each(overlays, n, overlay) {
                        ret = initialize_flat(overlay, CACHE_METADATA, true);
-                       IF_DEBUG(free((void *)ret));
+                       if (USE_CLEANUP)
+                               free((void *)ret);
                }
 }
 

diff --git a/main.h b/main.h
index 6848a9f..d3df8b1 100644
--- a/main.h
+++ b/main.h
@@ -95,6 +95,19 @@
 # define IF_DEBUG(x)
 #endif
 
+#undef USE_CLEANUP
+/* LSAN (Leak Sanitizer) will complain about things we leak. */
+#ifdef __SANITIZE_ADDRESS__
+# define USE_CLEANUP 1
+#endif
+/* Coverity catches some things we leak on purpose. */
+#ifdef __COVERITY__
+# define USE_CLEANUP 1
+#endif
+#ifndef USE_CLEANUP
+# define USE_CLEANUP 0
+#endif
+
 #define GETOPT_LONG(A, a, ex) \
        getopt_long(argc, argv, ex A ## _FLAGS, a ## _long_opts, NULL)
 

diff --git a/q.c b/q.c
index 6ee9aef..ea1fb4d 100644
--- a/q.c
+++ b/q.c
@@ -87,8 +87,8 @@ int q_main(int argc, char **argv)
                case 'm':
                        if (optarg) {
                                const char *path = initialize_flat(optarg, 
CACHE_METADATA, true);
-                               if (path) { /* silence warning */ }
-                               IF_DEBUG(free((void *)path));
+                               if (USE_CLEANUP)
+                                       free((void *)path);
                                reinitialize_metacache = -1;
                        } else
                                reinitialize_metacache = 1;
@@ -96,8 +96,8 @@ int q_main(int argc, char **argv)
                case 'r':
                        if (optarg) {
                                const char *path = initialize_flat(optarg, 
CACHE_EBUILD, true);
-                               if (path) { /* silence warning */ }
-                               IF_DEBUG(free((void *)path));
+                               if (USE_CLEANUP)
+                                       free((void *)path);
                                reinitialize = -1;
                        } else
                                reinitialize = 1;

Reply via email to