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

[perl #127663] Safety for -i option

From:
Tony Cook via RT
Date:
June 21, 2016 05:50
Subject:
[perl #127663] Safety for -i option
Message ID:
rt-4.0.18-23400-1466488247-991.127663-15-0@perl.org
On Tue Apr 05 07:55:14 2016, davem wrote:
> 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
>     $

One problem I have with this example is I'm not sure die should be treated as a failure case.

Should a similar case where exit() is called instead of die() revert any edits?

If not, I don't see a reliable mechanism to distinguish the two.

For the standard -n or -p generated loop it's fine because the user can expect iterating to the next in-place file will close the old ARGVOUT and do whatever extra cleanup is needed to replace the input file with the output (nothing currently, a rename for my working branch), but what if the user "last"s out of the inplace loop for some reason?

Tony


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=127663



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