Skip to content

Commit 9c7adf5

Browse files
committed
erts: Check for duplicated native record fields in binary_to_term/1
Fix erlang#11398
1 parent 7c5a9a2 commit 9c7adf5

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

erts/emulator/beam/external.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,6 +5326,13 @@ dec_term(ErtsDistExternal *edep,
53265326
qsort(fields, num_fields, sizeof(struct erl_record_field),
53275327
(int (*)(const void *, const void *)) record_compare);
53285328

5329+
for (Sint i = 1; i < num_fields; i++) {
5330+
if (fields[i-1].key == fields[i].key) {
5331+
erts_free(ERTS_ALC_T_TMP, fields);
5332+
goto error;
5333+
}
5334+
}
5335+
53295336
order_tuple = make_boxed(order);
53305337
*order++ = make_arityval(num_fields);
53315338
for (int i = 0; i < num_fields; i++) {

erts/emulator/test/native_record_SUITE.erl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
init_per_group/2,end_per_group/2,
2828
create/1,explicit_module_name/1,
2929
term_order/1,gc/1,external_term_format/1,
30-
messages/1,errors/1,records_module/1, dist/1]).
30+
messages/1,errors/1,records_module/1, dist/1,
31+
gh_11398/1]).
3132

3233
-record #a{x=1, y=2}.
3334
-record #b{x=none, y=none, z=none}.
@@ -62,7 +63,8 @@ all() ->
6263
messages,
6364
errors,
6465
records_module,
65-
dist].
66+
dist,
67+
gh_11398].
6668

6769
groups() ->
6870
[].
@@ -568,6 +570,24 @@ dist(_Config) ->
568570

569571
ok.
570572

573+
gh_11398(_Config) ->
574+
#native_record_SUITE:exp{a=1} = binary_to_term(y(1, 1)),
575+
?assertError(badarg, binary_to_term(y(1, 2))).
576+
577+
y(A, N) when is_integer(A) ->
578+
<<131, AValue/bytes>> = erlang:term_to_binary(A),
579+
<<
580+
131,
581+
%% RECORD_EXT, N fields, exported flag set
582+
$C,
583+
N:32,
584+
1:8,
585+
$w, 19, "native_record_SUITE",
586+
$w, 3, "exp",
587+
%% N copies of field name 'a', followed by N copies of value A
588+
(binary:copy(<<$w, 1, "a">>, N))/bytes,
589+
(binary:copy(AValue, N))/bytes
590+
>>.
571591

572592
%%% Common utilities.
573593

0 commit comments

Comments
 (0)