Blob Blame History Raw
diff -urNp coreutils-8.17-orig/src/sort.c coreutils-8.17/src/sort.c
--- coreutils-8.17-orig/src/sort.c	2012-08-20 13:15:39.703470009 +0200
+++ coreutils-8.17/src/sort.c	2012-08-20 13:29:47.177468563 +0200
@@ -292,6 +292,9 @@ struct merge_node_queue
                                    when popping. */
 };
 
+/* Used to implement --unique (-u). */
+static struct line saved_line;
+
 /* FIXME: None of these tables work with multibyte character sets.
    Also, there are many other bugs when handling multibyte characters.
    One way to fix this is to rewrite 'sort' to use wide characters
@@ -3943,13 +3946,12 @@ queue_pop (struct merge_node_queue *queu
 static void
 write_unique (struct line const *line, FILE *tfp, char const *temp_output)
 {
-  static struct line saved;
 
   if (unique)
     {
-      if (saved.text && ! compare (line, &saved))
+      if (saved_line.text && ! compare (line, &saved_line))
         return;
-      saved = *line;
+      saved_line = *line;
     }
 
   write_line (line, tfp, temp_output);
@@ -4451,6 +4453,7 @@ sort (char *const *files, size_t nfiles,
               break;
             }
 
+          saved_line.text = NULL;
           line = buffer_linelim (&buf);
           if (buf.eof && !nfiles && !ntemps && !buf.left)
             {
diff -urNp coreutils-8.17-orig/tests/misc/sort coreutils-8.17/tests/misc/sort
--- coreutils-8.17-orig/tests/misc/sort	2012-02-03 10:22:06.000000000 +0100
+++ coreutils-8.17/tests/misc/sort	2012-08-20 13:31:38.685565488 +0200
@@ -226,6 +226,10 @@ my @Tests =
 ["15c", '-i -u', {IN=>"a\1\na\n"}, {OUT=>"a\1\n"}],
 ["15d", '-i -u', {IN=>"\1a\na\n"}, {OUT=>"\1a\n"}],
 ["15e", '-i -u', {IN=>"a\n\1\1\1\1\1a\1\1\1\1\n"}, {OUT=>"a\n"}],
+# Before 8.19, this would trigger a free-memory read.
+["unique-free-mem-read", '-u --p=1 -S32b',
+ {IN=>"a\n"."b"x900 ."\n"},
+ {OUT=>"a\n"."b"x900 ."\n"}],
 
 # From Erick Branderhorst -- fixed around 1.19e
 ["16a", '-f',