[erlang-questions] queue:split/2 unsafe!

Ivan Uemlianin <>
Tue Apr 9 18:31:57 CEST 2013


Dear All

I've just discovered that queue:split(N, Queue) causes a crash if N is 
larger than Queue has items:

     1> self().
     <0.37.0>
     2> Q1 = queue:new().
     {[],[]}
     3> Q2 = queue:in(a, Q1).
     {[a],[]}
     4> Q3 = queue:in(b, Q2).
     {[b],[a]}
     5> Q4 = queue:in(c, Q3).
     {[c,b],[a]}
     6> queue:split(4, Q4).
     ** exception error: bad argument
          in function  queue:split/2
             called as queue:split(4,{[c,b],[a]})
     7> self().
     <0.45.0>

I've written a safe split:

     queue_safe_split(N, Q) ->
         case queue:len(Q) >= N of
             true ->
                 queue:split(N,Q);
             false ->
                 {Q, queue:new()}
         end.

Thanks

Ivan


-- 
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

                     
                      www.llaisdy.com
                          llaisdy.wordpress.com
               github.com/llaisdy
                      www.linkedin.com/in/ivanuemlianin

                         festina lente
============================================================



More information about the erlang-questions mailing list