aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Högberg <[email protected]>2017-09-20 19:55:22 +0200
committerJohn Högberg <[email protected]>2017-09-22 14:53:14 +0200
commitae559453fed714fecd4284c7a4332bc2c8483c29 (patch)
tree0673aff5fa2d3084831955aa9be002b760d0ab62
parent0f4d186e1b2213e533975db393a8c87e1303fddd (diff)
downloadotp-ae559453fed714fecd4284c7a4332bc2c8483c29.tar.gz
otp-ae559453fed714fecd4284c7a4332bc2c8483c29.tar.bz2
otp-ae559453fed714fecd4284c7a4332bc2c8483c29.zip
Correctly append sub-binaries in iolist_to_iovec
The byte_offset of sub-binaries wasn't taken into account for ProcBins, subtly ruining the results. The test suite didn't catch it since it didn't check for sub-binaries in particular, and only checked for equality between variations -- not whether the output was equal to the input.
-rw-r--r--erts/emulator/beam/erl_io_queue.c7
-rw-r--r--erts/emulator/test/iovec_SUITE.erl43
2 files changed, 26 insertions, 24 deletions
diff --git a/erts/emulator/beam/erl_io_queue.c b/erts/emulator/beam/erl_io_queue.c
index a01b676d39..190ba6bbb9 100644
--- a/erts/emulator/beam/erl_io_queue.c
+++ b/erts/emulator/beam/erl_io_queue.c
@@ -973,9 +973,10 @@ static int iol2v_append_binary(iol2v_state_t *state, Eterm bin_term) {
UWord binary_size;
Uint byte_offset, bit_offset, bit_size;
+ byte *binary_data;
+
Eterm *parent_header;
Eterm parent_binary;
- byte *binary_data;
ASSERT(state->bytereds_available > state->bytereds_spent);
@@ -1001,14 +1002,14 @@ static int iol2v_append_binary(iol2v_state_t *state, Eterm bin_term) {
erts_emasculate_writable_binary(pb);
}
- binary_data = pb->bytes;
+ binary_data = &((byte*)pb->bytes)[byte_offset];
} else {
ErlHeapBin *hb = (ErlHeapBin*)parent_header;
ASSERT(thing_subtag(*parent_header) == HEAP_BINARY_SUBTAG);
ASSERT(is_bin_small);
- binary_data = &((unsigned char*)&hb->data)[byte_offset];
+ binary_data = &((byte*)&hb->data)[byte_offset];
}
if (!is_bin_small && (state->acc_size == 0 || !is_acc_small)) {
diff --git a/erts/emulator/test/iovec_SUITE.erl b/erts/emulator/test/iovec_SUITE.erl
index 28df36d293..49dc64b0d2 100644
--- a/erts/emulator/test/iovec_SUITE.erl
+++ b/erts/emulator/test/iovec_SUITE.erl
@@ -24,7 +24,8 @@
-export([integer_lists/1, binary_lists/1, empty_lists/1, empty_binary_lists/1,
mixed_lists/1, improper_lists/1, illegal_lists/1, cons_bomb/1,
- iolist_to_iovec_idempotence/1, iolist_to_iovec_correctness/1]).
+ sub_binary_lists/1, iolist_to_iovec_idempotence/1,
+ iolist_to_iovec_correctness/1]).
-include_lib("common_test/include/ct.hrl").
@@ -34,8 +35,8 @@ suite() ->
all() ->
[integer_lists, binary_lists, empty_lists, empty_binary_lists, mixed_lists,
- illegal_lists, improper_lists, cons_bomb, iolist_to_iovec_idempotence,
- iolist_to_iovec_correctness].
+ sub_binary_lists, illegal_lists, improper_lists, cons_bomb,
+ iolist_to_iovec_idempotence, iolist_to_iovec_correctness].
init_per_suite(Config) ->
Config.
@@ -46,15 +47,16 @@ end_per_suite(Config) ->
integer_lists(Config) when is_list(Config) ->
Variations = gen_variations([I || I <- lists:seq(1, 255)]),
+ equivalence_test(fun erlang:iolist_to_iovec/1, Variations).
- equivalence_test(fun erlang:iolist_to_iovec/1, Variations),
-
- ok.
+sub_binary_lists(Config) when is_list(Config) ->
+ Parent = <<0:256/unit:8, "gazurka">>,
+ <<0:196/unit:8, Child/binary>> = Parent,
+ equivalence_test(fun erlang:iolist_to_iovec/1, gen_variations(Child)).
binary_lists(Config) when is_list(Config) ->
Variations = gen_variations([<<I:8>> || I <- lists:seq(1, 255)]),
- equivalence_test(fun erlang:iolist_to_iovec/1, Variations),
- ok.
+ equivalence_test(fun erlang:iolist_to_iovec/1, Variations).
empty_lists(Config) when is_list(Config) ->
Variations = gen_variations([[] || _ <- lists:seq(1, 256)]),
@@ -70,8 +72,7 @@ empty_binary_lists(Config) when is_list(Config) ->
mixed_lists(Config) when is_list(Config) ->
Variations = gen_variations([<<>>, lists:seq(1, 40), <<12, 45, 78>>]),
- equivalence_test(fun erlang:iolist_to_iovec/1, Variations),
- ok.
+ equivalence_test(fun erlang:iolist_to_iovec/1, Variations).
illegal_lists(Config) when is_list(Config) ->
BitStrs = gen_variations(["gurka", <<1:1>>, "gaffel"]),
@@ -82,18 +83,15 @@ illegal_lists(Config) when is_list(Config) ->
Variations =
BitStrs ++ BadInts ++ Atoms ++ BadTails,
- illegality_test(fun erlang:iolist_to_iovec/1, Variations),
-
- ok.
+ illegality_test(fun erlang:iolist_to_iovec/1, Variations).
improper_lists(Config) when is_list(Config) ->
Variations = [
[[[[1 | <<2>>] | <<3>>] | <<4>>] | <<5>>],
- [[<<"test">>, 3] | <<"improper tail">>],
- [1, 2, 3 | <<"improper tail">>]
+ [[<<1>>, 2] | <<3, 4, 5>>],
+ [1, 2, 3 | <<4, 5>>]
],
- equivalence_test(fun erlang:iolist_to_iovec/1, Variations),
- ok.
+ equivalence_test(fun erlang:iolist_to_iovec/1, Variations).
cons_bomb(Config) when is_list(Config) ->
IntBase = gen_variations([I || I <- lists:seq(1, 255)]),
@@ -108,8 +106,7 @@ cons_bomb(Config) when is_list(Config) ->
end,
Variations = gen_variations([IntBase, BinBase, MixBase], Rounds),
- equivalence_test(fun erlang:iolist_to_iovec/1, Variations),
- ok.
+ equivalence_test(fun erlang:iolist_to_iovec/1, Variations).
iolist_to_iovec_idempotence(Config) when is_list(Config) ->
IntVariations = gen_variations([I || I <- lists:seq(1, 255)]),
@@ -134,11 +131,15 @@ iolist_to_iovec_correctness(Config) when is_list(Config) ->
ok.
illegality_test(Fun, Variations) ->
- [{'EXIT',{badarg, _}} = (catch Fun(Variation)) || Variation <- Variations].
+ [{'EXIT',{badarg, _}} = (catch Fun(Variation)) || Variation <- Variations],
+ ok.
equivalence_test(Fun, [Head | _] = Variations) ->
+ %% Check that each variation is equal to the others, and that the sum of
+ %% them is equal to the input.
Comparand = Fun(Head),
- [is_iolist_equal(Comparand, Fun(Variation)) || Variation <- Variations],
+ [true = is_iolist_equal(Comparand, Fun(V)) || V <- Variations],
+ true = is_iolist_equal(Variations, Fun(Variations)),
ok.
is_iolist_equal(A, B) ->