Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Any way to simulate a Windows path handling for File::Spec without Windows?

by davido (Cardinal)
on Sep 08, 2019 at 20:38 UTC ( [id://11105843]=note: print w/replies, xml ) Need Help??


in reply to Any way to simulate a Windows path handling for File::Spec without Windows?

This isn't the point to your post but I wanted to mention a possible bug I noticed:

my ($new_string, $longest_string) = '';

This probably ought to be my ($new_string, $longest_string) = ('','');, because later you're using $longest_string as a pattern with the s/$longest_string// construct. But being undefined is the least of your worries:

#!usr/bin/env perl use strict; use warnings; use File::Temp qw(tempdir); use File::Spec::Functions qw(catfile); my $dir = tempdir('pmtest_XXXXX', TMPDIR => 1, CLEANUP => 1); my $test_filename = '{a}'; my $full_path = catfile($dir, $test_filename); open my $fh, '>', $full_path or die "Cannot create $full_path: $!\n"; print $fh "Hello world.\n"; close $fh; print "We created $full_path as a file.\n" if -e $full_path && -f _; my $target = "$full_path.extra"; $target =~ s/$full_path//;

...produces...

We created /tmp/pmtest_hmfIn/{a} as a file. Unescaped left brace in regex is passed through in regex; marked by <- +- HERE in m//tmp/pmtest_hmfIn/{ <-- HERE a}/ at mytest.pl line 23.

If you use a part of a path as a regular expression pattern that pattern has all the semantics of a regular expression pattern. You probably want to use quotemeta or \Q, to play it a little safer. Otherwise, you're exposing the regex engine to user input, which should be considered hostile. One could create a path that results in a pattern that either fails to parse, or that parses but has abysmal performance.


Dave

Replies are listed 'Best First'.
Re^2: Any way to simulate a Windows path handling for File::Spec without Windows?
by nysus (Parson) on Sep 08, 2019 at 22:41 UTC

    Thanks, yeah, I caught the regex bug and fixed it in a release earlier today. Thanks for the not on multiple assignment. I'll fix that up.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-23 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found