[erlang-bugs] bug in string:copies when copying float times character

Michael Santos <>
Sun Oct 24 14:26:43 CEST 2010


On Sun, Oct 24, 2010 at 01:02:36PM +0200, PAILLEAU Eric wrote:

> Number should obviously be an integer, but it seems there no
> guards on the type.
> 
> I forgot do round my copy number of characters, I got crashes, and I
> finally found the problem :
> 
> by doing :
> --------------------------------------------------------------------
> Erlang R14A (erts-5.8) [source] [smp:2:2] [rq:2] [async-threads:0]
> [hipe] [kernel-poll:false]
> 
> Eshell V5.8  (abort with ^G)
> 1> string:copies("a",2.5).
> 
> Crash dump was written to: erl_crash.dump
> eheap_alloc: Cannot allocate 1140328500 bytes of memory (of type "heap").
> --------------------------------------------------------------------

string:copies/2 decrements to 0 with each copy. With a float, 0 will
never be matched and the function goes into an infinite loop.

Using your suggestion of a guard fixes it:

1> string:copies("a",2.5).
** exception error: no function clause matching string:copies("a",2.5)

I'll send a patch to erlang-patches.


diff --git a/lib/stdlib/src/string.erl b/lib/stdlib/src/string.erl
index 6636a03..c987c22 100644
--- a/lib/stdlib/src/string.erl
+++ b/lib/stdlib/src/string.erl
@@ -202,5 +202,5 @@ chars(C, 0, Tail) when is_integer(C) ->
 -spec copies(string(), non_neg_integer()) -> string().
 
-copies(CharList, Num) when is_list(CharList), Num >= 0 ->
+copies(CharList, Num) when is_list(CharList), is_integer(Num), Num >= 0 ->
     copies(CharList, Num, []).
 



More information about the erlang-bugs mailing list