Bug #29991 assertion in network-mysqld-proto.c:99 when auto_increment key > 65534
Submitted: 23 Jul 2007 22:21 Modified: 31 Aug 2007 5:01
Reporter: Justin Swanhart Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Proxy: Core Severity:S1 (Critical)
Version:0.5.1 OS:Linux
Assigned to: Jan Kneschke CPU Architecture:Any
Tags: assertion

[23 Jul 2007 22:21] Justin Swanhart
Description:
When a query comes into read_query() I inject an INSERT INTO.

Once my test table (with a BIGINT key) hits 65534 entries, the proxy stops with an assertion:
./mysql-proxy --proxy-lua-script=proxy_test.lua
file network-mysqld-proto.c: line 99 (network_mysqld_proto_get_int_len): assertion failed: (*_off + size <= packet->len)
Aborted

to duplicate:
run the proxy with the lua script from 'how to repeat', then execute 65535+ queries.  here is a PHP script to run the queries easily:

<?php
###
### CREATE THE TEST TABLE NOT USING THE PROXY CONNECTION
###
$conn = mysql_connect('127.0.0.1:3306');
mysql_query("DROP TABLE IF EXISTS test.proxytest");
$table = mysql_query("CREATE TABLE test.proxytest (pk bigint auto_increment primary key, col varchar(10))");
$conn = false;
###
### EXECUTE LOTS OF SELECT 1; THROUGH THE PROXY
###
$conn = mysql_connect('127.0.0.1:4040');
for($i=0;$i<70000;$i++) {
  $result = mysql_query("SELECT 1");
  if (!$result) {
        echo mysql_error($conn);
        exit;
  }

}
?>

How to repeat:
-- use the following script for --proxy-lua-script
-- then run 65535+_ queries
-- -----------------------------------------------------------

-- this function makes a COM_QUERY packet from a SQL statement
function make_query(sql) 
--      print (sql .. "\n")
        return string.char(proxy.COM_QUERY) .. sql
end

-- this function adds a query to the query queue
-- the id for the query is automatically incremented
-- and returned by the function 
_G.packet_id = 0
function add_query(packet) 
        _G.packet_id = _G.packet_id + 1
--      print(string.sub(packet,2))
        proxy.queries:append(_G.packet_id, packet)
        return _G.packet_id
end

-- when a query comes in from the client, this event fires
function read_query(packet)
        --INJECT OUR TEST QUERY
        -- THIS BREAKS AFTER 65534 INSERT INTO statements....
        add_query(make_query("INSERT INTO test.proxytest (pk) values (NULL)"))
        _G.orig_query_id = add_query(packet)
        return proxy.PROXY_SEND_QUERY
end

-- answer to any injected queries 
-- this won't fire unless we answer read_query with PROXY_SEND_QUERY....
function read_query_result(inj)
        -- send the result of the original SQL statement
        if inj.id == _G.orig_query_id then
                return proxy.PROXY_SEND_RESULT;
        end

        return proxy.PROXY_IGNORE_RESULT;
end
[31 Jul 2007 5:01] Jan Kneschke
I can't reproduce the problem with the latest SVN code.

The script has to be adjusted with work with the svn-code. If you return SEND_RESULT you have to set proxy.response to send a hand-crafted result. If you just want to pass a result on to the client, return nothing.

Instead of the _G.query_id you can also just use 1 and 2 as the packet-id only has be be unique per client-query
[31 Aug 2007 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[10 Sep 2007 8:53] Vikas Jayna
I'm getting the same error while I'm using the mysql 5.0 library. In my case this error is appearing in an insert query that is inserting record in a table containing an auto_increment column as a result of which mysql-proxy crashes. If I remove the auto_increment from the column definition then mysql-proxy doesn't crash on this query.