Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an edge case of non-Int being wrongly validated as Int #103

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions t/101_issues/Int.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Mouse::Util::TypeConstraints;

my $Int = find_type_constraint 'Int';

subtest "Non-Int from numerical literal: my \$num = 3.14", sub {
my $num = 3.14;
ok !$Int->check($num), "\$num is not Int";
{ no warnings; int($num) };
ok !$Int->check($num), "\$num is still not Int";
};

subtest "Non-Int from string literal: my \$num = \"3.14\"", sub {
my $num = "3.14";
ok !$Int->check($num), "\$num is not Int";
{ no warnings; int($num) };
ok !$Int->check($num), "\$num is still not Int";
};

subtest "Int from string literal: my \$num = \"3\"", sub {
my $num = "3";
ok $Int->check($num), "\$num is Int";
{ no warnings; int($num) };
ok $Int->check($num), "\$num is still Int";
};

subtest "Int from integer literal: my \$num = 3", sub {
my $num = 3;
ok $Int->check($num), "\$num is Int";
{ no warnings; int($num) };
ok $Int->check($num), "\$num is still Int";
};

subtest "MAXUINT", sub {
my $maxuint = ~0;
ok $Int->check( $maxuint ), 'yes MAXUINT';
my $as_string = sprintf '%f', $maxuint;
ok $Int->check( $maxuint ), 'yes MAXUINT after use as float';
};

done_testing;
11 changes: 8 additions & 3 deletions xs-src/MouseTypeConstraints.xs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,21 @@ S_nv_is_integer(pTHX_ NV const nv) {
int
mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
assert(sv);
if(SvPOKp(sv)){

if(SvPOK(sv)){
int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
return num_type && !(num_type & IS_NUMBER_NOT_INT);
}
else if(SvIOKp(sv)){
else if(SvIOK(sv)){
return TRUE;
}
else if(SvNOKp(sv)) {
else if(SvNOK(sv)) {
return S_nv_is_integer(aTHX_ SvNVX(sv));
}
else if (SvOK(sv) && (SvMAGIC(sv) || SvTYPE(sv) == SVt_PVMG) && SvIOKp(sv)) {
return TRUE;
}

return FALSE;
}

Expand Down