erlang 21 binary matching bug in functin definition

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

erlang 21 binary matching bug in functin definition

格通
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.

```
My erlang version is 21.0.4, and the pr address is  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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: erlang 21 binary matching bug in functin definition

Brujo Benavides-3
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.



On 3 Aug 2018, at 13:08, 格通 <[hidden email]> 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.

```
My erlang version is 21.0.4, and the pr address is  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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: erlang 21 binary matching bug in functin definition

Brujo Benavides-3
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

On 3 Aug 2018, at 13:32, Brujo Benavides <[hidden email]> 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.



On 3 Aug 2018, at 13:08, 格通 <[hidden email]> 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.

```
My erlang version is 21.0.4, and the pr address is  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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: erlang 21 binary matching bug in functin definition

Michał Muskała
In reply to this post by Brujo Benavides-3
On 3 Aug 2018, 18:32 +0200, Brujo Benavides <[hidden email]>, wrote:
Well, that’s not happening here…
 

I think you simplified the function too much, so it doesn't trigger the bug in the compiler anymore. I can reproduce the bug with a somewhat simpler code:


-module(a).

-export([err/2]).

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

Calling a:err(1, <<0>>) returns <<0>>. That's on 21.0.2

Michał.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: erlang 21 binary matching bug in functin definition

Brujo Benavides-3
Yeah, you’re right Michał! :)

In OTP20 it works…

Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.1  (abort with ^G)
1> c(a), a:err(<<0>>).
{0,<<>>}
2>

格通: I think you should report the bug :)


On 3 Aug 2018, at 13:45, Michał Muskała <[hidden email]> wrote:

On 3 Aug 2018, 18:32 +0200, Brujo Benavides <[hidden email]>, wrote:
Well, that’s not happening here…
 

I think you simplified the function too much, so it doesn't trigger the bug in the compiler anymore. I can reproduce the bug with a somewhat simpler code:


-module(a).

-export([err/2]).

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

Calling a:err(1, <<0>>) returns <<0>>. That's on 21.0.2

Michał.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions