Skip to content

Commit

Permalink
Fix reading small or empty slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jan 6, 2025
1 parent ec80c34 commit ab0a75a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "tl-proto"
description = "A collection of traits for working with TL serialization/deserialization"
authors = ["Ivan Kalinin <[email protected]>"]
repository = "https://github.com/broxus/tl-proto"
version = "0.4.10"
version = "0.4.11"
edition = "2021"
include = ["src/**/*.rs", "README.md"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion scheme/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub struct Constructor<'a> {
pub output: OutputType<'a>,
}

impl<'a> Constructor<'a> {
impl Constructor<'_> {
pub fn as_normalized(&self) -> String {
NormalizedConstructor(self).to_string()
}
Expand Down
2 changes: 1 addition & 1 deletion src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where

struct HashWriter<'a>(pub &'a mut dyn Hasher);

impl<'a> TlPacket for HashWriter<'a> {
impl TlPacket for HashWriter<'_> {
const TARGET: TlTarget = TlTarget::Hasher;

#[inline(always)]
Expand Down
15 changes: 14 additions & 1 deletion src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ fn read_bytes<'a>(packet: &'a [u8], offset: &mut usize) -> TlResult<&'a [u8]> {
#[inline(always)]
fn compute_bytes_meta(packet: &[u8], offset: usize) -> TlResult<(usize, usize, usize)> {
let packet_len = packet.len();
if unlikely(packet_len <= offset + 4) {
if unlikely(packet_len < offset + 4) {
return Err(TlError::UnexpectedEof);
}

Expand All @@ -750,3 +750,16 @@ fn compute_bytes_meta(packet: &[u8], offset: usize) -> TlResult<(usize, usize, u
}

const SIZE_MAGIC: u8 = 254;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn read_small_slice() {
assert_eq!(read_bytes(&[0, 0, 0, 0], &mut 0).unwrap(), &[]);
assert_eq!(read_bytes(&[1, 123, 0, 0], &mut 0).unwrap(), &[123]);
assert_eq!(read_bytes(&[2, 123, 3, 0], &mut 0).unwrap(), &[123, 3]);
assert_eq!(read_bytes(&[3, 123, 3, 2], &mut 0).unwrap(), &[123, 3, 2]);
}
}

0 comments on commit ab0a75a

Please sign in to comment.