Skip to content

Commit

Permalink
fix: harden again manufactured input which could previously trigger a…
Browse files Browse the repository at this point in the history
…ssertion failures. (#91)
  • Loading branch information
Byron committed Dec 11, 2024
1 parent b526d7c commit a368f0f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ where
classes,
attrs,
} => {
assert_eq!(state.current_heading, None);
state.current_heading = Some(self::Heading {
id: id.as_ref().map(|id| id.clone().into()),
classes: classes.iter().map(|class| class.clone().into()).collect(),
Expand Down Expand Up @@ -554,34 +553,40 @@ where
}
}
End(tag) => match tag {
TagEnd::Link => match state.link_stack.pop().unwrap() {
LinkCategory::AngleBracketed => formatter.write_char('>'),
LinkCategory::Reference { uri, title, id } => {
state
.shortcuts
.push((id.to_string(), uri.to_string(), title.to_string()));
formatter.write_str("][")?;
formatter.write_str(&id)?;
formatter.write_char(']')
}
LinkCategory::Collapsed { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
TagEnd::Link => {
let Some(category) = state.link_stack.pop() else {
// This should always be present, but people can provide any kind of event stream.
return Ok(());
};
match category {
LinkCategory::AngleBracketed => formatter.write_char('>'),
LinkCategory::Reference { uri, title, id } => {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
.push((id.to_string(), uri.to_string(), title.to_string()));
formatter.write_str("][")?;
formatter.write_str(&id)?;
formatter.write_char(']')
}
formatter.write_str("][]")
}
LinkCategory::Shortcut { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
LinkCategory::Collapsed { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
}
formatter.write_str("][]")
}
formatter.write_char(']')
LinkCategory::Shortcut { uri, title } => {
if let Some(shortcut_text) = state.current_shortcut_text.take() {
state
.shortcuts
.push((shortcut_text, uri.to_string(), title.to_string()));
}
formatter.write_char(']')
}
LinkCategory::Other { uri, title } => close_link(&uri, &title, formatter, LinkType::Inline),
}
LinkCategory::Other { uri, title } => close_link(&uri, &title, formatter, LinkType::Inline),
},
}
TagEnd::Image => match state.image_stack.pop().unwrap() {
ImageLink::Reference { uri, title, id } => {
state
Expand Down Expand Up @@ -614,11 +619,15 @@ where
TagEnd::Emphasis => formatter.write_char(options.emphasis_token),
TagEnd::Strong => formatter.write_str(options.strong_token),
TagEnd::Heading(_) => {
let Some(heading) = state.current_heading.take() else {
// Harden against manufactured input.
return Ok(());
};
let self::Heading {
id,
classes,
attributes,
} = state.current_heading.take().unwrap();
} = heading;
let emit_braces = id.is_some() || !classes.is_empty() || !attributes.is_empty();
if emit_braces {
formatter.write_str(" {")?;
Expand Down
26 changes: 26 additions & 0 deletions tests/integrate/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
mod display;
mod fmt;
mod spec;
mod fuzzed {
use pulldown_cmark::{Event, HeadingLevel, Tag, TagEnd};
use pulldown_cmark_to_cmark::cmark_resume;

#[test]
fn cmark_resume_with_options_does_not_panic() {
let events = [
Event::Start(Tag::Heading {
level: HeadingLevel::H2,
id: None,
classes: vec![],
attrs: vec![],
}),
Event::Start(Tag::Heading {
level: HeadingLevel::H2,
id: None,
classes: vec![],
attrs: vec![],
}),
Event::Text(pulldown_cmark::CowStr::Borrowed("(")),
Event::End(TagEnd::Heading(HeadingLevel::H2)),
Event::End(TagEnd::Heading(HeadingLevel::H2)),
];
let _ = cmark_resume(events.iter(), String::new(), None);
}
}

#[cfg(test)]
mod calculate_code_block_token_count {
Expand Down

0 comments on commit a368f0f

Please sign in to comment.