commit:     51d892de53b850467de615774c2d26de007db649
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Tue Aug 18 15:09:28 2015 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Tue Aug 18 15:11:42 2015 +0000
URL:        https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=51d892de

debug: improve cleanup logic

Always set up the cleanup symbol and use it at C time rather than CPP.

We can delete the warning code since we clean those strings up now.

Add ASAN support so LSAN doesn't complain either.

Fix a bug in the leak checking when the -F flag is used -- normally we
don't allocate that string but set it to one of the argv constants.

 porting.h | 11 +++++++++--
 scanelf.c | 53 ++++++++++++++++++++++++++---------------------------
 2 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/porting.h b/porting.h
index 5bbaa77..1f989d2 100644
--- a/porting.h
+++ b/porting.h
@@ -16,7 +16,6 @@
 #endif
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*arr))
-#undef __PAX_UTILS_CLEANUP
 
 #include <assert.h>
 #include <ctype.h>
@@ -66,12 +65,20 @@
 # define __PAX_UTILS_DEFAULT_LD_CACHE_CONFIG ""
 #endif
 
+#undef PAX_UTILS_CLEANUP
 /* bounds checking code will fart on free(NULL) even though that
  * is valid usage.  So let's wrap it if need be.
  */
 #ifdef __BOUNDS_CHECKING_ON
 # define free(ptr) do { if (ptr) free(ptr); } while (0)
-# define __PAX_UTILS_CLEANUP
+# define PAX_UTILS_CLEANUP 1
+#endif
+/* LSAN (Leak Sanitizer) will complain about things we leak. */
+#ifdef __SANITIZE_ADDRESS__
+# define PAX_UTILS_CLEANUP 1
+#endif
+#ifndef PAX_UTILS_CLEANUP
+# define PAX_UTILS_CLEANUP 0
 #endif
 
 /* Few arches can safely do unaligned accesses */

diff --git a/scanelf.c b/scanelf.c
index 82ab626..8232ccd 100644
--- a/scanelf.c
+++ b/scanelf.c
@@ -2264,7 +2264,10 @@ static int parseargs(int argc, char *argv[])
                        break;
                case 'F': {
                        if (out_format) warn("You prob don't want to specify -F 
twice");
-                       out_format = optarg;
+                       if (PAX_UTILS_CLEANUP)
+                               out_format = xstrdup(optarg);
+                       else
+                               out_format = optarg;
                        break;
                }
                case 'z': {
@@ -2478,26 +2481,26 @@ static int parseargs(int argc, char *argv[])
                ret = scanelf_dir(search_path);
        }
 
-#ifdef __PAX_UTILS_CLEANUP
-       /* clean up */
-       xarrayfree(ldpaths);
-       xarrayfree(find_sym_arr);
-       xarrayfree(find_lib_arr);
-       xarrayfree(find_section_arr);
-       free(find_sym);
-       free(find_lib);
-       free(find_section);
-       {
-               size_t n;
-               regex_t *preg;
-               array_for_each(find_sym_regex_arr, n, preg)
-                       regfree(preg);
-               xarrayfree(find_sym_regex_arr);
-       }
+       if (PAX_UTILS_CLEANUP) {
+               /* clean up */
+               xarrayfree(ldpaths);
+               xarrayfree(find_sym_arr);
+               xarrayfree(find_lib_arr);
+               xarrayfree(find_section_arr);
+               free(find_sym);
+               free(find_lib);
+               free(find_section);
+               {
+                       size_t n;
+                       regex_t *preg;
+                       array_for_each(find_sym_regex_arr, n, preg)
+                               regfree(preg);
+                       xarrayfree(find_sym_regex_arr);
+               }
 
-       if (ldcache != 0)
-               munmap(ldcache, ldcache_size);
-#endif
+               if (ldcache != 0)
+                       munmap(ldcache, ldcache_size);
+       }
 
        return ret;
 }
@@ -2544,15 +2547,16 @@ static void parseenv(void)
        qa_wx_load = get_split_env("QA_WX_LOAD");
 }
 
-#ifdef __PAX_UTILS_CLEANUP
 static void cleanup(void)
 {
+       if (!PAX_UTILS_CLEANUP)
+               return;
+
        free(out_format);
        free(qa_textrels);
        free(qa_execstack);
        free(qa_wx_load);
 }
-#endif
 
 int main(int argc, char *argv[])
 {
@@ -2562,12 +2566,7 @@ int main(int argc, char *argv[])
        parseenv();
        ret = parseargs(argc, argv);
        fclose(stdout);
-#ifdef __PAX_UTILS_CLEANUP
        cleanup();
-       warn("The calls to add/delete heap should be off:\n"
-            "\t- 1 due to the out_buffer not being freed in scanelf_fileat()\n"
-            "\t- 1 per QA_TEXTRELS/QA_EXECSTACK/QA_WX_LOAD");
-#endif
        return ret;
 }
 

Reply via email to