Skip to content

Commit 15b12e8

Browse files
authored
Merge pull request #327 from stephen-hlx/fix-StmtParamsMismatch-in-ExecRoutine
Add `DriverError::StmtParamsNumberExceedsLimit`
2 parents 7e57a8a + c575708 commit 15b12e8

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/conn/routines/exec.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use mysql_common::{packets::ComStmtExecuteRequestBuilder, params::Params};
66
#[cfg(feature = "tracing")]
77
use tracing::{field, info_span, Level, Span};
88

9-
use crate::{BinaryProtocol, Conn, DriverError, Statement};
9+
use crate::{conn::MAX_STATEMENT_PARAMS, BinaryProtocol, Conn, DriverError, Statement};
1010

1111
use super::Routine;
1212

@@ -52,10 +52,16 @@ impl Routine<()> for ExecRoutine<'_> {
5252
Span::current().record("mysql_async.query.params", ps);
5353
}
5454

55+
if params.len() > MAX_STATEMENT_PARAMS {
56+
Err(DriverError::StmtParamsNumberExceedsLimit {
57+
supplied: params.len(),
58+
})?
59+
}
60+
5561
if self.stmt.num_params() as usize != params.len() {
5662
Err(DriverError::StmtParamsMismatch {
5763
required: self.stmt.num_params(),
58-
supplied: params.len() as u16,
64+
supplied: params.len(),
5965
})?
6066
}
6167

src/error/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ use std::{io, result};
2121
/// Result type alias for this library.
2222
pub type Result<T> = result::Result<T, Error>;
2323

24+
/// The maximum number of bind variables supported by MySQL.
25+
/// https://stackoverflow.com/questions/4922345/how-many-bind-variables-can-i-use-in-a-sql-query-in-mysql-5#comment136409462_11131824
26+
pub(crate) const MAX_STATEMENT_PARAMS: usize = u16::MAX as usize;
27+
2428
/// This type enumerates library errors.
2529
#[derive(Debug, Error)]
2630
pub enum Error {
@@ -135,7 +139,14 @@ pub enum DriverError {
135139
required,
136140
supplied
137141
)]
138-
StmtParamsMismatch { required: u16, supplied: u16 },
142+
StmtParamsMismatch { required: u16, supplied: usize },
143+
144+
#[error(
145+
"MySQL supports up to {} parameters but {} was supplied.",
146+
MAX_STATEMENT_PARAMS,
147+
supplied
148+
)]
149+
StmtParamsNumberExceedsLimit { supplied: usize },
139150

140151
#[error("Unexpected packet.")]
141152
UnexpectedPacket { payload: Vec<u8> },

0 commit comments

Comments
 (0)