GNU bug report logs - #18449
"cat x >> x" error even when x is empty

Previous Next

Package: coreutils;

Reported by: Vincent Lefevre <vincent <at> vinc17.net>

Date: Thu, 11 Sep 2014 13:02:02 UTC

Severity: normal

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#18449: closed ("cat x >> x" error even when x is empty)
Date: Thu, 11 Sep 2014 15:53:01 +0000
[Message part 1 (text/plain, inline)]
Your message dated Thu, 11 Sep 2014 08:52:17 -0700
with message-id <5411C531.60305 <at> cs.ucla.edu>
and subject line Re: bug#18449: "cat x >> x" error even when x is empty
has caused the debbugs.gnu.org bug report #18449,
regarding "cat x >> x" error even when x is empty
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
18449: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18449
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Vincent Lefevre <vincent <at> vinc17.net>
To: bug-coreutils <at> gnu.org
Subject: "cat x >> x" error even when x is empty
Date: Thu, 11 Sep 2014 15:00:44 +0200
With coreutils 8.23 under Debian/unstable:

ypig% : > x
ypig% cat x >> x
cat: x: input file is output file
ypig% POSIXLY_CORRECT=1 cat x >> x
cat: x: input file is output file

while there's no reason to return an error in this case: the file
should just remain empty. Using the same file for input and output
isn't disallowed by POSIX, AFAIK.

This may not seem really useful here, but this can potentially break
scripts with things like:

  cat "$foo" >> "$bar"

where "$foo" may be the same file as "$bar" only if it is empty.

BTW, when x isn't empty, I wonder whether an error is correct if
POSIXLY_CORRECT is set. The result will typically depend on the
implementation and possibly be non-deterministic, but POSIX doesn't
seem to allow an error (except FS errors, such as disk full).

-- 
Vincent Lefèvre <vincent <at> vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


[Message part 3 (message/rfc822, inline)]
From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Eric Blake <eblake <at> redhat.com>, Pádraig Brady
 <P <at> draigBrady.com>, Vincent Lefevre <vincent <at> vinc17.net>, 
 18449-done <at> debbugs.gnu.org
Subject: Re: bug#18449: "cat x >> x" error even when x is empty
Date: Thu, 11 Sep 2014 08:52:17 -0700
[Message part 4 (text/plain, inline)]
Eric Blake wrote:
> Testing for an empty file is enough of an additional special case over
> the existing check for same files that I don't think it is worth it.

It's a really cheap check, as a system call is needed only when the 
input and output files are the same, and even then it's only an lseek 
with SEEK_CUR so it's all in-memory.  Plus, the POSIX 'cat' spec gives 
an example of using 'cat' to copy an empty regular file to itself, and 
(not unreasonably) says that should work.

As far as POSIX conformance goes, I don't think we need to worry about 
POSIXLY_CORRECT here.  We'll get POSIX fixed instead.  Eric has already 
started the ball rolling on that (thanks).  Though the POSIX fix should 
involve updating its example -- and quite possibly POSIX should continue 
to allow catting an empty file to itself.

I installed the attached patch and am marking this as done.  The 'cat' 
source code is simpler now, so that's a win anyway....
[0001-cat-allow-copying-empty-files-to-themselves.patch (text/plain, attachment)]

This bug report was last modified 10 years and 251 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.