4
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
4
%% Copyright Ericsson AB 1997-2010. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
51
52
bad_term_to_binary/1,
52
bad_binary_to_term_2/1,
53
bad_binary_to_term_2/1,safe_binary_to_term2/1,
53
54
bad_binary_to_term/1, bad_terms/1, more_bad_terms/1,
54
55
otp_5484/1,otp_5933/1,
55
56
ordering/1,unaligned_order/1,gc_test/1,
66
67
t_split_binary, bad_split, t_concat_binary,
67
68
bad_list_to_binary, bad_binary_to_list, terms, terms_float,
68
69
external_size, t_iolist_size,
70
bad_binary_to_term_2,safe_binary_to_term2,
70
71
bad_binary_to_term, bad_terms, t_hash, bad_size, bad_term_to_binary,
71
72
more_bad_terms, otp_5484, otp_5933, ordering, unaligned_order,
72
73
gc_test, bit_sized_binary_sizes, bitlevel_roundtrip, otp_6817, otp_8117,
440
441
Term = binary_to_term(Bin),
442
Term = erlang:binary_to_term(Bin, [safe]),
441
443
Unaligned = make_unaligned_sub_binary(Bin),
442
444
Term = binary_to_term(Unaligned),
445
Term = erlang:binary_to_term(Unaligned, []),
446
Term = erlang:binary_to_term(Bin, [safe]),
443
447
BinC = erlang:term_to_binary(Term, [compressed]),
444
448
Term = binary_to_term(BinC),
445
449
true = size(BinC) =< size(Bin),
538
542
bad_bin_to_term(BadBin) ->
539
543
{'EXIT',{badarg,_}} = (catch binary_to_term(BadBin)).
545
bad_bin_to_term(BadBin,Opts) ->
546
{'EXIT',{badarg,_}} = (catch erlang:binary_to_term(BadBin,Opts)).
548
safe_binary_to_term2(doc) -> "Test safety options for binary_to_term/2";
549
safe_binary_to_term2(Config) when is_list(Config) ->
550
?line bad_bin_to_term(<<131,100,0,14,"undefined_atom">>, [safe]),
551
?line bad_bin_to_term(<<131,100,0,14,"other_bad_atom">>, [safe]),
552
BadHostAtom = <<100,0,14,"badguy@badhost">>,
554
BadRef = <<131,114,0,3,BadHostAtom/binary,0,<<0,0,0,255>>/binary,
555
Empty/binary,Empty/binary>>,
556
?line bad_bin_to_term(BadRef, [safe]), % good ref, with a bad atom
557
?line fullsweep_after = erlang:binary_to_term(<<131,100,0,15,"fullsweep_after">>, [safe]), % should be a good atom
558
BadExtFun = <<131,113,100,0,4,98,108,117,101,100,0,4,109,111,111,110,97,3>>,
559
?line bad_bin_to_term(BadExtFun, [safe]),
541
562
%% Tests bad input to binary_to_term/1.
543
564
bad_terms(suite) -> [];
559
580
?line corrupter(CompressedBin, size(CompressedBin)-1).
561
582
corrupter(Bin, Pos) when Pos >= 0 ->
562
?line {ShorterBin, _} = split_binary(Bin, Pos),
583
?line {ShorterBin, Rest} = split_binary(Bin, Pos),
563
584
?line catch binary_to_term(ShorterBin), %% emulator shouldn't crash
564
585
?line MovedBin = list_to_binary([ShorterBin]),
565
586
?line catch binary_to_term(MovedBin), %% emulator shouldn't crash
566
?line corrupter(MovedBin, Pos-1);
588
%% Bit faults, shouldn't crash
589
<<Byte,Tail/binary>> = Rest,
590
Fun = fun(M) -> FaultyByte = Byte bxor M,
591
catch binary_to_term(<<ShorterBin/binary,
592
FaultyByte, Tail/binary>>) end,
593
?line lists:foreach(Fun,[1,2,4,8,16,32,64,128,255]),
594
?line corrupter(Bin, Pos-1);
567
595
corrupter(_Bin, _) ->