From 53585acd63716d64bf8dc01909661f80a13e9e8a Mon Sep 17 00:00:00 2001 From: Marek Kuskowski <50183564+nylonicious@users.noreply.github.com> Date: Mon, 23 Dec 2024 20:10:40 +0100 Subject: [PATCH 1/3] Remove byteorder, use bytes::Buf::get_uint instead --- Cargo.toml | 1 - src/protocol/frame/frame.rs | 16 ++++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1fe90f09..244c1a06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,6 @@ __rustls-tls = ["rustls", "rustls-pki-types"] [dependencies] data-encoding = { version = "2", optional = true } -byteorder = "1.3.2" bytes = "1.9.0" http = { version = "1.0", optional = true } httparse = { version = "1.3.4", optional = true } diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 31a4c004..88806658 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -1,4 +1,3 @@ -use byteorder::{NetworkEndian, ReadBytesExt}; use log::*; use std::{ default::Default, @@ -18,7 +17,7 @@ use crate::{ error::{Error, ProtocolError, Result}, protocol::frame::Utf8Bytes, }; -use bytes::{Bytes, BytesMut}; +use bytes::{Buf, Bytes, BytesMut}; /// A struct representing the close command. #[derive(Debug, Clone, Eq, PartialEq)] @@ -161,14 +160,11 @@ impl FrameHeader { let length_byte = second & 0x7F; let length_length = LengthFormat::for_byte(length_byte).extra_bytes(); if length_length > 0 { - match cursor.read_uint::(length_length) { - Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => { - return Ok(None); - } - Err(err) => { - return Err(err.into()); - } - Ok(read) => read, + let mut buffer = [0; 8]; + match cursor.read_exact(&mut buffer[..length_length]) { + Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => return Ok(None), + Err(err) => return Err(err.into()), + Ok(()) => buffer[..length_length].as_ref().get_uint(length_length), } } else { u64::from(length_byte) From cce13fa40a7c89020a37a891ce0724c7b9299123 Mon Sep 17 00:00:00 2001 From: Marek Kuskowski <50183564+nylonicious@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:25:00 +0100 Subject: [PATCH 2/3] avoid unecessary copy inside bytes::Buf::get_uint --- src/protocol/frame/frame.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 88806658..5df87347 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -17,7 +17,7 @@ use crate::{ error::{Error, ProtocolError, Result}, protocol::frame::Utf8Bytes, }; -use bytes::{Buf, Bytes, BytesMut}; +use bytes::{Bytes, BytesMut}; /// A struct representing the close command. #[derive(Debug, Clone, Eq, PartialEq)] @@ -160,11 +160,18 @@ impl FrameHeader { let length_byte = second & 0x7F; let length_length = LengthFormat::for_byte(length_byte).extra_bytes(); if length_length > 0 { - let mut buffer = [0; 8]; - match cursor.read_exact(&mut buffer[..length_length]) { + const SIZE: usize = mem::size_of::(); + let mut buffer = [0; SIZE]; + let start = match SIZE.checked_sub(length_length) { + Some(start) => start, + None => { + panic!("the integer can fit {} bytes, but {} is given", SIZE, length_length) + } + }; + match cursor.read_exact(&mut buffer[start..]) { Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => return Ok(None), Err(err) => return Err(err.into()), - Ok(()) => buffer[..length_length].as_ref().get_uint(length_length), + Ok(()) => u64::from_be_bytes(buffer), } } else { u64::from(length_byte) From 6e61fc9b0b1057b943761b1b48107c2f3b0ea16a Mon Sep 17 00:00:00 2001 From: Marek Kuskowski <50183564+nylonicious@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:33:52 +0100 Subject: [PATCH 3/3] use assert! to verify length number --- src/protocol/frame/frame.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 5df87347..baede5bf 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -161,13 +161,9 @@ impl FrameHeader { let length_length = LengthFormat::for_byte(length_byte).extra_bytes(); if length_length > 0 { const SIZE: usize = mem::size_of::(); + assert!(length_length <= SIZE, "length exceeded size of u64"); + let start = SIZE - length_length; let mut buffer = [0; SIZE]; - let start = match SIZE.checked_sub(length_length) { - Some(start) => start, - None => { - panic!("the integer can fit {} bytes, but {} is given", SIZE, length_length) - } - }; match cursor.read_exact(&mut buffer[start..]) { Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => return Ok(None), Err(err) => return Err(err.into()),