Skip to content

Commit

Permalink
Pandoc fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Mar 5, 2024
1 parent 206ffcf commit 99ca650
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
18 changes: 18 additions & 0 deletions fuzz/fuzz_targets/pandoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ fuzz_target!(|text: String| {
return;
}

if text.contains("<") {
return;
}
if text.contains("[^ ") {
return;
}
Expand Down Expand Up @@ -111,9 +114,24 @@ fuzz_target!(|text: String| {
}
let pulldown_cmark_events = normalize_pandoc(pulldown_cmark_events);
let mut footstack = vec![];
let mut liststack = 0;
for event in &pulldown_cmark_events {
use pulldown_cmark::{Event, Tag, TagEnd};
match event {
Event::Start(Tag::CodeBlock(..)) => {
// differences in list tightness
if liststack != 0 {
return;
}
}
Event::Start(Tag::Item) => {
// differences in list tightness
liststack += 1;
}
Event::End(TagEnd::Item) => {
// differences in list tightness
liststack -= 1;
}
Event::Start(Tag::FootnoteDefinition(id)) => {
footstack.push(id.to_string());
}
Expand Down
39 changes: 1 addition & 38 deletions fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ pub fn normalize_pandoc(events: Vec<Event<'_>>) -> Vec<Event<'_>> {
for event in normalized.iter_mut() {
match event {
// Pandoc collapses spaces
Event::Text(p) if p.contains(" ") || p.contains("\n") => {
Event::Text(p) | Event::InlineMath(p) | Event::DisplayMath(p) if p.contains(" ") || p.contains("\n") => {
let mut st = String::with_capacity(p.len());
let mut prev_char_is_space = false;
for c in p.chars() {
Expand All @@ -599,29 +599,6 @@ pub fn normalize_pandoc(events: Vec<Event<'_>>) -> Vec<Event<'_>> {
Event::Start(Tag::CodeBlock(fence)) => {
*fence = CodeBlockKind::Fenced("".into());
}
Event::InlineMath(p) if p.contains(" ") || p.contains("\n") => {
let mut st = String::with_capacity(p.len());
let mut prev_char_is_space = false;
for c in p.chars() {
if c == ' ' {
if prev_char_is_space {
continue;
}
prev_char_is_space = true;
st.push(' ');
} else if c == '\n' {
if prev_char_is_space {
continue;
}
prev_char_is_space = true;
st.push('\n');
} else {
prev_char_is_space = false;
st.push(c);
}
}
*p = st.into();
}
_ => {},
}
}
Expand Down Expand Up @@ -891,20 +868,6 @@ pub fn normalize(events: Vec<Event<'_>>) -> Vec<Event<'_>> {
(Some(Event::Text(prev)), Event::Text(next)) => *prev = format!("{prev}{next}").into(),
(Some(Event::Html(prev)), Event::Html(next)) => *prev = format!("{prev}{next}").into(),

// commonmark.js wraps non-empty list items in a paragraph.
(Some(Event::Start(Tag::Item)), next)
if next != &Event::Start(Tag::Paragraph) && next != &Event::End(TagEnd::Item) =>
{
normalized.push(Event::Start(Tag::Paragraph));
normalized.push(event);
}
(Some(prev), Event::End(TagEnd::Item))
if prev != &Event::End(TagEnd::Paragraph) && prev != &Event::Start(Tag::Item) =>
{
normalized.push(Event::End(TagEnd::Paragraph));
normalized.push(event);
}

// commonmark.js always adds a final newline to code blocks.
(Some(Event::Text(prev)), Event::End(TagEnd::CodeBlock)) => {
*prev = prev.trim_end().to_string().into();
Expand Down

0 comments on commit 99ca650

Please sign in to comment.