[erlang-bugs] Weird exception error return involving bitstrings

Adam Rutkowski <>
Mon Oct 29 23:56:19 CET 2012


Hi,

I am facing weird behaviour for the following code:

-module(foo).
-export([bar/1]).
-include_lib("eunit/include/eunit.hrl").

bar(<<>>) -> ok;
bar(Bin) when is_bitstring(Bin) andalso not is_binary(Bin) ->
    bar(<<Bin/binary, 0:1>>);
bar(<<_:8/integer, Rest/binary>>) ->
    bar(Rest).

foobar1_test() -> ok = bar(<<1,2,3>>).
foobar2_test() -> ok = bar(<<256:8>>).
foobar3_test() -> ok = bar(<<256:7>>).
foobar4_test() -> ok = bar(<<1,2,3>>).


The code is obviously wrong - it should be

   bar(<<Bin/bistring, 0:1>>)

in the second clause, however:

% erl
Erlang R15B02 (erts-5.9.2) [source] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.2  (abort with ^G)
1> c(foo).            
{ok,foo}
2> foo:bar(<<256:7>>).
ok
3> foo:foobar3_test().
** exception error: bad argument
     in function  foo:bar/1 (foo.erl, line 6)
     in call from foo:foobar3_test/0 (foo.erl, line 18)
4> foo:foobar3_test().
** exception error: []
5> catch(foo:foobar3_test()).
{'EXIT',{[],[]}}

Notice I have called the same function twice getting different exceptions.
Eunit looks confused too:

1> eunit:test(foo).
foo: foobar3_test...*failed*
in function eunit_proc:get_next_item/1 (eunit_proc.erl, line 465)
in call from eunit_proc:tests_inorder/3 (eunit_proc.erl, line 429)
**throw:{module_not_found,foo_tests}

--
Adam



More information about the erlang-bugs mailing list