Skip to content

Commit 3494c05

Browse files
waynexiaevenyag
authored andcommitted
fix: panic when received invalid query string (GreptimeTeam#5366)
Signed-off-by: Ruihang Xia <[email protected]>
1 parent 7fe7350 commit 3494c05

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/servers/src/mysql/federated.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,11 @@ pub(crate) fn check(
273273
) -> Option<Output> {
274274
// INSERT don't need MySQL federated check. We assume the query doesn't contain
275275
// federated or driver setup command if it starts with a 'INSERT' statement.
276-
if query.len() > 6 && query[..6].eq_ignore_ascii_case("INSERT") {
277-
return None;
276+
let the_6th_index = query.char_indices().nth(6).map(|(i, _)| i);
277+
if let Some(index) = the_6th_index {
278+
if query[..index].eq_ignore_ascii_case("INSERT") {
279+
return None;
280+
}
278281
}
279282

280283
// First to check the query is like "select @@variables".
@@ -295,6 +298,15 @@ mod test {
295298

296299
use super::*;
297300

301+
#[test]
302+
fn test_check_abnormal() {
303+
let session = Arc::new(Session::new(None, Channel::Mysql, Default::default()));
304+
let query = "🫣一点不正常的东西🫣";
305+
let output = check(query, QueryContext::arc(), session.clone());
306+
307+
assert!(output.is_none());
308+
}
309+
298310
#[test]
299311
fn test_check() {
300312
let session = Arc::new(Session::new(None, Channel::Mysql, Default::default()));

0 commit comments

Comments
 (0)