| 1 | package Perl::Critic::Policy::Community::OpenArgs; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant DESC => 'open() called with less than 3 arguments'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant EXPL => 'The one- and two-argument forms of open() parse functionality from the filename, use the three-argument form instead.'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | sub default_severity { $SEVERITY_MEDIUM } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub default_themes { 'community' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | sub applies_to { 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 21 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 21 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 21 | ▶ | return () unless $elem eq 'open' and is_function_call $elem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$elem eq "open" and is_function_call($elem)
MC/DC50%1/2
$elem eq "open" and is_function_call($elem)
$elem eq "open"
is_function_call($elem)
Truth table66%2/3
A and B
A= $elem eq "open"B= is_function_call($elem)
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | my @args = parse_arg_list $elem; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | ▶ | if (@args < 3) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | return () if @args == 2 and $args[1][0]->isa('PPI::Token::Quote') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@args == 2 and $args[1][0]->isa("PPI::Token::Quote") and $args[1][0]->string =~ /^(?:-\||\|-)\z/
@args == 2 and $args[1][0]->isa("PPI::Token::Quote")
MC/DC66%2/3
@args == 2 and $args[1][0]->isa("PPI::Token::Quote") and $args[1][0]->string =~ /^(?:-\||\|-)\z/
@args == 2
$args[1][0]->isa("PPI::Token::Quote")
$args[1][0]->string =~ /^(?:-\||\|-)\z/
Truth table75%3/4
A and B and C
A= @args == 2B= $args[1][0]->isa("PPI::Token::Quote")C= $args[1][0]->string =~ /^(?:-\||\|-)\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | and $args[1][0]->string =~ /^(?:-\||\|-)\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | return $self->violation(DESC, EXPL, $elem); | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Perl::Critic::Policy::Community::OpenArgs - Always use the three-argument form | ||||||||||||||||||||||||||||||||||||||||||
| 38 | of open | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | The C<open()> function may be called in a two-argument form where the filename | ||||||||||||||||||||||||||||||||||||||||||
| 43 | is parsed to determine the mode of opening, which may include piping input or | ||||||||||||||||||||||||||||||||||||||||||
| 44 | output. (In the one-argument form, this filename is retrieved from a global | ||||||||||||||||||||||||||||||||||||||||||
| 45 | variable, but the same magic is used.) This can lead to vulnerabilities if the | ||||||||||||||||||||||||||||||||||||||||||
| 46 | filename is retrieved from user input or could begin or end with a special | ||||||||||||||||||||||||||||||||||||||||||
| 47 | character. The three-argument form specifies the open mode as the second | ||||||||||||||||||||||||||||||||||||||||||
| 48 | argument, so it is always distinct from the filename. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | open FILE; # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 51 | open my $fh, "<$filename"; # not ok | ||||||||||||||||||||||||||||||||||||||||||
| 52 | open my $fh, '<', $filename; # ok | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | This policy is similar to the core policy | ||||||||||||||||||||||||||||||||||||||||||
| 55 | L<Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen>, but additionally | ||||||||||||||||||||||||||||||||||||||||||
| 56 | prohibits one-argument opens. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | This policy is part of L<Perl::Critic::Community>. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | This policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 75 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | L<Perl::Critic> | ||||||||||||||||||||||||||||||||||||||||||