coreutils-8.22-shuf-segfault.patch 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. From 24eb395471176e24762b08bfcef7562911537504 Mon Sep 17 00:00:00 2001
  2. From: Paul Eggert <eggert@cs.ucla.edu>
  3. Date: Sun, 23 Feb 2014 15:34:48 -0800
  4. Subject: [PATCH] shuf: with -r, don't dump core if the input is empty
  5. Problem reported by valiant xiao in <http://bugs.gnu.org/16855>.
  6. * NEWS: Document this.
  7. * src/shuf.c (main): With -r, report an error if the input is empty.
  8. * tests/misc/shuf.sh: Test for the bug.
  9. ---
  10. NEWS | 3 +++
  11. src/shuf.c | 15 +++++++++++----
  12. tests/misc/shuf.sh | 4 ++++
  13. 3 files changed, 18 insertions(+), 4 deletions(-)
  14. diff --git a/NEWS b/NEWS
  15. index e72942b..2df246d 100644
  16. --- a/NEWS
  17. +++ b/NEWS
  18. @@ -22,6 +22,9 @@ GNU coreutils NEWS -*- outline -*-
  19. it would display an error, requiring --no-dereference to avoid the issue.
  20. [bug introduced in coreutils-5.3.0]
  21. + shuf -r no longer dumps core if the input is empty.
  22. + [bug introduced in coreutils-8.22]
  23. +
  24. ** New features
  25. od accepts a new option: --endian=TYPE to handle inputs with different byte
  26. diff --git a/src/shuf.c b/src/shuf.c
  27. index d4641fe..2a91072 100644
  28. --- a/src/shuf.c
  29. +++ b/src/shuf.c
  30. @@ -576,11 +576,18 @@ main (int argc, char **argv)
  31. /* Generate output according to requested method */
  32. if (repeat)
  33. {
  34. - if (input_range)
  35. - i = write_random_numbers (randint_source, head_lines,
  36. - lo_input, hi_input, eolbyte);
  37. + if (head_lines == 0)
  38. + i = 0;
  39. else
  40. - i = write_random_lines (randint_source, head_lines, line, n_lines);
  41. + {
  42. + if (n_lines == 0)
  43. + error (EXIT_FAILURE, 0, _("No lines to repeat"));
  44. + if (input_range)
  45. + i = write_random_numbers (randint_source, head_lines,
  46. + lo_input, hi_input, eolbyte);
  47. + else
  48. + i = write_random_lines (randint_source, head_lines, line, n_lines);
  49. + }
  50. }
  51. else
  52. {
  53. diff --git a/tests/misc/shuf.sh b/tests/misc/shuf.sh
  54. index d3ea1f2..d7251d1 100755
  55. --- a/tests/misc/shuf.sh
  56. +++ b/tests/misc/shuf.sh
  57. @@ -43,6 +43,10 @@ compare in out1 || { fail=1; echo "not a permutation" 1>&2; }
  58. t=$(shuf -e a b c d e | sort | fmt)
  59. test "$t" = 'a b c d e' || { fail=1; echo "not a permutation" 1>&2; }
  60. +# coreutils-8.22 dumps core.
  61. +shuf -er
  62. +test $? -eq 1 || fail=1
  63. +
  64. # Before coreutils-6.3, this would infloop.
  65. # "seq 1860" produces 8193 (8K + 1) bytes of output.
  66. seq 1860 | shuf > /dev/null || fail=1
  67. --
  68. 1.8.5.3