develooper Front page | perl.perl5.porters | Postings from April 2016

Re: [perl #127663] Safety for -i option

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
April 5, 2016 14:54
Subject:
Re: [perl #127663] Safety for -i option
Message ID:
20160405145443.GS4160@iabyn.com
On Mon, Mar 07, 2016 at 04:10:03PM -0800, Tony Cook via RT wrote:
> On Sun Mar 06 02:32:46 2016, jiangyy@outlook.com wrote:
> > the file-system runs out of space (when the replaced text is longer)
> 
> It isn't necessary for the replaced text to be longer.
> 
> We're unlinking the file, but keeping a file handle open to it.  On a
> POSIX system the file will continue to take space until the file handle
> is closed.

For anyone following this ticket, a simple demonstration of why -i is
currently unsafe:

Here foo gets completely truncated:

    $ echo "hello" > foo; ./perl -i -pe'die' foo
    Died at -e line 1, <> line 1.
    $ ls -l foo
    -rw-rw-r--. 1 davem davem 0 Apr  5 15:49 foo
    $ 
    
and here foo gets partially truncated:

    $ perl -le'print "a" x 80 for 1..10_000' > foo
    $ ls -l foo
    -rw-rw-r--. 1 davem davem 810000 Apr  5 15:51 foo
    $ ./perl -i -pe'die if $. == 9_900' foo
    Died at -e line 1, <> line 9900.
    $ ls -l foo
    -rw-rw-r--. 1 davem davem 801819 Apr  5 15:52 foo
    $




-- 
It's not that I'm afraid to die, I just don't want to be there when it
happens.
    -- Woody Allen

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About