[PATCH] sort -m: don't segfault when output file is also an input file
Thanks to Otavio Salvador for finding/reporting this. Here's the patch I'm considering: >From 570beb56f58bb087a614af885bec7e9cf6b19423 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 22 Apr 2009 08:45:27 +0200 Subject: [PATCH] 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. --- NEWS|6 +- THANKS |1 + src/sort.c | 10 ++ tests/misc/sort | 17 - 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 5951bb5..ee44a23 100644 --- a/NEWS +++ b/NEWS @@ -1,9 +1,13 @@ GNU coreutils NEWS-*- outline -*- -* Noteworthy changes in release ?.? (-??-??) [?] +* Noteworthy changes in release 7.3 (-??-??) [?] ** Bug fixes + sort -m no longer segfaults when its output file is also an input file. + E.g., with this, touch 1; sort -m -o 1 1, sort would segfault. + [introduced in coreutils-7.2] + ls now aligns output correctly in the presence of abbreviated month names from the locale database that have differing widths. diff --git a/THANKS b/THANKS index fe523fe..876a6b6 100644 --- a/THANKS +++ b/THANKS @@ -435,6 +435,7 @@ Ole Laursen o...@hardworking.dk Oliver Kiddle okid...@yahoo.co.uk Ørn E. Hansen oehan...@daimi.aau.dk Oskar Liljeblad o...@hem.passagen.se +Otavio Salvador ota...@ossystems.com.br Pádraig Brady p...@draigbrady.com Patrick Mauritz oxyg...@studentenbude.ath.cx Paul D. Smith psm...@gnu.org diff --git a/src/sort.c b/src/sort.c index 2e6ce87..f48d727 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); } } diff --git a/tests/misc/sort b/tests/misc/sort index 4f377df..a27f47a 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 -- 1.6.3.rc1.205.g37f8 ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
Re: Arbitrary hunk sizes for patch
>> When modifying patch files, i'm getting a bit sick of counting lines >> just >> so I can modify hunk sizes to correctly apply a patch. > > Wrong list. You want to ask this on bug-patch (the address listed in > 'patch --help'). However, 'git apply', which is very similar to patch, > has already implemented this concept, in its --recount option, so it seems > like a reasonable request. Thanks Eric. Will cross posting. ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
Re: Arbitrary hunk sizes for patch
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 According to Alfie on 4/21/2009 6:55 PM: > Hi list, > > When modifying patch files, i'm getting a bit sick of counting lines just > so I can modify hunk sizes to correctly apply a patch. Wrong list. You want to ask this on bug-patch (the address listed in 'patch --help'). However, 'git apply', which is very similar to patch, has already implemented this concept, in its --recount option, so it seems like a reasonable request. - -- Don't work too hard, make some time for fun as well! Eric Blake e...@byu.net -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (Cygwin) Comment: Public key at home.comcast.net/~ericblake/eblake.gpg Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAknupmsACgkQ84KuGfSFAYD21QCeMElnu4oQ4gM314l6haKH9Iba w3kAn0thWNqisOrD9cMHO/BnG4xspQbs =O5st -END PGP SIGNATURE- ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
Arbitrary hunk sizes for patch
Hi list, When modifying patch files, i'm getting a bit sick of counting lines just so I can modify hunk sizes to correctly apply a patch. Then I noticed at the bottom of the man page for "patch": "Paul Eggert removed patchs arbitrary limits" Has that statement got anything to do with arbitrary hunk sizes? If so, could this be reinstated but only turned on with a flag e.g. "-a or --abitrary-size" Alfie ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
Re: chroot documentation
On Tue, Apr 21, 2009 at 6:35 PM, Stefano Carucci wrote: > > Hello all! > > Can anyone suggest to me a detailed guide about the chroot implementation ? The implementation in coreutils or the one in your kernel?The implementation of chroot(1) in coreurils looks pretty much like this: chroot("the-specified-directory") chdir("/") execvp(your_program_name, argument_list); If that looks very short, well, it is. The coreutils implementation of chroot is only 115 lines, including header comment, option parsing, and error handling. > What I am interested in is how it creates the new root, It doesn't. The directory to which you chroot must already exist. > what the computational effort is Minimal. # /usr/bin/time -v chroot /var/vserver/chroot/debian/etch/x86/a /bin/true Command being timed: "chroot /var/vserver/chroot/debian/etch/x86/a /bin/true" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 266% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 0 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 293 Voluntary context switches: 3 Involuntary context switches: 0 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0 > and what it does at a low-level; not just a synopsis. If you want to know what it does at a low level, you are better off enquiring into the properties of the implementation, not the command-line tool that thinly wraps the system call. You might want to download the sources for Linux or for some *BSD kernel or some such thing, in order to understand how the system call works. But this mailing list isn't really the right place to enquire about the internals of those kernels, since we don't write them. James. ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
chroot documentation
Hello all! Can anyone suggest to me a detailed guide about the chroot implementation ? What I am interested in is how it creates the new root, what the computational effort is and what it does at a low-level; not just a synopsis. Thank you in advance. Stefano _ Quante ne sai? Scoprilo con CrossWire! http://clk.atdmt.com/GBL/go/140630367/direct/01/___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
7.2 on Solaris 10 with Zones
Just built 7.2 on Solaris 10/SPARC and src/df doesn't seem to like zones: $ src/df ... src/df: `/var/zones/poog/root/.SUNWnative/lib': Permission denied src/df: `/var/zones/poog/root/.SUNWnative/platform': Permission denied src/df: `/var/zones/poog/root/.SUNWnative/usr': Permission denied src/df: `/var/zones/poog/root/dev': Permission denied src/df: `/var/zones/poog/root/opt/build': Permission denied src/df: `/var/zones/poog/root/proc': Permission denied src/df: `/var/zones/poog/root/etc/mnttab': Permission denied src/df: `/var/zones/poog/root/etc/svc/volatile': Permission denied src/df: `/var/zones/poog/root/dev/ksyms': Permission denied src/df: `/var/zones/poog/root/dev/fd': Permission denied ... When run as root, I get: $ src/df ... src/df: `/var/zones/poog/root/opt/dist': Not owner src/df: `/var/zones/poog/root/opt/tww': Not owner src/df: `/var/zones/poog/root/home': Not owner src/df: `/var/zones/poog/root/opt/src': Not owner src/df: `/var/zones/poog/root/opt/src/latest': Not owner ... $ grep /var/zones/poog/root/.SUNWnative/lib /etc/mnttab /lib/var/zones/poog/root/.SUNWnative/liblofs ro,nodevices,dev=80 1240195097 $ grep /var/zones/poog/root/opt/build /etc/mnttab /dev/dsk/c1t1d0s4 /var/zones/poog/root/opt/build ufs rw,intr,largefiles,logging,xattr,onerror=panic,dev=8c 1240195097 $ grep /var/zones/gax/root/home /etc/mnttab sanji:/home /var/zones/gax/root/homenfs rw,nosuid,hard,intr,bg,zone=gax,dev=5a40643 1240271940 -- albert chin (ch...@thewrittenword.com) ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
Re: factor is too fast
On Tue, 21 Apr 2009, Toralf F?rster wrote: For a long time I used the command "factor" to test my system WRT the cpu ondemand governor of the linux kernel, eg for issues like this : http://bugzilla.kernel.org/show_bug.cgi?id=12385 However switching from coreutils-6.10 to 7.1 (stable Gentoo Linux) now the factor command is too fast: it takes only 0.003 sec instead of 5.5 sec for the same prime number. That's probably due to this entry from NEWS: * Noteworthy changes in release 7.0 (2008-10-05) [beta] If the GNU MP library is available at configure time, factor and expr support arbitrarily large numbers. Pollard's rho algorithm is used to factor large numbers. Therefore I'm wondering whether you have a hint for me which number I could use nowadays ? If the goal is simply drive the CPU usage to 100% for 5 seconds, this would work: $ timeout 5 factor 20158916322613169725842061629370496430 Cheers, Phil ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils
factor is too fast
For a long time I used the command "factor" to test my system WRT the cpu ondemand governor of the linux kernel, eg for issues like this : http://bugzilla.kernel.org/show_bug.cgi?id=12385 However switching from coreutils-6.10 to 7.1 (stable Gentoo Linux) now the factor command is too fast: it takes only 0.003 sec instead of 5.5 sec for the same prime number. Therefore I'm wondering whether you have a hint for me which number I could use nowadays ? :-) Thx -- MfG/Sincerely Toralf Förster pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3 ___ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils