diff --git a/lib/Data/MessagePack/PP.pm b/lib/Data/MessagePack/PP.pm index 5d85c31..2d1f3ab 100644 --- a/lib/Data/MessagePack/PP.pm +++ b/lib/Data/MessagePack/PP.pm @@ -234,7 +234,7 @@ sub _pack { if ( $flags & B::SVp_POK ) { # raw / check needs before double if ( $self->{prefer_integer} ) { - if ( $value =~ /^-?[0-9]+$/ ) { # ok? + if ( $value =~ /^(0|-?[1-9][0-9]*)$/ ) { # ok? # checks whether $value is in (u)int32 my $ivalue = 0 + $value; if (!( diff --git a/t/05_preferred_int.t b/t/05_preferred_int.t index c1db9f3..4c68e8a 100644 --- a/t/05_preferred_int.t +++ b/t/05_preferred_int.t @@ -41,8 +41,10 @@ my @dat = ( '', 'c4 00', '0', '00', '1', '01', + '01', 'c4 02 30 31', '10', '0a', '-1', 'ff', + '-0', 'c4 02 2d 30', '-10', 'f6', '-', 'c4 01 2d', ''.0xEFFF => 'cd ef ff', diff --git a/xs-src/pack.c b/xs-src/pack.c index f09a747..15d9219 100644 --- a/xs-src/pack.c +++ b/xs-src/pack.c @@ -131,6 +131,8 @@ STATIC_INLINE int try_int(enc_t* enc, const char *p, size_t len) { if (len > 10) { return 0; } } + if (*p == '0' && len != 1) { return 0; } + #if '9'=='8'+1 && '8'=='7'+1 && '7'=='6'+1 && '6'=='5'+1 && '5'=='4'+1 \ && '4'=='3'+1 && '3'=='2'+1 && '2'=='1'+1 && '1'=='0'+1 do {