diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index b54874d5..8c85458e 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -126,7 +126,7 @@ pack_(List) when is_list(List) -> pack_({Map}) when is_list(Map) -> pack_map(Map); pack_(Other) -> - throw({error, {badarg, Other}}). + throw({badarg, Other}). -spec pack_uint_(non_neg_integer()) -> binary(). @@ -387,4 +387,9 @@ benchmark_test()-> {Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)), ?debugFmt("for ~p KB test data.", [byte_size(S) div 1024]). +error_test()-> + ?assertEqual({error,{badarg, atom}}, msgpack:pack(atom)), + Term = {"hoge", "hage", atom}, + ?assertEqual({error,{badarg, Term}}, msgpack:pack(Term)). + -endif. diff --git a/perl/Changes b/perl/Changes index f32d5f49..13fc98b5 100644 --- a/perl/Changes +++ b/perl/Changes @@ -1,3 +1,8 @@ +0.14 + + - fixed segv on serializing cyclic reference + (Dan Kogai) + 0.13 - clearly specify requires_c99(), because msgpack C header requires C99. diff --git a/perl/lib/Data/MessagePack.pm b/perl/lib/Data/MessagePack.pm index ee07d35f..3c38a795 100644 --- a/perl/lib/Data/MessagePack.pm +++ b/perl/lib/Data/MessagePack.pm @@ -4,7 +4,7 @@ use warnings; use XSLoader; use 5.008001; -our $VERSION = '0.13'; +our $VERSION = '0.14'; our $PreferInteger = 0; our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" }; diff --git a/perl/pack.c b/perl/pack.c index 589cee8b..af6669cd 100644 --- a/perl/pack.c +++ b/perl/pack.c @@ -49,6 +49,8 @@ static void need(enc_t *enc, STRLEN len); # error "msgpack only supports IVSIZE = 8,4,2 environment." #endif +#define ERR_NESTING_EXCEEDED "perl structure exceeds maximum nesting level (max_depth set too low?)" + static void need(enc_t *enc, STRLEN len) { @@ -146,9 +148,10 @@ static int try_int(enc_t* enc, const char *p, size_t len) { } -static void _msgpack_pack_rv(enc_t *enc, SV* sv); +static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth); -static void _msgpack_pack_sv(enc_t *enc, SV* sv) { +static void _msgpack_pack_sv(enc_t *enc, SV* sv, int depth) { + if (!depth) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); if (sv==NULL) { @@ -171,7 +174,7 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv) { } else if (SvIOKp(sv)) { PACK_IV(enc, SvIV(sv)); } else if (SvROK(sv)) { - _msgpack_pack_rv(enc, SvRV(sv)); + _msgpack_pack_rv(enc, SvRV(sv), depth-1); } else if (!SvOK(sv)) { msgpack_pack_nil(enc); } else if (isGV(sv)) { @@ -182,8 +185,9 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv) { } } -static void _msgpack_pack_rv(enc_t *enc, SV* sv) { +static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth) { svtype svt; + if (!depth) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); svt = SvTYPE(sv); @@ -207,8 +211,8 @@ static void _msgpack_pack_rv(enc_t *enc, SV* sv) { msgpack_pack_map(enc, count); while (he = hv_iternext(hval)) { - _msgpack_pack_sv(enc, hv_iterkeysv(he)); - _msgpack_pack_sv(enc, HeVAL(he)); + _msgpack_pack_sv(enc, hv_iterkeysv(he), depth); + _msgpack_pack_sv(enc, HeVAL(he), depth); } } else if (svt == SVt_PVAV) { AV* ary = (AV*)sv; @@ -218,7 +222,7 @@ static void _msgpack_pack_rv(enc_t *enc, SV* sv) { for (i=0; ipack($dat)"); + if (items < 2) { + Perl_croak(aTHX_ "Usage: Data::MessagePack->pack($dat [,$max_depth])"); } - SV* val = ST(1); + SV* val = ST(1); + int depth = 512; + if (items >= 3) depth = SvIV(ST(2)); enc_t enc; enc.sv = sv_2mortal(NEWSV(0, INIT_SIZE)); @@ -256,7 +262,7 @@ XS(xs_pack) { enc.end = SvEND(enc.sv); SvPOK_only(enc.sv); - _msgpack_pack_sv(&enc, val); + _msgpack_pack_sv(&enc, val, depth); SvCUR_set(enc.sv, enc.cur - SvPVX (enc.sv)); *SvEND (enc.sv) = 0; /* many xs functions expect a trailing 0 for text strings */ diff --git a/perl/t/08_cycle.t b/perl/t/08_cycle.t new file mode 100644 index 00000000..55d8427a --- /dev/null +++ b/perl/t/08_cycle.t @@ -0,0 +1,25 @@ +use t::Util; +use Test::More; +use Data::MessagePack; + +plan tests => 5; + +my $aref = [0]; +$aref->[1] = $aref; +eval { Data::MessagePack->pack($aref) }; +ok $@, $@; + +my $href = {}; +$href->{cycle} = $href; +eval { Data::MessagePack->pack($aref) }; +ok $@, $@; + +$aref = [0,[1,2]]; +eval { Data::MessagePack->pack($aref) }; +ok !$@; + +eval { Data::MessagePack->pack($aref, 3) }; +ok !$@; + +eval { Data::MessagePack->pack($aref, 2) }; +ok $@, $@;