[erlang-questions] erlang 21 binary matching bug in functin definition

Brujo Benavides elbrujohalcon@REDACTED
Fri Aug 3 18:42:35 CEST 2018


Actually… I could reproduce the bug, see…

Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Eshell V10.0  (abort with ^G)
1> {ok, A} = file:read_file("a.erl"), io:format("~s~n", [A]).
-module a.
-export [f1/1, f2/1].

f1(<<Length, Data/binary>>) ->
    case {Length, Data} of
        {0, _} -> {Length, Data};
        {4, <<_:16/little, _/binary>>} -> irrelevant
    end.

f2(<<Length, Data/binary>>) ->
    case {Length, Data} of
        {0, _} -> {Length, Data};
        {4, _} -> irrelevant
    end.

ok
2> a:f1(<<0>>).
{0,<<0>>}
3> a:f2(<<0>>).
{0,<<>>}
4> F = fun(<<Length, Data/binary>>) ->
4>     case {Length, Data} of
4>         {0, _} -> {Length, Data};
4>         {4, <<_:16/little, _/binary>>} -> irrelevant
4>     end
4> end.
#Fun<erl_eval.6.127694169>
5> F(<<0>>).
{0,<<>>}

It seems to be related to the second pattern matching in the case statement. That’s weird

Brujo Benavides <http://about.me/elbrujohalcon>



> On 3 Aug 2018, at 13:32, Brujo Benavides <elbrujohalcon@REDACTED> wrote:
> 
> Well, that’s not happening here…
> 
> Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
> 
> Eshell V10.0  (abort with ^G)
> 1> <<X, Y/binary>> = <<0>>, {X, Y}.
> {0,<<>>}
> 2> F = fun(<<X, Y/binary>>) -> {X, Y} end.
> #Fun<erl_eval.6.127694169>
> 3> F(<<0>>).
> {0,<<>>}
> 4> {ok, A} = file:read_file("a.erl"), io:format("~s~n", [A]).
> -module a.
> -export [f/1].
> 
> f(<<X, Y/binary>>) -> {X, Y}.
> 
> ok
> 5> c(a).
> {ok,a}
> 6> a:f(<<0>>).
> {0,<<>>}
> 7>
> 
> There must be something else happening with your code.
> 
> Brujo Benavides <http://about.me/elbrujohalcon>
> 
> 
> 
>> On 3 Aug 2018, at 13:08, 格通 <getonga2018@REDACTED <mailto:getonga2018@REDACTED>> wrote:
>> 
>> I try to fix the mysql-otp  for erlang 21, but found a bug  like this:
>> ```
>> -module(a).
>> -export([decode_binary/2]).
>> decode_binary(_, <<Length, Data/binary>>) ->
>>     %% Coded in the same way as DATETIME and TIMESTAMP below, but returned in
>>     %% a simple triple.
>> 
>>     case {Length, Data} of
>>         {0, _} -> io:format("~p length:~p, Data:~p, ~n" , [?LINE, Length, Data]),{{0, 0, 0}, Data};
>>         {4, <<Y:16/little, M, D, Rest/binary>>} -> io:format("~p~n" , [?LINE]),{{Y, M, D}, Rest}
>>     end.
>> 
>> ```
>> A function copy from https://github.com/mysql-otp/mysql-otp/blob/master/src/mysql_protocol.erl#L789-L795 <https://github.com/mysql-otp/mysql-otp/blob/master/src/mysql_protocol.erl#L789-L795>
>> My erlang version is 21.0.4, and the pr address is  https://github.com/mysql-otp/mysql-otp/pull/84 <https://github.com/mysql-otp/mysql-otp/pull/84> .
>> compile it and run:
>> ```
>> erlc a.erl
>> erl
>> 1> a:decode_binary(test, <<0>>).
>> 8 length:0, Data:<<0>>, 
>> {{0,0,0},<<0>>}
>> ```
>> But in pattern matching:
>> ```
>> 2> <<Length, Data/binary>> = <<0>>.
>> <<0>>
>> 3>Length.
>> 0
>> 4>Data.
>> <<>>
>> ```
>> It must be a bug in erlang 21 binary matching in function definition.
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>> http://erlang.org/mailman/listinfo/erlang-questions
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180803/f5fdc296/attachment.htm>


More information about the erlang-questions mailing list