oalders / html-restrict

HTML::Restrict - Strip away unwanted HTML tags

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uninitialized warnings

isync opened this issue · comments

commented

Probably by calling your module from really messy code on my end, I manage to get warnings from HTML::Restrict (v2.5.0) in logs, running on Perl v5.28.1:

(WARN) Use of uninitialized value in logical or (||) at /usr/share/perl5/HTML/Restrict.pm line 343.
(WARN) Use of uninitialized value $_[0] in return at /usr/share/perl5/HTML/Restrict.pm line 343.

I've looked into the (more up to date v3.0.0), where the problem still persists, and see the root in sub process (~line 300):

return if !@;
return $
[0] if !$_[0];

And although I don't see why Perl is complaining about "logical OR" here... (obviously I'm a noob), I think the error could be suppressed by handling the various return cases more strictly, for example by adding "return undef unless $_[0]" or so. Would that be safe?

Please excuse me not diving into understanding your module more and providing a proper patch, but could be it's a quick tweak for you. Thank you for your Module and effort nevertheless! Really appreciated! Long live Perl and its great community!

Thanks for this! Before I dive in, do you have a snippet of code I could use to demonstrate the problem?

commented

Well, at least not yet. I still don't know exactly which code portion is triggering this, or when. At least I can tell that I'm calling process() on pretty standard text scalars. Could be that some of these calls pass an undef or empty value.
The Object is initialized pretty standard, either with the first or the second rule set:

my $html_restrict = HTML::Restrict->new();
my $html_restrict_less = HTML::Restrict->new( rules => {
	a	=> ['href','target','rel'],
	b	=> [],
	br	=> [],
	em	=> [],
	i	=> [],
	li	=> [],
	ol	=> [],
	p	=> ['style'],
	span	=> ['style'],
	div	=> ['style','id'],
	strong	=> [],
	u	=> [],
	ul	=> [],
});

Not very helpful, right? I know, sorry!, everyone dreads such issues. I could understand if you just leave it at "can't reproduce" ;) Thanks anyway, already!

I'm not sure what's going on here. With the latest version, this is not triggering any warnings:

$ git diff
diff --git a/t/00-load.t b/t/00-load.t
index b173d58..09a89ac 100644
--- a/t/00-load.t
+++ b/t/00-load.t
@@ -1,13 +1,12 @@
 #!perl

-use Test::More;
-
 use strict;
 use warnings;

-use Data::Dump;
-use HTML::Restrict;
-use Scalar::Util;
+use HTML::Restrict ();
+use Scalar::Util ();
+use Test::More;
+use Test::Warnings;

 my $version = $HTML::Restrict::VERSION || 'development';
 diag( "Testing HTML::Restrict $version, Perl $], $^X" );
@@ -22,6 +21,13 @@ my $default_rules = $hr->get_rules;
 cmp_ok( Scalar::Util::reftype( $default_rules ),
     'eq', 'HASH', "default rules are empty" );

+# undef handling
+$hr->process(undef);
+$hr->process('');
+$hr->process(0);
+$hr->process();
+$hr->process(' ');
commented

Hi Olaf,
thank you for looking into this!
The gist of my results: I also can't reproduce the warnings with these tests.

I've run the above "undef tests" on the machine where I saw the warnings. Also, I've used my rules-set. Stil, no warnings. Even more spooky, I've looked through logs of the last couple of days and haven't seen the warnings anymore. Code is in progress, so could be there was some intermittent hickup that produced the warnings.

As such, I'd label the issue as "can't reproduce" and close it for now.
Thanks again!

Thanks for the update @isync! Feel free to re-open this if you find it's a problem again. :)