I came across a bug in HTTP::Message::content_charset. It has a ‘local $_’, which is unnecessary, since foreach loops already localise their topic. In fact, they do it in a safer way that is more like local *_= \do{my$x}. Using local $_ causes problems if $_ is tied. The attached patch removes the offending line and adds a test for it. This did actually occur in real code, and is not just a theoretical problem.
diff -Nurp libwww-perl-5.834-Yk3Kpv/lib/HTTP/Message.pm 
libwww-perl-5.834-Yk3Kpvcopy/lib/HTTP/Message.pm
--- libwww-perl-5.834-Yk3Kpv/lib/HTTP/Message.pm        2009-11-21 
04:57:55.000000000 -0800
+++ libwww-perl-5.834-Yk3Kpvcopy/lib/HTTP/Message.pm    2010-01-03 
11:25:13.000000000 -0800
@@ -205,7 +205,6 @@ sub content_charset
     my $cref = $self->decoded_content(ref => 1, charset => "none");
 
     # Unicode BOM
-    local $_;
     for ($$cref) {
        return "UTF-8"     if /^\xEF\xBB\xBF/;
        return "UTF-32-LE" if /^\xFF\xFE\x00\x00/;
diff -Nurp libwww-perl-5.834-Yk3Kpv/t/base/message-charset.t 
libwww-perl-5.834-Yk3Kpvcopy/t/base/message-charset.t
--- libwww-perl-5.834-Yk3Kpv/t/base/message-charset.t   2009-06-25 
12:38:55.000000000 -0700
+++ libwww-perl-5.834-Yk3Kpvcopy/t/base/message-charset.t       2010-01-03 
11:20:33.000000000 -0800
@@ -15,7 +15,7 @@ BEGIN {
 }
 
 use Test;
-plan tests => 21;
+plan tests => 22;
 
 use HTTP::Response;
 my $r = HTTP::Response->new(200, "OK");
@@ -91,3 +91,13 @@ $r->content(<<'EOT');
 encoding="US-ASCII" ?>
 EOT
 ok($r->content_charset, "US-ASCII");
+
+{
+ sub TIESCALAR{bless[]}
+ tie $_, "";
+ my $fail = 0;
+ sub STORE{ ++$fail }
+ sub FETCH{}
+ $r->content_charset;
+ ok($fail, 0, 'content_charset leaves $_ alone');
+}

Reply via email to