Browse Source

Fixes decode TINYINT as signed in binary protocol

Viktor Söderqvist 10 years ago
parent
commit
6a9fbfbb5a
2 changed files with 7 additions and 2 deletions
  1. 1 1
      src/mysql_protocol.erl
  2. 6 1
      test/mysql_tests.erl

+ 1 - 1
src/mysql_protocol.erl

@@ -557,7 +557,7 @@ decode_binary(#col{type = T}, <<Value:32/signed-little, Rest/binary>>)
 decode_binary(#col{type = T}, <<Value:16/signed-little, Rest/binary>>)
   when T == ?TYPE_SHORT; T == ?TYPE_YEAR ->
     {Value, Rest};
-decode_binary(#col{type = ?TYPE_TINY}, <<Value:8, Rest/binary>>) ->
+decode_binary(#col{type = ?TYPE_TINY}, <<Value:8/signed, Rest/binary>>) ->
     {Value, Rest};
 decode_binary(#col{type = ?TYPE_DOUBLE},
               <<Value:64/float-little, Rest/binary>>) ->

+ 6 - 1
test/mysql_tests.erl

@@ -198,7 +198,12 @@ int(Pid) ->
                            <<"i">>),
     write_read_text_binary(Pid, -987654321, <<"-987654321">>,
                            <<"ints">>, <<"i">>),
-    ok = mysql:query(Pid, "DROP TABLE ints").
+    ok = mysql:query(Pid, "DROP TABLE ints"),
+    %% Overflow with TINYINT
+    ok = mysql:query(Pid, "CREATE TABLE tint (i TINYINT)"),
+    write_read_text_binary(Pid, 127, <<"1000">>, <<"tint">>, <<"i">>),
+    write_read_text_binary(Pid, -128, <<"-1000">>, <<"tint">>, <<"i">>),
+    ok = mysql:query(Pid, "DROP TABLE tint").
 
 %% The BIT(N) datatype in MySQL 5.0.3 and later: the equivallent to bitstring()
 bit(Pid) ->