Skip to content

fix(mysql): validate parameter count for prepared statements #3857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cvzx
Copy link

@cvzx cvzx commented May 14, 2025

Does your PR solve an issue?

fixes #3774

Is this a breaking change?

No. This PR adds proper error handling when the number of parameters provided doesn't match the number expected by a prepared statement in MySQL.

@cvzx cvzx force-pushed the fix/issue-3774-param-validation branch from 1d2eba4 to b1caf01 Compare May 14, 2025 02:22
Add validation to ensure the number of provided parameters matches the
expected count for MySQL prepared statements.
This prevents protocol errors by returning an error if the counts do not match before sending
the statement for execution.
@cvzx cvzx force-pushed the fix/issue-3774-param-validation branch from b1caf01 to 149a36c Compare May 14, 2025 02:45
Copy link
Contributor

@joeydewaal joeydewaal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned, I'm no maintainer but here is some feedback. Also, it is generally recommended to add a regression test when fixing an issue so that might be worth adding.

Comment on lines 126 to 132
if arguments.types.len() != metadata.parameters {
return Err(Error::Protocol(format!(
"Prepared statement expected {} parameters but {} parameters were provided",
metadata.parameters,
arguments.types.len()
)));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a maintainer but there is the err_protocol macro from sqlx-core which can be used here.

Comment on lines 148 to 154
if arguments.types.len() != metadata.parameters {
return Err(Error::Protocol(format!(
"Prepared statement expected {} parameters but {} parameters were provided",
metadata.parameters,
arguments.types.len()
)));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Replace direct Error::Protocol(format!()) calls with err_protocol!
macro in MySQL connection executor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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