From 25eb4c69097ca4f5665b050cfa4247a19ffd8c55 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 22 Apr 2009 08:45:27 +0200 Subject: sort -m: don't segfault when output file is also an input file * src/sort.c (avoid_trashing_input): Fix an off-by-one error and guard the use of memmove. * NEWS (Bug fixes): Mention it. * tests/misc/sort: Add tests to exercise the offending code. * THANKS: Update. Reported by Otavio Salvador in http://bugs.debian.org/525048. --- tests/misc/sort | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/misc/sort b/tests/misc/sort index 4f377df26..a27f47ad5 100755 --- a/tests/misc/sort +++ b/tests/misc/sort @@ -1,6 +1,6 @@ #!/usr/bin/perl -# Copyright (C) 2008 Free Software Foundation, Inc. +# Copyright (C) 2008, 2009 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 @@ -339,6 +339,21 @@ my @Tests = ["sort-numeric", '--sort=numeric', {IN=>".01\n0\n"}, {OUT=>"0\n.01\n"}], ["sort-gennum", '--sort=general-numeric', {IN=>"1e2\n2e1\n"}, {OUT=>"2e1\n1e2\n"}], + +# -m with output file also used as an input file +# In coreutils-7.2, this caused a segfault. +# This test looks a little strange. Here's why: +# since we're using "-o f", standard output will be empty, hence OUT=>'' +# We still want to ensure that the output file, "f" has expected contents, +# hence the added CMP=> directive. +["output-is-input", '-m -o f', {IN=> {f=> "a\n"}}, {OUT=>''}, + {CMP=> ["a\n", {'f'=> undef}]} ], +["output-is-input-2", '-m -o f', {OUT=>''}, + {IN=> {f=> "a\n"}}, {IN=> {g=> "b\n"}}, {IN=> {h=> "c\n"}}, + {CMP=> ["a\nb\nc\n", {'f'=> undef}]} ], +["output-is-input-3", '-m -o f', {OUT=>''}, + {IN=> {g=> "a\n"}}, {IN=> {h=> "b\n"}}, {IN=> {f=> "c\n"}}, + {CMP=> ["a\nb\nc\n", {'f'=> undef}]} ], ); # Add _POSIX2_VERSION=199209 to the environment of each test -- cgit v1.2.3-70-g09d2