mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-28 03:06:29 +00:00
Check Slice bounds in read_message to avoid panics (#371)
When recv is called in the mirroring client, we noticed an occasional panic when reading the message. thread 'tokio-runtime-worker' panicked at 'slice index starts at 5 but ends at 0', src/messages.rs:522:18 We are still debugging the reason why this happens but adding a check for slice bounds seems like a good idea. Instead of panicking, this will return an Err to the caller which will close the connection.
This commit is contained in:
committed by
GitHub
parent
ac21ce50f1
commit
d66b377a8e
@@ -517,14 +517,18 @@ where
|
|||||||
|
|
||||||
bytes.resize(bytes.len() + len as usize - mem::size_of::<i32>(), b'0');
|
bytes.resize(bytes.len() + len as usize - mem::size_of::<i32>(), b'0');
|
||||||
|
|
||||||
match stream
|
let slice_start = mem::size_of::<u8>() + mem::size_of::<i32>();
|
||||||
.read_exact(
|
let slice_end = slice_start + len as usize - mem::size_of::<i32>();
|
||||||
&mut bytes[mem::size_of::<u8>() + mem::size_of::<i32>()
|
|
||||||
..mem::size_of::<u8>() + mem::size_of::<i32>() + len as usize
|
// Avoids a panic
|
||||||
- mem::size_of::<i32>()],
|
if slice_end < slice_start {
|
||||||
)
|
return Err(Error::SocketError(format!(
|
||||||
.await
|
"Error reading message from socket - Code: {:?} - Length {:?}, Error: {:?}",
|
||||||
{
|
code, len, "Unexpected length value for message"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
match stream.read_exact(&mut bytes[slice_start..slice_end]).await {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(Error::SocketError(format!(
|
return Err(Error::SocketError(format!(
|
||||||
|
|||||||
Reference in New Issue
Block a user