[Bug 1367928] Open() fails if filename ends in a space

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



https://bugzilla.redhat.com/show_bug.cgi?id=1367928

Petr Pisar <ppisar@xxxxxxxxxx> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |CLOSED
         Resolution|---                         |NOTABUG
        Last Closed|                            |2016-08-30 11:00:10



--- Comment #1 from Petr Pisar <ppisar@xxxxxxxxxx> ---
Checking what the code does on operating level shows:

$ strace -e open ./test 
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libperl.so.5.22", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libnsl.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libcrypt.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libutil.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libfreebl3.so", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("/dev/urandom", O_RDONLY)          = 3
open("./test", O_RDONLY)                = 3
open("/usr/share/perl5/strict.pm", O_RDONLY) = 4
open("/usr/share/perl5/warnings.pm", O_RDONLY) = 4
open("t", O_RDONLY)                     = -1 ENOENT (No such file or directory)
whoops: No such file or directory at ./test line 5.
+++ exited with 2 +++

So the problem is that the trailing white space is removed.

Reading open() documentation in perlfunc POD revelads this is a feature:

            The filename passed to the one- and two-argument forms of open()
            will have leading and trailing whitespace deleted and normal
            redirection characters honored. This property, known as "magic
            open", can often be used to good effect. A user could specify a
            filename of "rsh cat file |", or you could change certain
            filenames as needed:

                $filename =~ s/(.*\.gz)\s*$/gzip -dc < $1|/;
                open(FH, $filename) or die "Can't open $filename: $!";

            Use the three-argument form to open a file with arbitrary weird
            characters in it,

                open(FOO, "<", $file)
                    || die "can't open < $file: $!";

            otherwise it's necessary to protect any leading and trailing
            whitespace:

                $file =~ s#^(\s)#./$1#;
                open(FOO, "< $file\0")
                    || die "open failed: $!";


Therefore I recommend you to use 3-argument form with explicit open mode like
this:

$o = open($fh, '<', $file)|| die 'whoops';

-- 
You are receiving this mail because:
You are on the CC list for the bug.
--
Fedora Extras Perl SIG
http://www.fedoraproject.org/wiki/Extras/SIGs/Perl
perl-devel mailing list
perl-devel@xxxxxxxxxxxxxxxxxxxxxxx
https://lists.fedoraproject.org/admin/lists/perl-devel@xxxxxxxxxxxxxxxxxxxxxxx




[Index of Archives]     [Fedora Announce]     [Fedora Kernel]     [Fedora Testing]     [Fedora Legacy Announce]     [Fedora PHP Devel]     [Kernel Devel]     [Fedora Legacy]     [Fedora Maintainers]     [Fedora Desktop]     [PAM]     [Red Hat Development]     [Big List of Linux Books]     [Gimp]     [Yosemite Information]
  Powered by Linux