diff --git a/src/header/mod.rs b/src/header/mod.rs index 2517eb06..6c1b4955 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -169,4 +169,4 @@ pub use self::name::{ /// Generally, 64kb for a header name is WAY too much than would ever be needed /// in practice. Restricting it to this size enables using `u16` values to /// represent offsets when dealing with header names. -const MAX_HEADER_NAME_LEN: usize = 1 << 16; +const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1; diff --git a/src/header/name.rs b/src/header/name.rs index 4d70ff30..775f75c4 100644 --- a/src/header/name.rs +++ b/src/header/name.rs @@ -1116,10 +1116,6 @@ fn parse_hdr<'a>( ($d:ident, $src:ident, 35) => { to_lower!($d, $src, 34); $d[34] = table[$src[34] as usize]; }; } - assert!(len < super::MAX_HEADER_NAME_LEN, - "header name too long -- max length is {}", - super::MAX_HEADER_NAME_LEN); - match len { 0 => Err(InvalidHeaderName::new()), 2 => { @@ -1509,17 +1505,16 @@ fn parse_hdr<'a>( validate(b, len) } } - _ => { - if len < 64 { - for i in 0..len { - b[i] = table[data[i] as usize]; - } - - validate(b, len) - } else { - Ok(HdrName::custom(data, false)) + len if len < 64 => { + for i in 0..len { + b[i] = table[data[i] as usize]; } + validate(b, len) + } + len if len <= super::MAX_HEADER_NAME_LEN => { + Ok(HdrName::custom(data, false)) } + _ => Err(InvalidHeaderName::new()), } } @@ -2156,6 +2151,24 @@ mod tests { } } + #[test] + fn test_invalid_name_lengths() { + assert!( + HeaderName::from_bytes(&[]).is_err(), + "zero-length header name is an error", + ); + let mut long = vec![b'a'; super::super::MAX_HEADER_NAME_LEN]; + assert!( + HeaderName::from_bytes(long.as_slice()).is_ok(), + "max header name length is ok", + ); + long.push(b'a'); + assert!( + HeaderName::from_bytes(long.as_slice()).is_err(), + "longer than max header name length is an error", + ); + } + #[test] fn test_from_hdr_name() { use self::StandardHeader::Vary;