Skip to content

Commit

Permalink
Don't panic if passed in a large max age.
Browse files Browse the repository at this point in the history
Duration will panic if the number of seconds is greater than
2^63/1000. This just caps the Max-Age to the highest value we
can parse.
  • Loading branch information
erickt committed May 7, 2017
1 parent 32d74f8 commit ee18b79
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/parse.rs
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::cmp;
use std::error::Error;
use std::ascii::AsciiExt;
use std::str::Utf8Error;
Expand Down Expand Up @@ -152,7 +153,12 @@ fn parse_inner<'c>(s: &str, decode: bool) -> Result<Cookie<'c>, ParseError> {
// max age as 0 seconds.
cookie.max_age = match v.parse() {
Ok(val) if val <= 0 => Some(Duration::zero()),
Ok(val) => Some(Duration::seconds(val)),
Ok(val) => {
// Don't panic if the max age seconds is greater than what's supported by
// `Duration`.
let val = cmp::min(val, Duration::max_value().num_seconds());
Some(Duration::seconds(val))
}
Err(_) => continue,
};
}
Expand Down Expand Up @@ -392,4 +398,13 @@ mod tests {

assert_eq!(cookie, expected);
}

#[test]
fn do_not_panic_on_large_max_ages() {
let max_seconds = Duration::max_value().num_seconds();
let expected = Cookie::build("foo", "bar")
.max_age(Duration::seconds(max_seconds))
.finish();
assert_eq_parse!(format!(" foo=bar; Max-Age={:?}", max_seconds + 1), expected);
}
}

0 comments on commit ee18b79

Please sign in to comment.