Skip to content

Make max parsing depth configurable. - #1223

Merged
LaurenzV merged 2 commits into
linebender:mainfrom
jdm:option-max-depth
Nov 5, 2026
Merged

LaurenzV merged 2 commits into
linebender:mainfrom
jdm:option-max-depth

Conversation

@jdm

@jdm jdm commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Servo hits stack overflows with extreme nesting at depths lower than 1024. With the limit lowered, we're able to handle those cases without taking down the browser.

@LaurenzV LaurenzV left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM once CI is fixed, but in addition:

We probably should pass the same limit here:

pub fn from_data_nested(data: &[u8], opt: &Options) -> Result<Self, Error> {

Would also be good to add a test in usvg/tests/parser:

fn parse_with_max_depth(svg: &str, max_depth: u32) -> Result<usvg::Tree, usvg::Error> {
    let opt = usvg::Options {
        max_depth,
        ..usvg::Options::default()
    };
    usvg::Tree::from_str(svg, &opt)
}

#[test]
fn max_depth_limits_nesting() {
    let svg = "<svg xmlns='http://www.w3.org/2000/svg'>\
        <g><g><rect width='1' height='1'/></g></g>\
    </svg>";

    assert!(parse_with_max_depth(svg, 3).is_ok());
    assert!(matches!(
        parse_with_max_depth(svg, 2),
        Err(usvg::Error::NodesLimitReached)
    ));
}

#[test]
fn max_depth_counts_use_instances() {
    let without_use = "<svg xmlns='http://www.w3.org/2000/svg'>\
        <defs><rect id='r' width='1' height='1'/></defs>\
    </svg>";
    let with_use = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>\
        <defs><rect id='r' width='1' height='1'/></defs>\
        <use xlink:href='#r'/>\
    </svg>";

    assert!(parse_with_max_depth(without_use, 2).is_ok());
    assert!(matches!(
        parse_with_max_depth(with_use, 2),
        Err(usvg::Error::NodesLimitReached)
    ));
}

#[test]
fn max_depth_applies_to_nested_svg_images() {
    let svg = "<svg xmlns='http://www.w3.org/2000/svg'>\
        <image width='1' height='1' href=\"data:image/svg+xml,\
            %3Csvg xmlns='http://www.w3.org/2000/svg' width='1' height='1'%3E\
            %3Cg%3E%3Crect width='1' height='1'/%3E%3C/g%3E%3C/svg%3E\"/>\
    </svg>";

    assert!(parse_with_max_depth(svg, 2).unwrap().root().has_children());
    assert!(!parse_with_max_depth(svg, 1).unwrap().root().has_children());
}

@LaurenzV

LaurenzV commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

I'll probably cut a new release later today or tomorrow, so if you need this and don't have the time to apply the fixes, let me know and I can do it quickly. 😄

@jdm

jdm commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for writing those tests! I fixed the compile errors.

@jdm
jdm force-pushed the option-max-depth branch from c8210eb to 110f415 Compare November 4, 2026 18:14
@nicoburns

Copy link
Copy Markdown
Collaborator

cargo fmt is failing.

Signed-off-by: Josh Matthews <[email protected]>
@jdm
jdm force-pushed the option-max-depth branch from 110f415 to 98bcf76 Compare November 4, 2026 21:48
@LaurenzV
LaurenzV merged commit 847abd6 into linebender:main Nov 5, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants