Erlang Mailing Lists

Author Message

<  Erlang bugs mailing list  ~  bug in string:copies when copying float times character

Guest
Posted: Fri Oct 29, 2010 7:07 am Reply with quote
Guest
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.Cool [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:erlang-bugs-unsubscribe@erlang.org

Post received from mailinglist

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum