bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Remove from join some recently introduced memory leaks.


From: James Youngman
Subject: [PATCH] Remove from join some recently introduced memory leaks.
Date: Wed, 20 Feb 2008 09:56:09 +0000

2008-02-20  James Youngman  <address@hidden>

        * src/join.c (prevline): Make prevline module-level static, so that
        the allocated items can be freed at exit.
        (free_prevline): new atexit function; frees items in prevline[].
        Use ARRAY_CARDINALITY, so include "argmatch.h" to get that.
        (main): Arrange for free_prevline to be called during exit.
        (get_line): Free prevline[which - 1] also, as that
        will have been allocated by dup_line.

Signed-off-by: James Youngman <address@hidden>
---
 src/join.c |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/join.c b/src/join.c
index 71b11da..f7920e0 100644
--- a/src/join.c
+++ b/src/join.c
@@ -31,6 +31,7 @@
 #include "stdio--.h"
 #include "xmemcoll.h"
 #include "xstrtol.h"
+#include "argmatch.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "join"
@@ -78,6 +79,11 @@ struct seq
     struct line *lines;
   };
 
+/* The previous line read from each file. */
+static struct line *prevline[2];
+
+
+
 /* The name this program was run with.  */
 char *program_name;
 
@@ -300,8 +306,6 @@ freeline (struct line *line)
 static bool
 get_line (FILE *fp, struct line *line, int which)
 {
-  static struct line *prevline[2];
-
   initbuffer (&line->buf);
 
   if (! readlinebuffer (&line->buf, fp))
@@ -322,12 +326,27 @@ get_line (FILE *fp, struct line *line, int which)
     {
       checkorder (prevline[which - 1], line, which);
       freeline (prevline[which - 1]);
+      free (prevline[which - 1]);
     }
   prevline[which - 1] = dup_line (line);
   return true;
 }
 
 static void
+free_prevline (void)
+{
+  size_t i;
+
+  for (i=0; i<ARRAY_CARDINALITY (prevline); i++)
+    {
+      if (prevline[i])
+       freeline (prevline[i]);
+      free (prevline[i]);
+      prevline[i] = NULL;
+    }
+}
+
+static void
 initseq (struct seq *seq)
 {
   seq->count = 0;
@@ -925,6 +944,7 @@ main (int argc, char **argv)
   hard_LC_COLLATE = hard_locale (LC_COLLATE);
 
   atexit (close_stdout);
+  atexit (free_prevline);
 
   print_pairables = true;
   seen_unpairable = false;
-- 
1.5.3.8





reply via email to

[Prev in Thread] Current Thread [Next in Thread]