aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test
diff options
context:
space:
mode:
authorSverker Eriksson <[email protected]>2016-02-12 18:52:20 +0100
committerLukas Larsson <[email protected]>2016-03-29 14:57:11 +0200
commita2a86dadc648dda68b5221a7c1d83b9238be1e25 (patch)
tree6229f416cc8a8e8a4b413a3a91246e37a693ff3a /erts/emulator/test
parent209c5cf22b5cdc70eb48e6afdcddfa7132471aab (diff)
downloadotp-a2a86dadc648dda68b5221a7c1d83b9238be1e25.tar.gz
otp-a2a86dadc648dda68b5221a7c1d83b9238be1e25.tar.bz2
otp-a2a86dadc648dda68b5221a7c1d83b9238be1e25.zip
erts: Improve enif_binary_to_term
* Accept a raw data buffer instead of ErlNifBinary * Accept option ERL_NIF_BIN2TERM_SAFE * Return number of read bytes
Diffstat (limited to 'erts/emulator/test')
-rw-r--r--erts/emulator/test/nif_SUITE.erl17
-rw-r--r--erts/emulator/test/nif_SUITE_data/nif_SUITE.c20
2 files changed, 26 insertions, 11 deletions
diff --git a/erts/emulator/test/nif_SUITE.erl b/erts/emulator/test/nif_SUITE.erl
index bbd523b4ef..4dcfbf35ca 100644
--- a/erts/emulator/test/nif_SUITE.erl
+++ b/erts/emulator/test/nif_SUITE.erl
@@ -1971,13 +1971,22 @@ nif_term_to_binary(Config) ->
true = term_to_binary_nif(T, self()),
receive Bin -> ok end.
+-define(ERL_NIF_BIN2TERM_SAFE, 16#20000000).
+
nif_binary_to_term(Config) ->
ensure_lib_loaded(Config),
T = {#{ok => nok}, <<0:8096>>, lists:seq(1,100)},
Bin = term_to_binary(T),
- T = binary_to_term_nif(Bin, undefined),
- true = binary_to_term_nif(Bin, self()),
- receive T -> ok end.
+ Len = byte_size(Bin),
+ {Len,T} = binary_to_term_nif(Bin, undefined, 0),
+ Len = binary_to_term_nif(Bin, self(), 0),
+ T = receive M -> M after 1000 -> timeout end,
+
+ {Len, T} = binary_to_term_nif(Bin, undefined, ?ERL_NIF_BIN2TERM_SAFE),
+ false = binary_to_term_nif(<<131,100,0,14,"undefined_atom">>,
+ undefined, ?ERL_NIF_BIN2TERM_SAFE),
+ false = binary_to_term_nif(Bin, undefined, 1),
+ ok.
nif_port_command(Config) ->
ensure_lib_loaded(Config),
@@ -2069,7 +2078,7 @@ unique_integer_nif(_) -> ?nif_stub.
is_process_alive_nif(_) -> ?nif_stub.
is_port_alive_nif(_) -> ?nif_stub.
term_to_binary_nif(_, _) -> ?nif_stub.
-binary_to_term_nif(_, _) -> ?nif_stub.
+binary_to_term_nif(_, _, _) -> ?nif_stub.
port_command_nif(_, _) -> ?nif_stub.
%% maps
diff --git a/erts/emulator/test/nif_SUITE_data/nif_SUITE.c b/erts/emulator/test/nif_SUITE_data/nif_SUITE.c
index 317f440257..b3c6cc5ba3 100644
--- a/erts/emulator/test/nif_SUITE_data/nif_SUITE.c
+++ b/erts/emulator/test/nif_SUITE_data/nif_SUITE.c
@@ -2057,25 +2057,31 @@ static ERL_NIF_TERM term_to_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM
static ERL_NIF_TERM binary_to_term(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary bin;
- ERL_NIF_TERM term;
+ ERL_NIF_TERM term, ret_term;
ErlNifPid pid;
ErlNifEnv *msg_env = env;
+ unsigned int opts;
+ ErlNifUInt64 ret;
if (enif_get_local_pid(env, argv[1], &pid))
msg_env = enif_alloc_env();
- if (!enif_inspect_binary(env, argv[0], &bin))
+ if (!enif_inspect_binary(env, argv[0], &bin)
+ || !enif_get_uint(env, argv[2], &opts))
return enif_make_badarg(env);
- if (!enif_binary_to_term(env, &bin, &term))
- return enif_make_badarg(env);
+ ret = enif_binary_to_term(msg_env, bin.data, bin.size, &term,
+ (ErlNifBinaryToTerm)opts);
+ if (!ret)
+ return atom_false;
+ ret_term = enif_make_uint64(env, ret);
if (msg_env != env) {
enif_send(env, &pid, msg_env, term);
enif_free_env(msg_env);
- return atom_true;
+ return ret_term;
} else {
- return term;
+ return enif_make_tuple2(env, ret_term, term);
}
}
@@ -2170,7 +2176,7 @@ static ErlNifFunc nif_funcs[] =
{"is_process_alive_nif", 1, is_process_alive},
{"is_port_alive_nif", 1, is_port_alive},
{"term_to_binary_nif", 2, term_to_binary},
- {"binary_to_term_nif", 2, binary_to_term},
+ {"binary_to_term_nif", 3, binary_to_term},
{"port_command_nif", 2, port_command}
};