a command with pitfalls
Hi there,
I just came across a buggy (not quite in some sense) command that I feel obligatory to address to you, hoping you will be interested in fixing it if it's deemed a necessity.
Here is how it went:
1) I grabbed some content into a file:
mtr www.google.com >> /tmp/test.txt
2) out of curiosity, I typed:
cat < /tmp/test.txt >> /tmp/test.txt
3) then, insanity kicked off:
in the blink, the file "/tmp/test.txt" went up to a 1GB big file. If it was terminated immediately, I believe for a while longer the whole hard disc could have been quickly stuffed up and caused nothing to work.
I wonder if that's intentionally left unattended or it's just not been discovered yet.
On Ubuntu, at least when typing " cat < FILENAME >> FILENAME", it will complain the input and output use the same file, no matter what its intent is, to prevent a suicide-like command.
Let me know how you will get on with that.
Cheers,
Victor Zhong
Responses
Definitely a bug: I know on older versions of Solaris (last time I made that mistake), if you tried to do something like that, it would typically error out because you attempted to open a file simultaneously with conflicting open-methods.
As to the ginormous file: are you sure it was actually consuming space on disk? Seems like there'd be a decent chance that all you'd actually end up doing is creating a sparsefile.
Created a file on RedHat 6.8 with one line ( "1\n") and used strace:
fstat(1, {st_mode=S_IFREG|0664, st_size=2, ...}) = 0
fstat(0, {st_mode=S_IFREG|0664, st_size=2, ...}) = 0
read(0, "1\n", 32768) = 2
write(1, "1\n", 2) = 2
read(0, "1\n", 32768) = 2
write(1, "1\n", 2) = 2
read(0, "1\n", 32768) = 2
write(1, "1\n", 2) = 2
It looks like cat does not decide it has reached end of file when it reads less data than requested. And the written file does consume space on disk.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
