From 92d4148a6490823dc285c8cc240d750e08f88d6a Mon Sep 17 00:00:00 2001 From: dlzht <78463157+dlzht@users.noreply.github.com> Date: Tue, 3 Dec 2024 21:43:42 +0800 Subject: [PATCH] fix: off by 1 error in Method::from_bytes causing an allocation (#708) --- src/method.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/method.rs b/src/method.rs index 3d45ef9f..7b4584ab 100644 --- a/src/method.rs +++ b/src/method.rs @@ -123,7 +123,7 @@ impl Method { _ => Method::extension_inline(src), }, _ => { - if src.len() < InlineExtension::MAX { + if src.len() <= InlineExtension::MAX { Method::extension_inline(src) } else { let allocated = AllocatedExtension::new(src)?; @@ -465,6 +465,21 @@ mod test { let long_method = "This_is_a_very_long_method.It_is_valid_but_unlikely."; assert_eq!(Method::from_str(long_method).unwrap(), long_method); + + let longest_inline_method = [b'A'; InlineExtension::MAX]; + assert_eq!( + Method::from_bytes(&longest_inline_method).unwrap(), + Method(ExtensionInline( + InlineExtension::new(&longest_inline_method).unwrap() + )) + ); + let shortest_allocated_method = [b'A'; InlineExtension::MAX + 1]; + assert_eq!( + Method::from_bytes(&shortest_allocated_method).unwrap(), + Method(ExtensionAllocated( + AllocatedExtension::new(&shortest_allocated_method).unwrap() + )) + ); } #[test]