Skip to content

into_offset_iter() panics (Option::unwrap on None, parse.rs:2199) on a 9-character document, no options required #1129

Closed

Description

@chattersley

Parser::into_offset_iter() panics on a nine-character document. Plain iteration over the same parser is fine, and no options are required — Options::empty() is enough.

thread 'main' panicked at pulldown-cmark-0.13.4/src/parse.rs:2199:47:
called `Option::unwrap()` on a `None` value

Reproduction

use pulldown_cmark::{Options, Parser};

fn main() {
    let input = "+\t[\u{0}]:d\n\u{b}";

    // Fine — two events.
    println!("plain: {}", Parser::new_ext(input, Options::empty()).count());

    // Panics.
    let n = Parser::new_ext(input, Options::empty())
        .into_offset_iter()
        .count();
    println!("offsets: {n}");
}

Output:

plain: 2
thread 'main' panicked at .../pulldown-cmark-0.13.4/src/parse.rs:2199:47:
called `Option::unwrap()` on a `None` value

The document is a list item containing what looks like a link reference definition with a NUL in its label, followed by a vertical tab:

+ \t list marker and tab
[\u{0}]:d link reference definition, NUL in the label
\n \u{b} newline, then U+000B

What narrows it down

Only the offset iterator panics. Parser::next() walks the same document to completion and yields two events; into_offset_iter() panics. That points at the range bookkeeping rather than at block or inline parsing.

No options needed. Options::empty(), ENABLE_STRIKETHROUGH and Options::all() all panic identically, so this is not extension-specific.

The input is minimal. Every single-character deletion I tried stops the panic:

Variant Result
+\t[\0]:d\n\u{b} panics
+\t[]:d\n\u{b} (no NUL) ok
+\t[\0]:d\n (no VT) ok
+[\0]:d\n\u{b} (no tab) ok
\t[\0]:d\n\u{b} (no +) ok
+\t[\0]:\n\u{b} (no destination) ok

So it needs the list marker, the tab, the NUL in the label, the destination, and the trailing vertical tab together.

Version

pulldown-cmark 0.13.4, Rust 1.90 on Windows 11. Also reproduces on Linux (found there first).

How it was found

A fuzz target over a Markdown editor's segmenting layer, which uses into_offset_iter() to recover byte ranges for every event. libFuzzer reached it in about four minutes; the case above is its minimised output.

Relationship to #1084

Probably not a duplicate, though possibly the same family. #1084 is at parse.rs:2367, needs ENABLE_TASKLISTS and ENABLE_STRIKETHROUGH, and panics through push_html — i.e. plain iteration. This one is at 2199, needs no options, and does not panic under plain iteration at all. Both inputs do contain a [...]: link reference definition with a NUL, so a common cause would not be surprising.

Happy to test a patch.

Activity

  1. scriswell commented on Sep 29, 2026

    @scriswell

    Another reproduction of the same panic, with only printable characters and whitespace (no NUL or vertical tab), on 0.13.4:

    pulldown_cmark::Parser::new("- [o]:u\n    \t").into_offset_iter().count(); // panics at parse.rs:2199
    pulldown_cmark::Parser::new("- [o]:u\n    \t").count();                    // fine

    It's a list item holding only a link reference definition, followed by a whitespace-only line with a tab past the item's indent. The same happens with 1. and * markers, inside a > block quote, after another list item, with CRLF line endings, and with \t\t. We found it by fuzzing random edits in an editor that re-parses on every keystroke; we're guarding it with catch_unwind for now.

  2. richardguenther commented on Oct 16, 2026

    @richardguenther

    The same panic here, and I think it is already fixed on main, just not released. A one-hunk backport to branch_0.13 fixes it, and a regression test is below.

    Reproducers (0.13.4; Options::empty(), ENABLE_TABLES and Options::all() behave the same):

    use pulldown_cmark::{Options, Parser};
    
    fn main() {
        for body in [
            "- [o]:u\n    \t",
            "> - [o]:u\n>     \t",
            "> - [o]:u\n> \u{c}",
            "+\t[\0]:d\n\u{b}", // the body from this issue
        ] {
            // panics at parse.rs:2199:47: called `Option::unwrap()` on a `None` value
            let _ = Parser::new_ext(body, Options::empty()).into_offset_iter().count();
        }
    }
    into_offset_iter() plain Parser / push_html
    0.13.4 and branch_0.13 at 21a182d panics no panic, but the events end early: "- [o]:u\n \t" gives <ul>\n<li> with no closing tags
    main at ad1622b ok ok: <ul>\n<li></li>\n</ul>\n

    Cause. After a link reference definition, scan_next_line_or_lazy_continuation (firstpass.rs:417 in 0.13.4) takes a line that is white space after its container markers as a lazy continuation. The list item then gets a TightParagraph with no children. OffsetIter::next calls tree.cur().unwrap() on it (parse.rs:2199). #1096 handled the same case in Parser::next by returning None. That avoids the panic, but it ends the whole event stream, which explains the missing closing tags above.

    Fix on main. edee73b7 ("Prevent spurious <br> at <p> start after four-space blank line", for #997) ends that paragraph on a blank line (firstpass.rs:526 on main). Bisecting the commits main has over 0.13.4 lands on this commit. Its hunk applies unchanged to branch_0.13, whose firstpass.rs is identical to 0.13.4's:

    --- a/pulldown-cmark/src/firstpass.rs
    +++ b/pulldown-cmark/src/firstpass.rs
    @@ -421,12 +421,13 @@
             let mut line_start = LineStart::new(bytes);
             let tree_position = scan_containers(&self.tree, &mut line_start, self.options);
             let current_container = tree_position == self.tree.spine_len();
    -        if !line_start.scan_space(4)
    +        if (!line_start.scan_space(4)
                 && self.scan_paragraph_interrupt(
                     &bytes[line_start.bytes_scanned()..],
                     current_container,
                     tree_position,
    -            )
    +            ))
    +            || scan_blank_line(&bytes[line_start.bytes_scanned()..]).is_some()
             {
                 None
             } else {

    With it, 0.13.4's own tests still pass. All four bodies above then parse without a panic and give balanced events, both with into_offset_iter() and with plain iteration.

    Regression test (appended to tests/html.rs). On 0.13.4 it fails at the first case with right: "<ul>\n<li>"; with the hunk it passes:

    #[test]
    fn whitespace_line_after_refdef_in_list_item() {
        for (original, expected) in [
            ("- [o]:u\n    \t", "<ul>\n<li></li>\n</ul>\n"),
            ("> - [o]:u\n>     \t", "<blockquote>\n<ul>\n<li></li>\n</ul>\n</blockquote>\n"),
            ("+\t[\0]:d\n\u{b}", "<ul>\n<li></li>\n</ul>\n"),
        ] {
            let mut s = String::new();
            html::push_html(&mut s, Parser::new(original));
            assert_eq!(expected, s, "{original:?}");
            let mut s = String::new();
            html::push_html(&mut s, Parser::new(original).into_offset_iter().map(|(e, _)| e));
            assert_eq!(expected, s, "{original:?} via into_offset_iter");
        }
    }

    Could this go into a 0.13.5? One more thing that may explain why fuzzing did not find it: the targets in fuzz/fuzz_targets/ never call into_offset_iter(), and that is the only path that panics here.

  3. DanielBoigk commented on Oct 17, 2026

    @DanielBoigk

    Another independent hit, from a coverage-guided fuzz target over into_offset_iter() in an editor (a Markdown math-delimiter scanner), on 0.13.4 with Options::empty(). Two more minimal inputs in the same family. Here the link destination is an unterminated [, and the second line is a single vertical tab or form feed (an exhaustive probe of single trailing ASCII bytes found only these two):

    use pulldown_cmark::{Options, Parser};
    
    fn main() {
        for input in ["- [a]:[\n\u{b}", "- [a]:[\n\u{c}"] {
            // plain iteration is fine; the offset iterator panics at parse.rs:2199:47
            println!("plain events: {}", Parser::new(input).count());
            assert!(std::panic::catch_unwind(|| {
                Parser::new_ext(input, Options::empty()).into_offset_iter().count()
            })
            .is_err());
        }
    }

    catch_unwind doesn't help builds with panic = "abort" (our wasm client), so we skip parsing for these inputs until a fix is released. +1 for a 0.13.5 with the backport above.

  4. junkdog commented on Oct 19, 2026

    @junkdog

    4. The same panic here, and I think it is already fixed on main, just not released. A one-hunk backport to branch_0.13 fixes it, and a regression test is below.

    I agree with @richardguenther - verified with a short, targeted fuzzing session.

  5. Martin1887 commented on Oct 19, 2026

    @Martin1887
    Collaborator

    Fixed by #1021.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions