diff options
author | Chris 'BinGOs' Williams <chris@bingosnet.co.uk> | 2014-11-07 11:37:34 +0000 |
---|---|---|
committer | Chris 'BinGOs' Williams <chris@bingosnet.co.uk> | 2014-11-07 12:48:51 +0000 |
commit | 44f85850d172082a150f79f5a9bdcfe9d9c59d84 (patch) | |
tree | bdbfc797a49bef3119b6c1fb0790c2bc0580d86f /cpan | |
parent | ac681bb324daa37001a1a03002e1d02efbaf7fcd (diff) | |
download | perl-44f85850d172082a150f79f5a9bdcfe9d9c59d84.tar.gz |
Update Encode to CPAN version 2.64
[DELTA]
$Revision: 2.64 $ $Date: 2014/10/29 15:37:54 $
! t/utf8warnings.t MANIFEST
Retouch pull #26 so it works with perl < 5.14
! Encode.pm
+ t/utf8warnings.t
Pulled: Catch and re-issue utf8 warnings at a higher level
https://github.com/dankogai/p5-encode/pull/26
+ Encode.xs
Pulled: Validate continuations in the incremental UTF-X decoder
https://github.com/dankogai/p5-encode/pull/25
Diffstat (limited to 'cpan')
-rw-r--r-- | cpan/Encode/Encode.pm | 34 | ||||
-rw-r--r-- | cpan/Encode/Encode.xs | 12 | ||||
-rw-r--r-- | cpan/Encode/t/utf8warnings.t | 66 |
3 files changed, 104 insertions, 8 deletions
diff --git a/cpan/Encode/Encode.pm b/cpan/Encode/Encode.pm index 03eded648e..820d6f76c1 100644 --- a/cpan/Encode/Encode.pm +++ b/cpan/Encode/Encode.pm @@ -1,10 +1,10 @@ # -# $Id: Encode.pm,v 2.63 2014/10/19 07:02:18 dankogai Exp $ +# $Id: Encode.pm,v 2.64 2014/10/29 15:37:54 dankogai Exp dankogai $ # package Encode; use strict; use warnings; -our $VERSION = sprintf "%d.%02d", q$Revision: 2.63 $ =~ /(\d+)/g; +our $VERSION = sprintf "%d.%02d", q$Revision: 2.64 $ =~ /(\d+)/g; use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG}; use XSLoader (); XSLoader::load( __PACKAGE__, $VERSION ); @@ -156,7 +156,20 @@ sub encode($$;$) { require Carp; Carp::croak("Unknown encoding '$name'"); } - my $octets = $enc->encode( $string, $check ); + # For Unicode, warnings need to be caught and re-issued at this level + # so that callers can disable utf8 warnings lexically. + my $octets; + if ( ref($enc) eq 'Encode::Unicode' ) { + my $warn = ''; + { + local $SIG{__WARN__} = sub { $warn = shift }; + $octets = $enc->encode( $string, $check ); + } + warnings::warnif('utf8', $warn) if length $warn; + } + else { + $octets = $enc->encode( $string, $check ); + } $_[1] = $string if $check and !ref $check and !( $check & LEAVE_SRC() ); return $octets; } @@ -172,7 +185,20 @@ sub decode($$;$) { require Carp; Carp::croak("Unknown encoding '$name'"); } - my $string = $enc->decode( $octets, $check ); + # For Unicode, warnings need to be caught and re-issued at this level + # so that callers can disable utf8 warnings lexically. + my $string; + if ( ref($enc) eq 'Encode::Unicode' ) { + my $warn = ''; + { + local $SIG{__WARN__} = sub { $warn = shift }; + $string = $enc->decode( $octets, $check ); + } + warnings::warnif('utf8', $warn) if length $warn; + } + else { + $string = $enc->decode( $octets, $check ); + } $_[1] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() ); return $string; } diff --git a/cpan/Encode/Encode.xs b/cpan/Encode/Encode.xs index 755d6d910d..32be9b8ab7 100644 --- a/cpan/Encode/Encode.xs +++ b/cpan/Encode/Encode.xs @@ -1,5 +1,5 @@ /* - $Id: Encode.xs,v 2.30 2014/10/19 07:01:15 dankogai Exp $ + $Id: Encode.xs,v 2.31 2014/10/29 15:37:54 dankogai Exp dankogai $ */ #define PERL_NO_GET_CONTEXT @@ -343,10 +343,14 @@ process_utf8(pTHX_ SV* dst, U8* s, U8* e, SV *check_sv, if (UTF8_IS_START(*s)) { U8 skip = UTF8SKIP(s); if ((s + skip) > e) { - /* Partial character */ - /* XXX could check that rest of bytes are UTF8_IS_CONTINUATION(ch) */ - if (stop_at_partial || (check & ENCODE_STOP_AT_PARTIAL)) + if (stop_at_partial || (check & ENCODE_STOP_AT_PARTIAL)) { + const U8 *p = s + 1; + for (; p < e; p++) { + if (!UTF8_IS_CONTINUATION(*p)) + goto malformed_byte; + } break; + } goto malformed_byte; } diff --git a/cpan/Encode/t/utf8warnings.t b/cpan/Encode/t/utf8warnings.t new file mode 100644 index 0000000000..9d93ecea54 --- /dev/null +++ b/cpan/Encode/t/utf8warnings.t @@ -0,0 +1,66 @@ +use strict; +use warnings; +BEGIN { + if ($] < 5.014){ + print "1..0 # Skip: Perl 5.14.0 or later required\n"; + exit 0; + } +} + +use Encode; +use Test::More tests => 7; + +my $valid = "\x61\x00\x00\x00"; +my $invalid = "\x78\x56\x34\x12"; + +my @warnings; +$SIG{__WARN__} = sub {push @warnings, "@_"}; + +my $enc = find_encoding("UTF32-LE"); + +{ + @warnings = (); + my $ret = Encode::Unicode::decode( $enc, $valid ); + is("@warnings", "", "Calling decode in Encode::Unicode on valid string produces no warnings"); +} + +{ + @warnings = (); + my $ret = Encode::Unicode::decode( $enc, $invalid ); + like("@warnings", qr/is not Unicode/, "Calling decode in Encode::Unicode on invalid string warns"); +} + +{ + no warnings 'utf8'; + @warnings = (); + my $ret = Encode::Unicode::decode( $enc, $invalid ); + is("@warnings", "", "Warning from decode in Encode::Unicode can be silenced via no warnings 'utf8'"); +} + +{ + no warnings; + @warnings = (); + my $ret = Encode::Unicode::decode( $enc, $invalid ); + is("@warnings", "", "Warning from decode in Encode::Unicode can be silenced via no warnings"); +} + +{ + @warnings = (); + my $ret = Encode::decode( $enc, $invalid ); + like("@warnings", qr/is not Unicode/, "Calling decode in Encode on invalid string warns"); +} + +{ + no warnings 'utf8'; + @warnings = (); + my $ret = Encode::decode( $enc, $invalid ); + is("@warnings", "", "Warning from decode in Encode can be silenced via no warnings 'utf8'"); +}; + +{ + no warnings; + @warnings = (); + my $ret = Encode::decode( $enc, $invalid ); + is("@warnings", "", "Warning from decode in Encode can be silenced via no warnings 'utf8'"); +}; + |