[Haskell-beginners] Reading a list of null-terminated filenames from stdin?

Harald Hanche-Olsen hanche at math.ntnu.no
Sun Oct 11 09:22:34 UTC 2015


Andrew Bernard wrote:
> Hi Brandon,
>
> It may be valid, but it is just asking for trouble with many tools and utilities and scripts. Funny, I had to check re UNIX. I have been programming UNIX systems for over thirty years and never even imagined a newline in a filename!
>
> Would the OP say why he needs to use newlines in filenames? Something best avoided. I suppose this is not a Haskell matter, but one does have to ask.

The OP must speak for himself, but as far as I am concerned, no 
reasonable person uses newlines in filenames, ever.

However, the possibility is there, and it may happen that someone 
unreasonable has created a filename with a newline in it. This may 
become a security issue if, for example, someone creates a file named

"/tmp/foo
/etc/passwd
bar.log"

and a careless system person runs a script as root to remove all files 
named *.log from /tmp/ and subdirectories.

find /tmp -name '*.log' -print | xargs rm

is an insanely reckless way to do it, which in this case would cause the 
removal of the password file. Much better:

find /tmp -name '*.log' -type f -print0 | xargs -0 rm

or even better:

find /tmp -name '*.log' -type f -exec rm {} +

In summary, if you have to encode a list of filenames in a byte stream, 
doing it with zero terminated is the correct way to do it.

– Harald


More information about the Beginners mailing list