Bug #120566 Raw COM_STMT_PREPARE triggers send_statement(): `CountVisibleFields(param_list) == stmt->m_param_count`
Submitted: 29 May 7:12
Reporter: Hongbo Feng Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Prepared statements Severity:S6 (Debug Builds)
Version:9.6.0-debug-asan OS:Ubuntu (22.04)
Assigned to: CPU Architecture:x86 (x86_64)
Tags: assertion, com-stmt-prepare, crash, derived-table, prepared-statement, protocol

[29 May 7:12] Hongbo Feng
Description:
I found a server-side assertion failure while testing MySQL prepared statements through the classic MySQL protocol.

The reproducer creates a small schema with normal SQL, then sends a raw `COM_STMT_PREPARE` packet directly to the authenticated socket. The prepared SQL contains an `EXISTS` subquery over derived-table expressions and parameter markers.

I expected the server either to prepare the statement or to reject it with a normal SQL/protocol error. Instead, the server aborted with an assertion failure.

The saved server error log starts with:

```text
mysqld: /home/fhb/mysql-server/sql/sql_prepare.cc:972: bool send_statement(...): Assertion `CountVisibleFields(param_list) == stmt->m_param_count' failed.
```

Backtrace excerpt:

```text
#10 send_statement at /home/fhb/mysql-server/sql/sql_prepare.cc:972
#11 Prepared_statement::prepare_query at /home/fhb/mysql-server/sql/sql_prepare.cc:1438
#12 Prepared_statement::prepare at /home/fhb/mysql-server/sql/sql_prepare.cc:2599
#13 mysqld_stmt_prepare at /home/fhb/mysql-server/sql/sql_prepare.cc:1550
#14 dispatch_command at /home/fhb/mysql-server/sql/sql_parse.cc:2074
```

How to repeat:
1. Start a debug/ASan MySQL server build.
2. Install Python dependencies:

   ```bash
   pip install pymysql mysql-connector-python
   ```

3. Run the attached reproducer against the test server:

   ```bash
   python3 bug_reproduce_1.py -h <host> -P <port> -u <user> -p <password> -d bug1_repro
   ```

4. The script runs the setup SQL and then sends one raw `COM_STMT_PREPARE` packet directly through the socket.

Observed output:

```text
Raw send packet_0_stmt32.bin: COM_STMT_PREPARE payload_len=977
Connection lost while waiting for packet_0_stmt32.bin: server closed the connection
BUG CONFIRMED: server appears to be down
```