On 09/09/14 10:50, Pádraig Brady wrote:
> On 09/09/2014 04:55 AM, Paul Eggert wrote:
>> I noticed other problems that are at least somewhat related to the
>> recent coreutils multi-binary executable changes, and fixed some of
>> these problems with the attached patches.
>>
>> Subject: [PATCH 3/4] maint: prefer 'return status;' to 'exit (status);' in 
>> 'main'
>> http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=v8.23-19-g8defcee
> 
> simpler so +1
> There were a couple of syntax-check errors with this, fixed in
> http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=v8.23-21-g20f46a2

Another caveat with using return() instead of exit()
is that valgrind will now report mem allocated in main()
as "definitely lost" as noted at http://stackoverflow.com/q/31622764/4421

That precludes using this to auto flag mem leaks:
  valgrind --leak-check=full --error-exitcode=1 --errors-for-leak-kinds=definite

The attached patch explicitly free()s these allocations, in dev builds.

cheers,
Pádraig.
From 8077dd7ab7ebd43f9254aaa1062a72ad1be832cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Fri, 4 Sep 2015 14:38:06 +0100
Subject: [PATCH] maint: avoid "definitely lost" valgrind warnings

Since commit v8.23-19-g8defcee, main() will return,
rather than calling exit(), this inducing "definitely lost"
warnings in valgrinds leak checker.  That precludes using
the following to flag memory leaks:

  valgrind --leak-check=full --error-exitcode=1 \
           --errors-for-leak-kinds=definite

* src/pr.c (main): In dev bnuilds, explicitly free memory allocated.
* src/sort.c (main): Likewise.
* src/tail.c (main): Likewise.
* src/tsort.c (tsort): Likewise.
---
 src/pr.c    | 1 +
 src/sort.c  | 2 ++
 src/tail.c  | 2 ++
 src/tsort.c | 2 ++
 4 files changed, 7 insertions(+)

diff --git a/src/pr.c b/src/pr.c
index d79d84f..887e125 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -1130,6 +1130,7 @@ main (int argc, char **argv)
     }
 
   cleanup ();
+  IF_LINT (free (file_names));
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, _("standard input"));
diff --git a/src/sort.c b/src/sort.c
index 85fc38f..a7eda83 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -4742,6 +4742,8 @@ main (int argc, char **argv)
       sort (files, nfiles, outfile, nthreads);
     }
 
+  IF_LINT (free (files));
+
   if (have_read_stdin && fclose (stdin) == EOF)
     die (_("close failed"), "-");
 
diff --git a/src/tail.c b/src/tail.c
index c062d40..f916d74 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -2354,6 +2354,8 @@ main (int argc, char **argv)
       tail_forever (F, n_files, sleep_interval);
     }
 
+  IF_LINT (free (F));
+
   if (have_read_stdin && close (STDIN_FILENO) < 0)
     error (EXIT_FAILURE, errno, "-");
   return ok ? EXIT_SUCCESS : EXIT_FAILURE;
diff --git a/src/tsort.c b/src/tsort.c
index d4be03d..0533451 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -528,6 +528,8 @@ tsort (const char *file)
         }
     }
 
+  IF_LINT (free (root));
+
   if (fclose (stdin) != 0)
     error (EXIT_FAILURE, errno, "%s",
            is_stdin ? _("standard input") : quote (file));
-- 
2.4.1

Reply via email to