Skip to content

Commit

Permalink
fix: off by 1 error in Method::from_bytes causing an allocation (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlzht authored Dec 3, 2024
1 parent 5a1a5e8 commit 92d4148
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 92d4148

Please sign in to comment.