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

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

Thread Previous | Thread Next
From:
Yanyan Jiang
Date:
March 7, 2016 04:18
Subject:
Re: [perl #127663] Safety for -i option
Message ID:
BLU436-SMTP231B992E18AB0B2BA55888FB7B00@phx.gbl
> Can you supply us with: (a) the list of commands you invoked at the command-line to get these results; (b) some idea of the size of the file in question relative to the size of memory?

For perl, I just used a simple case of in-place text replacement:

    perl5.22.1 -i -pe 's/old/new/g’ file.txt

I get the system-call trace via

    strace COMMAND

The file is small (just kilobytes). If the program terminates just after unlink(), the file is gone. I simulated this process by killing it immediately after unlink(), and the file is indeed gone. If the file contents are huge, the overwrite itself can cause inconsistency (the first half is updated, the second half is old, and there are some corruptions in the middle). 

> Can you elaborate on the portable solutions that gtk and qt provide?

Sed just uses rename() to replace the file with a temporary one, seems it is assuming a POSIX runtime, and this is POSIX safe. Gtk provides g_file_replace(), and Qt provides QSaveFile. Both are portable. We extensively tested these two implementations, and they are both safe in handling file overwrite. 

We believe that perl is an extremely portable software, and semantics of rename() may be different on other platforms, and this shall be handled with care (though I’m not an expert on portability).
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