sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

MySQL query returns "(1835) Malformed communication packet."

Open Srlion opened this issue 1 year ago • 2 comments

I have found these related issues/pull requests

N/A

Description

Second query returns that error (1835) Malformed communication packet.

It should return a MySQL error but it returns that message, it only happens if you supply no parameters when it expected parameters.

Reproduction steps

main.rs

use sqlx::mysql::MySqlConnection;
use sqlx::{Connection, Error};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let database_url = "";

    let mut conn = MySqlConnection::connect(database_url).await?;

    sqlx::query("CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY AUTO_INCREMENT, value TEXT);")
        .execute(&mut conn)
        .await?;

    let res = sqlx::query("INSERT INTO`test_table`(`value`,`id`)VALUES('test',?)ON DUPLICATE KEY UPDATE`value`=VALUES(`value`);").execute(&mut conn).await;

    println!("Query result: {:?}", res);

    Ok(())
}

Cargo.toml

[package]
name = "sqlx-bug"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.43.0", default-features = false, features = [
    "rt-multi-thread",
    "macros",
] }
anyhow = "1.0.96"
sqlx = { version = "0.8.3", features = [
    "runtime-tokio",
    "tls-native-tls",
    "mysql",
    "rust_decimal",
    "chrono",
] }

SQLx version

0.8.3

Enabled SQLx features

"runtime-tokio", "tls-native-tls", "mysql", "rust_decimal", "chrono",

Database server and version

MySQL 8.0.4

Operating system

Windows

Rust version

rustc 1.84.1

Srlion avatar Mar 09 '25 01:03 Srlion

This likely means we need to check that the number of arguments matches the number of parameters expected before we send the COM_STMT_EXECUTE:

  • https://github.com/launchbadge/sqlx/blob/ca3a5090369238d156eb859d3fd699d86681f73f/sqlx-mysql/src/connection/executor.rs#L127
  • https://github.com/launchbadge/sqlx/blob/ca3a5090369238d156eb859d3fd699d86681f73f/sqlx-mysql/src/connection/executor.rs#L141

abonander avatar Mar 09 '25 21:03 abonander

It seems that this error is emitted in general when not enough parameters are provided, not just when no parameters are given. Error like Prepared statement expected X parameters but Y parameters where provided would be nice.

esamattirs avatar Mar 20 '25 13:03 esamattirs