blib/lib/Perl/Critic/Policy/InputOutput/RequireEncodingWithUTF8Layer.pm

filter: Statementstmt 63.0% 29 / 46 Branchbran 6.2% 1 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.5% 11 / 14 Podpod 80.0% 4 / 5 totaltotal 52.3% 45 / 86 CC 10 SCAR 23.4
CC
10
Cov
46%
_get_argument_string
30.0
_recover_open_encoding
12.0
violates
10.2
CRAP 10.4
« blib/lib/Perl/Critic/Policy/NamingConventions/Capitalization.pm ↑ Summary blib/lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm »
1 package Perl::Critic::Policy::InputOutput::RequireEncodingWithUTF8Layer;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw{ :severities :ppi };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{I/O layer ":utf8" used};
17 40 Readonly::Scalar my $EXPL => q{Use ":encoding(UTF-8)" to get strict validation};
18  
19 40 Readonly::Scalar my $THREE_ARGUMENT_OPEN => 3;
20 40 Readonly::Hash my %RECOVER_ENCODING => (
21 binmode => \&_recover_binmode_encoding,
22 open => \&_recover_open_encoding,
23 );
24  
25 #-----------------------------------------------------------------------------
26  
27 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
28 75 ▶sub default_severity { return $SEVERITY_HIGHEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
29 74 ▶sub default_themes { return qw(core bugs security) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
30 37 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
31  
32 #-----------------------------------------------------------------------------
33  
34 sub violates {
35 361 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=4 SCAR=23.2
Pod documented
36  
37 361 ▶ my $handler = $RECOVER_ENCODING{ $elem->content() }
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $RECOVER_ENCODING{$elem->content} or return 361 0 361
38 or return; # If we don't have a handler, we're not interested.
39 0 ▶ my $encoding = $handler->( parse_arg_list( $elem ) )
Branch: 1 branch
BranchTrueFalseTotal
my $encoding = &$handler(parse_arg_list($elem)) or return 0 0 0
40 or return; # If we can't recover an encoding, we give up.
41 0 ▶ return if $encoding !~ m/ (?: \A | : ) utf8 \b /smxi; # OK
Branch: 1 branch
BranchTrueFalseTotal
unless $encoding =~ / (?: \A | : ) utf8 \b /imsx 0 0 0
42  
43 0 return $self->violation( $DESC, $EXPL, $elem );
44 }
45  
46 #-----------------------------------------------------------------------------
47  
48 # my $string = _get_argument_string( $arg[1] );
49 #
50 # This subroutine returns the string from the given argument (which must
51 # be a reference to an array of PPI objects), _PROVIDED_ the array
52 # contains a single PPI::Token::Quote object. Otherwise it simply
53 # returns, since we're too stupid to analyze anything else.
54  
55 sub _get_argument_string {
56 0 ▶ my ( $arg ) = @_;
Subroutine _get_argument_string: not called CC=5 SCAR=34.0
57 0 ▶ ref $arg eq 'ARRAY' or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq "ARRAY" or return 0 0 0
58 0 ▶ return if @{ $arg } == 0 || @{ $arg } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$arg;} == 0 or @{$arg;} > 1 0 0 0
Condition0%0/3
@{$arg;} == 0 or @{$arg;} > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$arg;} == 0 or @{$arg;} > 1
@{$arg;} == 0 @{$arg;} > 1
Truth table0%0/3
A or B
A= @{$arg;} == 0B= @{$arg;} > 1
ABresult
1X1
011
000
59 0 ▶ return $arg->[0]->string() if $arg->[0]->isa( 'PPI::Token::Quote' );
Branch: 1 branch
BranchTrueFalseTotal
if $arg->[0]->isa("PPI::Token::Quote") 0 0 0
60 0 return;
61 }
62  
63 #-----------------------------------------------------------------------------
64  
65 # my $encoding = _recover_binmode_encoding( _parse_arg_list( $elem ) );
66 #
67 # This subroutine returns the encoding specified by the given $elem,
68 # which _MUST_ be the 'binmode' of a binmode() call.
69  
70 sub _recover_binmode_encoding {
71 0 ▶ my ( @args ) = @_;
Subroutine _recover_binmode_encoding: not called CC=1 SCAR=6.9
72 0 return _get_argument_string( $args[1] );
73 }
74  
75 #-----------------------------------------------------------------------------
76  
77 # my $encoding = _recover_open_encoding( _parse_arg_list( $elem ) );
78 #
79 # This subroutine returns the encoding specified by the given $elem,
80 # which _MUST_ be the 'open' of an open() call.
81  
82 sub _recover_open_encoding {
83 0 ▶ my ( @args ) = @_;
Subroutine _recover_open_encoding: not called CC=3 SCAR=24.8
84 0 ▶ @args < $THREE_ARGUMENT_OPEN
Branch: 1 branch
BranchTrueFalseTotal
@args < $THREE_ARGUMENT_OPEN and return 0 0 0
85 and return;
86 0 ▶ defined( my $string = _get_argument_string( $args[1] ) )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $string = _get_argument_string($args[1])) or return 0 0 0
87 or return;
88 0 $string =~ s/ [+]? (?: < | >{1,2} ) //smx;
89 0 return $string;
90 }
91  
92 40 1;
93  
94 __END__
95  
96 #-----------------------------------------------------------------------------
97  
98 =pod
99  
100 =for stopwords PerlIO PerlMonks Wiki
101  
102 =head1 NAME
103  
104 Perl::Critic::Policy::InputOutput::RequireEncodingWithUTF8Layer - Write C<< open $fh, q{<:encoding(UTF-8)}, $filename; >> instead of C<< open $fh, q{<:utf8}, $filename; >>.
105  
106 =head1 AFFILIATION
107  
108 This Policy is part of the core L<Perl::Critic|Perl::Critic>
109 distribution.
110  
111  
112 =head1 DESCRIPTION
113  
114 Use of the C<:utf8> I/O layer (as opposed to C<:encoding(UTF8)> or
115 C<:encoding(UTF-8)>) was suggested in the Perl documentation up to
116 version 5.8.8. This may be OK for output, but on input C<:utf8> does not
117 validate the input, leading to unexpected results.
118  
119 An exploit based on this behavior of C<:utf8> is exhibited on PerlMonks
120 at L<http://www.perlmonks.org/?node_id=644786>. The exploit involves a
121 string read from an external file and sanitized with C<m/^(\w+)$/>,
122 where C<$1> nonetheless ends up containing shell meta-characters.
123  
124 To summarize:
125  
126 open $fh, '<:utf8', 'foo.txt'; # BAD
127 open $fh, '<:encoding(UTF8)', 'foo.txt'; # GOOD
128 open $fh, '<:encoding(UTF-8)', 'foo.txt'; # BETTER
129  
130 See the L<Encode|Encode> documentation for the difference between
131 C<UTF8> and C<UTF-8>. The short version is that C<UTF-8> implements the
132 Unicode standard, and C<UTF8> is liberalized.
133  
134 For consistency's sake, this policy checks files opened for output as
135 well as input. For complete coverage it also checks C<binmode()> calls,
136 where the direction of operation can not be determined.
137  
138  
139 =head1 CONFIGURATION
140  
141 This Policy is not configurable except for the standard options.
142  
143  
144 =head1 NOTES
145  
146 Because C<Perl::Critic> does a static analysis, this policy can not
147 detect cases like
148  
149 my $encoding = ':utf8';
150 binmode $fh, $encoding;
151  
152 where the encoding is computed.
153  
154  
155 =head1 SEE ALSO
156  
157 L<PerlIO|PerlIO>
158  
159 L<Encode|Encode>
160  
161 C<perldoc -f binmode>
162  
163 L<http://www.socialtext.net/perl5/index.cgi?the_utf8_perlio_layer>
164  
165 L<http://www.perlmonks.org/?node_id=644786>
166  
167 =head1 AUTHOR
168  
169 Thomas R. Wyant, III F<wyant at cpan dot org>
170  
171 =head1 COPYRIGHT
172  
173 Copyright (c) 2010-2011 Thomas R. Wyant, III
174  
175 This program is free software; you can redistribute it and/or modify
176 it under the same terms as Perl itself.
177  
178 =cut
179  
180 # Local Variables:
181 # mode: cperl
182 # cperl-indent-level: 4
183 # fill-column: 78
184 # indent-tabs-mode: nil
185 # c-indentation-style: bsd
186 # End:
187 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/NamingConventions/Capitalization.pm ↑ Summary blib/lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm »