Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Filenames named "..\n"

by zzspectrez (Hermit)
on Aug 02, 2005 at 05:45 UTC ( [id://480083]=perlquestion: print w/replies, xml ) Need Help??

zzspectrez has asked for the wisdom of the Perl Monks concerning the following question:

In this node it was mentioned to me that the following code I wrote:

  • my @files = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -f "$dir/$_" } @tmp;

Will needlessly ignore filenames named "..\n". This would be obviously true, since I am matching the end with $ that matches the end of a string before a newline. So to fix this I believe changing the code to:

  • my @files = map { "$dir/$_" } grep { !/^\.{1,2}\z/ && -f "$dir/$_" } @tmp;

should fix it by matching at the end of the string.

However, where exactly would this be a bug?? This would only be a bug on systems that support a newline in the filename. On windows, ( only system I have access to at the moment ) this is not an issue. You cannot create a filename with a "\n" within it.

Example

use strict; use warnings; my $filename1 = "ap\ndd"; my $filename2 = "..\n"; open (OUT, '>', $filename1) or die "Error opening file [$filename] for write: $! -- $^E\n"; print OUT "TEST\n\nTEST\n"; close(OUT) or die "Error closing file [$filename]: $!\n";

Either filename will give an error:

C:\src\re\test.pl Error opening file [.. ] for write: Invalid argument -- The filename, directory name, or volu +me label syntax is incorrect
. So at least in windows this would not be a bug, since you would never encounter such a filename.

Are there many systems that support such filenames?? I just can't see this being encountered often..

Am I missing something here??

zzSPECTREz

Replies are listed 'Best First'.
Re: Filenames named "..\n" (unix)
by tye (Sage) on Aug 02, 2005 at 05:50 UTC

    Some inferior operating systems, including most based on Unix, allow all kinds of problems by allowing rediculous characters (everything but "\0" and "/", for example) in file names, leading to all manner of silly exploits and paranoid coding practices.

    - tye        

      Given this, would it not be more of a feature to ignore a filename named "..\n" instead of a bug. If someone bothered to embed a "\n" in the filename I probably dont want to process it anyways!? :)

        Exploits often justify paranoia...

        You wouldn't want your script that looks for set-UID exploits to miss one because the cracker put it in a file named "..\n".

        - tye        

      Yeah, using whitespace in a filename can cause a problem for a sloppy programmer. That's why no (well, most) Unix users will never create such files. The only problems are caused by file systems that are cross mounted between Unix and Windows systems, with the Windows people created files with spaces in them.

      Curse them.

        No, curse the sloppy programmers who assumed users wouldn't want to use spaces in filenames. Blame the programmer. I was astonished and annoyed that I had to create a symlink from ~/My Video to ~/.MyVideo just so I could allow dvdrip to function. The dumb writer thought I shouldn't have spaces in my directories.
Re: Filenames named "..\n"
by Anonymous Monk on Aug 02, 2005 at 08:41 UTC
    I either write the test as:
    grep {!/^\./}
    if I want to ignore all file (and directories) that start with a dot (like ls without the -a or -A option), or write it as
    grep {$_ ne "." && $_ ne ".."}
    which is a lot clearer than /^\.\.?\z/. It's longer, but anyone immediately sees you are excluding two names, and two names only.
Re: Filenames named "..\n"
by spiritway (Vicar) on Aug 02, 2005 at 07:26 UTC

    I just ran across this very issue in Programming Perl, where it says:

    "...(Yes, using a newline as a file extension Isn't Very Nice, but that doesn't mean it can't happen)."

    It seems likely that someone, somewhere, either got bored or wanted to make a little mischief, and created a filename as described. It only takes one, to break your code if you don't allow for it...

Re: Filenames named "..\n"
by Anonymous Monk on Aug 02, 2005 at 08:35 UTC
    Whether or not a filename may contain a newline is the property of the filesystem, not the OS. A Unix system can make a directory available as a Samba share, which can be mounted from a Windows machine. So you can encounter a file with a newline in them on a Windows machine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://480083]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-24 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found