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

unpack: use lazy alloc to avoid large array/map OOM #41

Open
wants to merge 1 commit 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
17 changes: 17 additions & 0 deletions t/25_invalid_large_oom.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use strict;
use warnings;
use Data::MessagePack;
use Test::More;
use t::Util;

my @data = (
["\xdd\xff\x00\x00\x00", "large array"],
["\xdf\xff\x00\x00\x00", "large map"],
);

foreach my $d (@data) {
eval { Data::MessagePack->unpack(@{$d}[0]); };
like $@, qr/insufficient bytes/i, @{$d}[1];
}

done_testing;
8 changes: 6 additions & 2 deletions xs-src/unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ typedef struct {
} unpack_user;
#define UNPACK_USER_INIT { false, false, NULL }

#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif

#include "msgpack/unpack_define.h"

#define msgpack_unpack_struct(name) \
Expand Down Expand Up @@ -225,7 +229,7 @@ STATIC_INLINE int template_callback_array(unpack_user* u PERL_UNUSED_DECL, unsig
dTHX;
AV* const a = newAV();
*o = newRV_noinc((SV*)a);
av_extend(a, n + 1);
av_extend(a, MIN(n, 4096) + 1);
return 0;
}

Expand All @@ -242,7 +246,7 @@ STATIC_INLINE int template_callback_map(unpack_user* u PERL_UNUSED_DECL, unsigne
{
dTHX;
HV* const h = newHV();
hv_ksplit(h, n);
hv_ksplit(h, MIN(n, 65536));
*o = newRV_noinc((SV*)h);
return 0;
}
Expand Down