Bugs in Erlang code

Bjorn Gustavsson bjorn@REDACTED
Thu Mar 17 11:33:23 CET 2005


Thanks!

I have written the missing test cases for async_call/4 and friends,
and corrected the two bugs ("infinity" instead of "infinite").

Matching against {value,R} in yield/1 is not a bug, since
do_yield(Key, Atom) cannot return anything else than {value,R}
(or crash if Atom is not infinity).

/Bjorn

P.S. Here is the updated code (which will be included in R10B-4).

yield(Key) when is_pid(Key) ->
    {value,R} = do_yield(Key, infinity),
    R.

nb_yield(Key, infinity=Inf) when is_pid(Key) ->
    do_yield(Key, Inf);
nb_yield(Key, Timeout) when is_pid(Key), is_integer(Timeout), Timeout >= 0 ->
    do_yield(Key, Timeout).

nb_yield(Key) when is_pid(Key) ->
    do_yield(Key, 0).

do_yield(Key, Timeout) ->
    receive
        {Key,{promise_reply,R}} ->
            {value,R}
        after Timeout ->
            timeout
    end.



Kostis Sagonas <kostis@REDACTED> writes:

> [ Apologies for the plug for Dialyzer, but I am currently quite excited. ]
> 
>   Intro: As part of Dialyzer, we have been developing a type
> 	 analysis for Erlang that is significantly different
> 	 that the one the current version of Dialyzer is using.
> 	 It is not totally usable yet, but it is very powerful.
> 	 Stay tuned for its release!
> 
> 
> I am really amazed at some of the bugs we have been able to find
> in supposedly well-tested Erlang code using the new type analysis.
> 
> Many of them, we've reported directly to the OTP team and they are
> already fixed.  Some others we've finding every day as the analysis
> gets stronger and stronger.  For example, today I've come across
> the following code (in kernel/src/rpc.erl).
> 
> =======================================================================
> yield(Key) when pid(Key) ->
>     {value, R} = do_yield(Key, infinite),
>     R.
> 
> nb_yield(Key, infinite) when pid(Key) ->
>     do_yield(Key, infinite);
> nb_yield(Key, Timeout) when pid(Key), integer(Timeout), Timeout >= 0 ->
>     do_yield(Key, Timeout).
> 
> nb_yield(Key) when pid(Key) ->
>     do_yield(Key, 0).
> 
> do_yield(Key, Timeout) ->
>     receive
>         {Key,{promise_reply,R}} ->
>             {value,R}
>         after Timeout ->
>             timeout
>     end.
> =========================================================================
> 
> There are actually two (and arguably three) errors in the above code!
> 
> Dialyzer found them.
> 
> QUIZ for the seasoned Erlang programmers: Who can spot them?
> 
> Cheers,
> Kostis
> 

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list