aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator
diff options
context:
space:
mode:
authorFilipe David Manana <[email protected]>2011-09-01 02:05:05 +0100
committerFilipe David Manana <[email protected]>2011-09-02 00:35:48 -0700
commit03d8c2877342d5ed57596330a61ec0374092f136 (patch)
treedc4f32655af4b0ab0fdd7faa30a1092b87bc65b7 /erts/emulator
parenteb4aa9b65c8b50597ed8fae092555145e466e1dc (diff)
downloadotp-03d8c2877342d5ed57596330a61ec0374092f136.tar.gz
otp-03d8c2877342d5ed57596330a61ec0374092f136.tar.bz2
otp-03d8c2877342d5ed57596330a61ec0374092f136.zip
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.
Diffstat (limited to 'erts/emulator')
-rw-r--r--erts/emulator/beam/erl_nif.c10
-rw-r--r--erts/emulator/test/nif_SUITE.erl6
2 files changed, 15 insertions, 1 deletions
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)
diff --git a/erts/emulator/test/nif_SUITE.erl b/erts/emulator/test/nif_SUITE.erl
index 2867e8e2e4..f6344791f1 100644
--- a/erts/emulator/test/nif_SUITE.erl
+++ b/erts/emulator/test/nif_SUITE.erl
@@ -281,6 +281,12 @@ types(Config) when is_list(Config) ->
end, int_list()),
?line verify_tmpmem(TmpMem),
+ ?line true = (compare(-1294536544000, -1178704800000) < 0),
+ ?line true = (compare(-1178704800000, -1294536544000) > 0),
+ ?line true = (compare(-295147905179352825856, -36893488147419103232) < 0),
+ ?line true = (compare(-36893488147419103232, -295147905179352825856) > 0),
+ ?line true = (compare(-29514790517935282585612345678, -36893488147419103232) < 0),
+ ?line true = (compare(-36893488147419103232, -29514790517935282585612345678) > 0),
ok.
int_list() ->