Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Expat/Expat.pm
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ sub expand_ns_prefix {
return ( defined($stack) and @$stack ) ? $stack->[-1] : undef;
}

return undef;
return;
}

sub current_ns_prefixes {
Expand Down Expand Up @@ -676,7 +676,7 @@ sub children {
if ( defined $children ) {
return @$children;
}
return undef;
return;
}

sub asString {
Expand Down
63 changes: 63 additions & 0 deletions t/return_undef_list_context.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use strict;
use warnings;

use Test::More tests => 6;
use XML::Parser;

# ContentModel::children() should return an empty list when there are
# no children (EMPTY/ANY models), not (undef). Using "return undef"
# in a list-returning method is a classic Perl footgun: it produces a
# one-element list containing undef instead of an empty list.
#
# This means code like:
# my @kids = $model->children;
# if (@kids) { ... } # incorrectly true for EMPTY models
# would silently process undef as a child element.

my %models;

my $p = XML::Parser->new(
Handlers => {
Element => sub { $models{ $_[1] } = $_[2] },
},
);

$p->parse(<<'XML');
<?xml version="1.0"?>
<!DOCTYPE doc [
<!ELEMENT empty EMPTY>
<!ELEMENT any ANY>
<!ELEMENT seq (a,b)>
]>
<doc/>
XML

# EMPTY model has no children
{
my $m = $models{empty};
ok( $m->isempty, 'EMPTY model detected' );

my @kids = $m->children;
is( scalar @kids, 0,
'children() returns empty list for EMPTY model' );
}

# ANY model has no children
{
my $m = $models{any};
ok( $m->isany, 'ANY model detected' );

my @kids = $m->children;
is( scalar @kids, 0,
'children() returns empty list for ANY model' );
}

# SEQ model has children — should still work
{
my $m = $models{seq};
ok( $m->isseq, 'SEQ model detected' );

my @kids = $m->children;
is( scalar @kids, 2,
'children() returns correct count for SEQ model' );
}
Loading