summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2009-04-22 08:45:27 +0200
committerJim Meyering <meyering@redhat.com>2009-04-23 21:03:14 +0200
commit25eb4c69097ca4f5665b050cfa4247a19ffd8c55 (patch)
tree0b17c83b2d096a6c3a0fd9bb15322fdbf25af46e /src
parentf41f926aab38d52734b3908eeb0496ebf1285cbd (diff)
downloadcoreutils-25eb4c69097ca4f5665b050cfa4247a19ffd8c55.tar.xz
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.
Diffstat (limited to 'src')
-rw-r--r--src/sort.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/sort.c b/src/sort.c
index 2e6ce877d..f48d727df 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1,5 +1,5 @@
/* sort - sort lines of text (with all kinds of options).
- Copyright (C) 1988, 1991-2008 Free Software Foundation, Inc.
+ Copyright (C) 1988, 1991-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
@@ -2602,18 +2602,20 @@ avoid_trashing_input (struct sortfile *files, size_t ntemps,
pid_t pid;
char *temp = create_temp (&tftp, &pid);
size_t num_merged = 0;
- while (i + num_merged < nfiles)
+ do
{
num_merged += mergefiles (&files[i], 0, nfiles - i, tftp, temp);
files[i].name = temp;
files[i].pid = pid;
- memmove(&files[i], &files[i + num_merged],
- num_merged * sizeof *files);
+ if (i + num_merged < nfiles)
+ memmove(&files[i + 1], &files[i + num_merged],
+ num_merged * sizeof *files);
ntemps += 1;
nfiles -= num_merged - 1;;
i += num_merged;
}
+ while (i < nfiles);
}
}