@@ -19,9 +19,9 @@ use crate::plugins::{Intercept, Plugin, PluginOutput, QueryLogger, TableAccess};
1919use crate :: pool:: PoolSettings ;
2020use crate :: sharding:: Sharder ;
2121
22- use std:: cmp;
2322use std:: collections:: BTreeSet ;
2423use std:: io:: Cursor ;
24+ use std:: { cmp, mem} ;
2525
2626/// Regexes used to parse custom commands.
2727const CUSTOM_SQL_REGEXES : [ & str ; 7 ] = [
@@ -141,6 +141,7 @@ impl QueryRouter {
141141 let mut message_cursor = Cursor :: new ( message_buffer) ;
142142
143143 let code = message_cursor. get_u8 ( ) as char ;
144+ let len = message_cursor. get_i32 ( ) as usize ;
144145
145146 // Check for any sharding regex matches in any queries
146147 match code as char {
@@ -150,9 +151,13 @@ impl QueryRouter {
150151 || self . pool_settings . sharding_key_regex . is_some ( )
151152 {
152153 // Check only the first block of bytes configured by the pool settings
153- let len = message_cursor. get_i32 ( ) as usize ;
154154 let seg = cmp:: min ( len - 5 , self . pool_settings . regex_search_limit ) ;
155- let initial_segment = String :: from_utf8_lossy ( & message_buffer[ 0 ..seg] ) ;
155+
156+ let query_start_index = mem:: size_of :: < u8 > ( ) + mem:: size_of :: < i32 > ( ) ;
157+
158+ let initial_segment = String :: from_utf8_lossy (
159+ & message_buffer[ query_start_index..query_start_index + seg] ,
160+ ) ;
156161
157162 // Check for a shard_id included in the query
158163 if let Some ( shard_id_regex) = & self . pool_settings . shard_id_regex {
@@ -192,7 +197,6 @@ impl QueryRouter {
192197 return None ;
193198 }
194199
195- let _len = message_cursor. get_i32 ( ) as usize ;
196200 let query = message_cursor. read_string ( ) . unwrap ( ) ;
197201
198202 let regex_set = match CUSTOM_SQL_REGEX_SET . get ( ) {
@@ -1291,6 +1295,11 @@ mod test {
12911295 // Shard should start out unset
12921296 assert_eq ! ( qr. active_shard, None ) ;
12931297
1298+ // Don't panic when short query eg. ; is sent
1299+ let q0 = simple_query ( ";" ) ;
1300+ assert ! ( qr. try_execute_command( & q0) == None ) ;
1301+ assert_eq ! ( qr. active_shard, None ) ;
1302+
12941303 // Make sure setting it works
12951304 let q1 = simple_query ( "/* shard_id: 1 */ select 1 from foo;" ) ;
12961305 assert ! ( qr. try_execute_command( & q1) == None ) ;
0 commit comments