]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
With -Dlint, make shuf free all heap-allocated storage.
authorJim Meyering <jim@meyering.net>
Fri, 23 Feb 2007 14:18:16 +0000 (15:18 +0100)
committerJim Meyering <jim@meyering.net>
Fri, 23 Feb 2007 14:18:16 +0000 (15:18 +0100)
* src/shuf.c (main): Move declaration of input_lines to
function scope, and initialize to NULL, so we can free it.
[lint]: Free all malloc'd memory.
* tests/misc/shuf: Also test shuf's -e and -i options.

ChangeLog
src/shuf.c
tests/misc/shuf

index 660eb5166ba42ccd6fe0fadaf55bf884265be36c..ed6d2bca3d48700623140befd529115f0bcae5c1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2007-02-23  Jim Meyering  <jim@meyering.net>
+
+       With -Dlint, make shuf free all heap-allocated storage.
+       * src/shuf.c (main): Move declaration of input_lines to
+       function scope, and initialize to NULL, so we can free it.
+       [lint]: Free all malloc'd memory.
+       * tests/misc/shuf: Also test shuf's -e and -i options.
+
 2007-02-22  Jim Meyering  <jim@meyering.net>
 
        Merge in a change from some other incarnation of this file (gzip?)
index 68003c2593657f6d6c13f7ec2bfc1ae5be9284e4..bfc9f30a27678bfbcd0f4715d170cc641ae03553 100644 (file)
@@ -1,6 +1,6 @@
 /* Shuffle lines of text.
 
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -251,6 +251,7 @@ main (int argc, char **argv)
   char const *outfile = NULL;
   char *random_source = NULL;
   char eolbyte = '\n';
+  char **input_lines = NULL;
 
   int optc;
   int n_operands;
@@ -258,7 +259,7 @@ main (int argc, char **argv)
   size_t n_lines;
   char **line;
   struct randint_source *randint_source;
-  size_t const *permutation;
+  size_t *permutation;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
@@ -366,8 +367,6 @@ main (int argc, char **argv)
     }
   else
     {
-      char **input_lines;
-
       switch (n_operands)
        {
        case 0:
@@ -408,5 +407,15 @@ main (int argc, char **argv)
   if (write_permuted_output (head_lines, line, lo_input, permutation) != 0)
     error (EXIT_FAILURE, errno, _("write error"));
 
+#ifdef lint
+  free (permutation);
+  randint_all_free (randint_source);
+  if (input_lines)
+    {
+      free (input_lines[0]);
+      free (input_lines);
+    }
+#endif
+
   return EXIT_SUCCESS;
 }
index 760bd4921908021c507d4c6cfd376a6239f7e074..a2d5891b23c56f4991fa516913253c40580ab6e2 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 # Ensure that shuf randomizes its input.
 
-# Copyright (C) 2006 Free Software Foundation, Inc.
+# Copyright (C) 2006, 2007 Free Software Foundation, Inc.
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -51,8 +51,18 @@ cmp in out > /dev/null && { fail=1; echo "not random?" 1>&2; }
 sort -n out > out1
 cmp in out1 || { fail=1; echo "not a permutation" 1>&2; }
 
+# Exercise shuf's -i option.
+shuf -i 1-100 > out || fail=1
+cmp in out > /dev/null && { fail=1; echo "not random?" 1>&2; }
+sort -n out > out1
+cmp in out1 || { fail=1; echo "not a permutation" 1>&2; }
+
+# Exercise shuf's -e option.
+t=`shuf -e a b c d e | sort | fmt`
+test "$t" = 'a b c d e' || { fail=1; echo "not a permutation" 1>&2; }
+
 # Before coreutils-6.3, this would infloop.
-# "seq 1860" produces 8193 bytes of output.
+# "seq 1860" produces 8193 (8K + 1) bytes of output.
 seq 1860 | shuf > /dev/null || fail=1
 
 (exit $fail); exit $fail