Skip to content

Commit 016d0b1

Browse files
committed
fmt/rfc2822: fix panic in handling of whitespace
Skipping whitespace happens immediately before `input[0]`. But skipping the whitespace could lead to `input` being empty. We handle the empty case and add a regression test. Fixes #359
1 parent 3efed44 commit 016d0b1

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# CHANGELOG
22

3-
0.2.12 (TBD)
4-
============
5-
TODO
3+
0.2.12 (2025-05-03)
4+
===================
5+
This release fixes a couple bugs in Jiff's parser. In particular, a regression
6+
was introduced in `jiff 0.2.11` where its RFC 2822 parser could panic on some
7+
inputs. Previous releases of Jiff are unaffected.
68

79
Bug fixes:
810

911
* [#357](https://github.com/BurntSushi/jiff/issues/357):
1012
Fix a bug where parsing `1970-06-01T00-00:45:00[Africa/Monrovia]` succeeded
1113
but it should fail.
14+
* [#359](https://github.com/BurntSushi/jiff/issues/359):
15+
Fix a bug where the RFC 2822 parser could panic on some inputs.
1216

1317

1418
0.2.11 (2025-05-01)

src/fmt/rfc2822.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -554,15 +554,19 @@ impl DateTimeParser {
554554
}
555555
};
556556
let Parsed { input, .. } = self.skip_whitespace(&input[3..]);
557-
if input[0] != b',' {
557+
let Some(should_be_comma) = input.get(0).copied() else {
558558
return Err(err!(
559-
"expected day at beginning of RFC 2822 datetime \
560-
since first non-whitespace byte, {first:?}, \
561-
is not a digit, but found {got:?} after parsed \
562-
weekday {wd:?} and expected a comma",
563-
first = escape::Byte(b1),
564-
got = escape::Byte(input[0]),
565-
wd = escape::Bytes(&[b1, b2, b3]),
559+
"expected comma after parsed weekday `{weekday}` in \
560+
RFC 2822 datetime, but found end of string instead",
561+
weekday = escape::Bytes(&[b1, b2, b3]),
562+
));
563+
};
564+
if should_be_comma != b',' {
565+
return Err(err!(
566+
"expected comma after parsed weekday `{weekday}` in \
567+
RFC 2822 datetime, but found `{got:?}` instead",
568+
weekday = escape::Bytes(&[b1, b2, b3]),
569+
got = escape::Byte(should_be_comma),
566570
));
567571
}
568572
let Parsed { input, .. } = self.skip_whitespace(&input[1..]);
@@ -1786,6 +1790,18 @@ mod tests {
17861790
p("Wed"),
17871791
@r###"failed to parse RFC 2822 datetime into Jiff zoned datetime: expected day at beginning of RFC 2822 datetime since first non-whitespace byte, "W", is not a digit, but given string is too short (length is 3)"###,
17881792
);
1793+
insta::assert_snapshot!(
1794+
p("Wed "),
1795+
@"failed to parse RFC 2822 datetime into Jiff zoned datetime: expected comma after parsed weekday `Wed` in RFC 2822 datetime, but found end of string instead",
1796+
);
1797+
insta::assert_snapshot!(
1798+
p("Wed ,"),
1799+
@"failed to parse RFC 2822 datetime into Jiff zoned datetime: expected day, but found end of input",
1800+
);
1801+
insta::assert_snapshot!(
1802+
p("Wed , "),
1803+
@"failed to parse RFC 2822 datetime into Jiff zoned datetime: expected day, but found end of input",
1804+
);
17891805
insta::assert_snapshot!(
17901806
p("Wat, "),
17911807
@r###"failed to parse RFC 2822 datetime into Jiff zoned datetime: expected day at beginning of RFC 2822 datetime since first non-whitespace byte, "W", is not a digit, but did not recognize "Wat" as a valid weekday abbreviation"###,

0 commit comments

Comments
 (0)