From 03d8c2877342d5ed57596330a61ec0374092f136 Mon Sep 17 00:00:00 2001 From: Filipe David Manana Date: Thu, 1 Sep 2011 02:05:05 +0100 Subject: Fix enif_compare on 64bits machines In 64bits machines the Sint type has a size of 8 bytes, while on 32bits machines it has a 4 bytes size. enif_compare was ignoring this and therefore returning incorrect values when the result of the CMP function (which returns a Sint value) doesn't fit in 4 bytes. For example, passing the operands -1294536544000 and -1178704800000 to enif_compare would trigger the bug. --- erts/emulator/beam/erl_nif.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'erts/emulator/beam/erl_nif.c') diff --git a/erts/emulator/beam/erl_nif.c b/erts/emulator/beam/erl_nif.c index d9b1a8e89d..6e7ac43676 100644 --- a/erts/emulator/beam/erl_nif.c +++ b/erts/emulator/beam/erl_nif.c @@ -578,7 +578,15 @@ int enif_is_identical(Eterm lhs, Eterm rhs) int enif_compare(Eterm lhs, Eterm rhs) { - return CMP(lhs,rhs); + Sint result = CMP(lhs,rhs); + + if (result < 0) { + return -1; + } else if (result > 0) { + return 1; + } + + return result; } int enif_get_tuple(ErlNifEnv* env, Eterm tpl, int* arity, const Eterm** array) -- cgit v1.2.3