bug in string:copies when copying float times character

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

bug in string:copies when copying float times character

PAILLEAU Eric

Hello,
I found something weird, that can be considered as a bug.
Here is below the documentation for string:copies function.

---------------------------------------
copies(String, Number) -> Copies

Types:
String = Copies = string()
Number = integer()
---------------------------------------

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").
--------------------------------------------------------------------

Erlang try to allocate the whole memory when trying to copy any float
number of characters. My computer is frozen a while, and reach hopefully
to garbage .

I got same behaviour in R13 and I did update to R14 in order to see if a
fix was done.

I guess an en exception 'badarith' or something else should be raised if
integer is not used on such function.

Best regards.










________________________________________________________________
erlang-bugs (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: bug in string:copies when copying float times character

Michael Santos-2
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, []).
 


________________________________________________________________
erlang-bugs (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]