Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Backtrack Limit Handling in Regex Matching #134

Open
paplorinc opened this issue Feb 12, 2024 · 0 comments
Open

Inconsistent Backtrack Limit Handling in Regex Matching #134

paplorinc opened this issue Feb 12, 2024 · 0 comments

Comments

@paplorinc
Copy link

The following reproducer:

    #[test]
    fn test_effect_of_backtrack_limit() {
        const INPUT_LIMIT: usize = 10;
        const INPUT_SIZE: usize = 100;
        let regex = RegexBuilder::new(r"(a|b|ab)*(?=c)")
            .backtrack_limit(INPUT_LIMIT)
            .build()
            .expect("Failed to build regex")
            .clone();

        let input = "ab".repeat(INPUT_SIZE) + "c";
        assert!(regex.is_match(&input).is_err(), "Should throw");
    }

fails correctly at

        if backtrack_count > options.backtrack_limit {
            return Err(Error::RuntimeError(RuntimeError::BacktrackLimitExceeded));
        }

but if we increase the SIZE and make the limit a lot bigger, it should not throw, but it panics (is it a simple stack overflow now?):

    #[test]
    fn test_effect_of_backtrack_limit() {
        const INPUT_LIMIT: usize = 10_000_000;
        const INPUT_SIZE: usize = 1_000_000;
        let regex = RegexBuilder::new(r"(a|b|ab)*(?=c)")
            .backtrack_limit(INPUT_LIMIT)
            .build()
            .expect("Failed to build regex")
            .clone();

        let input = "ab".repeat(INPUT_SIZE) + "c";
        assert!(regex.is_match(&input).is_ok(), "Should not throw");
    }

Is there a way to increase the backtracking limit to 10 million in e.g. openai/tiktoken#245?

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

No branches or pull requests

1 participant