aboutsummaryrefslogtreecommitdiffstats
path: root/erts/preloaded
diff options
context:
space:
mode:
authorJuan Jose Comellas <[email protected]>2013-09-09 14:19:41 -0300
committerJuan Jose Comellas <[email protected]>2013-09-09 14:28:40 -0300
commit49059dbd211987987a7e9d9ef40cc0b2484f829a (patch)
tree98a44f44ccb6cd76c186db5f4e294bc1ddbd506a /erts/preloaded
parent2f2824519d13e7745c02efbc7c29c37a76885fee (diff)
downloadotp-49059dbd211987987a7e9d9ef40cc0b2484f829a.tar.gz
otp-49059dbd211987987a7e9d9ef40cc0b2484f829a.tar.bz2
otp-49059dbd211987987a7e9d9ef40cc0b2484f829a.zip
Fix incorrect values returned by integer_to_binary/2
When integer_to_binary/2 receives 0 or a negative number as an argument with a base that is different from 10, it will return incorrect values (<<>> in the case of 0) or it will crash (with negative numbers). This commit fixes these problems and adds tests to cover these cases.
Diffstat (limited to 'erts/preloaded')
-rw-r--r--erts/preloaded/src/erlang.erl15
1 files changed, 8 insertions, 7 deletions
diff --git a/erts/preloaded/src/erlang.erl b/erts/preloaded/src/erlang.erl
index e016a50c4c..a969ef91dc 100644
--- a/erts/preloaded/src/erlang.erl
+++ b/erts/preloaded/src/erlang.erl
@@ -2891,22 +2891,23 @@ integer_to_binary(I, Base)
when erlang:is_integer(I), erlang:is_integer(Base),
Base >= 2, Base =< 1+$Z-$A+10 ->
if I < 0 ->
- <<"$-",(integer_to_binary(-I, Base, []))/binary>>;
+ <<$-,(integer_to_binary(-I, Base, <<>>))/binary>>;
true ->
integer_to_binary(I, Base, <<>>)
end;
integer_to_binary(I, Base) ->
erlang:error(badarg, [I, Base]).
-integer_to_binary(0, _Base, R0) ->
- R0;
integer_to_binary(I0, Base, R0) ->
D = I0 rem Base,
I1 = I0 div Base,
- if D >= 10 ->
- integer_to_binary(I1,Base,<<(D-10+$A),R0/binary>>);
- true ->
- integer_to_binary(I1,Base,<<(D+$0),R0/binary>>)
+ R1 = if
+ D >= 10 -> <<(D-10+$A),R0/binary>>;
+ true -> <<(D+$0),R0/binary>>
+ end,
+ if
+ I1 =:= 0 -> R1;
+ true -> integer_to_binary(I1, Base, R1)
end.
%% erlang:flush_monitor_message/2 is for internal use only!