From 4d14b6887e6882b9d432f45764496ff36f5d8935 Mon Sep 17 00:00:00 2001 From: dlzht Date: Mon, 1 Jul 2024 23:46:48 +0800 Subject: [PATCH 1/2] fix: off by 1 error in Method::from_bytes --- src/method.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/method.rs b/src/method.rs index 94e4d4ec..c3fc3448 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,5 +465,18 @@ 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); + + // if these two assert_eq! fail, the output message may not be helpful, because type info + // of Method.Inner is not printed + 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())) + ); } } From d71cf0f866d4ebea5a1936d21a2a3b87671fc6ba Mon Sep 17 00:00:00 2001 From: dlzht Date: Tue, 2 Jul 2024 09:24:05 +0800 Subject: [PATCH 2/2] chore: code style fix --- src/method.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/method.rs b/src/method.rs index c3fc3448..4df92bd6 100644 --- a/src/method.rs +++ b/src/method.rs @@ -466,17 +466,19 @@ 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); - // if these two assert_eq! fail, the output message may not be helpful, because type info - // of Method.Inner is not printed 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())) + 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())) + Method(ExtensionAllocated( + AllocatedExtension::new(&shortest_allocated_method).unwrap() + )) ); } }