Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect from undef values in Mojo::File objects #1393

Merged
merged 1 commit into from
Jul 31, 2019
Merged

Conversation

kraih
Copy link
Member

@kraih kraih commented Jul 31, 2019

It is currently very easy to delete the wrong directory by accident, like:

my $subdir = undef;
path('usr', 'share', $subdir)->remove_tree;

This would currently delete usr/share and only show a pretty insignificant warning about the undef value. I noticed the problem because we had a similar accident at work. 😉

$ perl -Mojo -E 'n { f("foo", "bar", "baz")->child("yada") } 1000000'
2.67053 wallclock secs ( 2.67 usr +  0.00 sys =  2.67 CPU) @ 374531.84/s (n=1000000)

$ perl -Ilib -Mojo -E 'n { f("foo", "bar", "baz")->child("yada") } 1000000'
2.80627 wallclock secs ( 2.81 usr +  0.00 sys =  2.81 CPU) @ 355871.89/s (n=1000000)

The performance cost is pretty minimal. I would have liked to include the empty string too, but unfortunately we depend on that currently for a Mojo::Home hack that i don't dare to change again.

@kraih
Copy link
Member Author

kraih commented Jul 31, 2019

I'm not 100% sure if it is worth the performance cost, but i'm giving it a 👍 personally and call for a vote anyway @mojolicious/core.

@kraih kraih added the vote label Jul 31, 2019
@christopherraa
Copy link
Member

If I had voting rights I'd be 👍 for this. I've fallen into this trap myself.

@marcusramberg
Copy link
Member

Im 👍

Sent with GitHawk

@jberger
Copy link
Member

jberger commented Jul 31, 2019

👍

lib/Mojo/File.pm Outdated
@@ -24,7 +24,7 @@ our @EXPORT_OK = ('path', 'tempdir', 'tempfile');

sub basename { File::Basename::basename ${shift()}, @_ }

sub child { $_[0]->new(@_) }
sub child { $_[0]->new(${shift()}, @_) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've already voted in favor but can you explain the significance of this line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally insignificant. It's an optimization that's already in master actually. 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes ->child calls a lot faster though since it avoids stringification later.

@kraih kraih merged commit ec0590b into master Jul 31, 2019
@kraih kraih deleted the no_undef_path branch September 10, 2019 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants