Fix Encode::encode_utf8(undef) to return undef

This commit is contained in:
Petr Písař 2016-09-30 10:36:38 +02:00
parent ab747f6aed
commit b6a844858c
2 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,73 @@
From 646aaae364fc8cd19786a66b88ec6aaf3f093024 Mon Sep 17 00:00:00 2001
From: Pali <pali@cpan.org>
Date: Thu, 11 Aug 2016 23:09:26 +0200
Subject: [PATCH] Fix return value of Encode::encode_utf8(undef)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Documentation says that '$octets = encode_utf8($string)' is equivalent to
'$octets = encode("utf8", $string)'. And if the $string is undef, then
undef is returned. However without this patch encode_utf8(undef) returned
'' (empty string) and not undef. This patch fixes it and undef is returned.
All other utf8 Encode calls already returns undef, just encode_utf8() acted
differently.
Encode::encode('utf8', undef) -> undef
Encode::decode('utf8', undef) -> undef
Encode::decode_utf8(undef) -> undef
Reported bug: https://rt.cpan.org/Public/Bug/Display.html?id=116904
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
Encode.pm | 1 +
t/utf8ref.t | 15 ++++++++++++++-
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/Encode.pm b/Encode.pm
index bda8e1b..bc600be 100644
--- a/Encode.pm
+++ b/Encode.pm
@@ -254,6 +254,7 @@ sub from_to($$$;$) {
sub encode_utf8($) {
my ($str) = @_;
+ return undef unless defined $str;
utf8::encode($str);
return $str;
}
diff --git a/t/utf8ref.t b/t/utf8ref.t
index 3253e08..aff098f 100644
--- a/t/utf8ref.t
+++ b/t/utf8ref.t
@@ -6,7 +6,7 @@ use strict;
use warnings;
use Encode;
use Test::More;
-plan tests => 4;
+plan tests => 12;
#plan 'no_plan';
# my $a = find_encoding('ASCII');
@@ -18,3 +18,16 @@ is $u->encode($r), '';
$r = {};
is decode_utf8($r), ''.$r;
is $u->decode($r), '';
+use warnings 'uninitialized';
+
+is encode_utf8(undef), undef;
+is decode_utf8(undef), undef;
+
+is encode_utf8(''), '';
+is decode_utf8(''), '';
+
+is Encode::encode('utf8', undef), undef;
+is Encode::decode('utf8', undef), undef;
+
+is Encode::encode('utf8', ''), '';
+is Encode::decode('utf8', ''), '';
--
2.7.4

View File

@ -8,7 +8,7 @@ Version: %{cpan_version}
# perl-encoding sub-package has independent version which does not change # perl-encoding sub-package has independent version which does not change
# often and consecutive builds would clash on perl-encoding NEVRA. This is the # often and consecutive builds would clash on perl-encoding NEVRA. This is the
# same case as in perl.spec. # same case as in perl.spec.
Release: 2%{?dist} Release: 3%{?dist}
Summary: Character encodings in Perl Summary: Character encodings in Perl
# ucm: UCD # ucm: UCD
# bin/encguess: Artistic 2.0 # bin/encguess: Artistic 2.0
@ -17,6 +17,9 @@ License: (GPL+ or Artistic) and Artistic 2.0 and UCD
Group: Development/Libraries Group: Development/Libraries
URL: http://search.cpan.org/dist/Encode/ URL: http://search.cpan.org/dist/Encode/
Source0: http://www.cpan.org/authors/id/D/DA/DANKOGAI/Encode-%{cpan_version}.tar.gz Source0: http://www.cpan.org/authors/id/D/DA/DANKOGAI/Encode-%{cpan_version}.tar.gz
# Fix Encode::encode_utf8(undef) to return undef, CPAN RT#116904,
# in upstream after 2.86
Patch0: Encode-2.86-Fix-return-value-of-Encode-encode_utf8-undef.patch
BuildRequires: coreutils BuildRequires: coreutils
BuildRequires: findutils BuildRequires: findutils
BuildRequires: make BuildRequires: make
@ -127,6 +130,7 @@ your own encoding to perl. No knowledge of XS is necessary.
%prep %prep
%setup -q -n Encode-%{cpan_version} %setup -q -n Encode-%{cpan_version}
%patch0 -p1
%build %build
# Additional scripts can be installed by appending MORE_SCRIPTS, UCM files by # Additional scripts can be installed by appending MORE_SCRIPTS, UCM files by
@ -168,6 +172,9 @@ make test
%{perl_vendorarch}/Encode/encode.h %{perl_vendorarch}/Encode/encode.h
%changelog %changelog
* Fri Sep 30 2016 Petr Pisar <ppisar@redhat.com> - 4:2.86-3
- Fix Encode::encode_utf8(undef) to return undef (CPAN RT#116904)
* Fri Sep 16 2016 Petr Pisar <ppisar@redhat.com> - 4:2.86-2 * Fri Sep 16 2016 Petr Pisar <ppisar@redhat.com> - 4:2.86-2
- Add Artistic 2.0 into license tag because of encguess tool - Add Artistic 2.0 into license tag because of encguess tool