Skip to content

nom_language::error::convert_error panics with too long of an input #1871

@thomasqueirozb

Description

@thomasqueirozb
use nom_language::error::{VerboseError, VerboseErrorKind};

fn main() {
    let input = "x".repeat(u16::MAX as usize);
    let input_str = input.as_str();

    let error_location = &input_str[u16::MAX as usize..];
    // // Doesn't panic
    // let error_location = &input_str[u16::MAX as usize - 1..];

    // Construct the error as nom would
    let error = VerboseError {
        errors: vec![(
            error_location,
            VerboseErrorKind::Nom(nom::error::ErrorKind::Tag),
        )],
    };

    // This will panic!
    // nom-language calculates: column_number = line.offset(substring) + 1 = 199001
    // Then tries: `write!(&mut result, "{caret:>column$}", caret='^', column=199001)` in nom-language/src/error.rs
    // Formatter cannot allocate 199001 spaces and panics
    nom_language::error::convert_error(input_str, error);
}

Running outputs:

thread 'main' (17897756) panicked at ~/.local/share/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/nom-language-0.1.0/src/error.rs:210:37:
Formatting argument out of range
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I think the solution here is to break/change implementation of convert_error to truncate the output to only the first and last X (maybe 1k?) characters. I don't think anyone needs to see >10k chars printed to the console or even saved to a file under any circumstance


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