Skip to content

grammar: handle misplaced special regex chars [*+?] #13391

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rick-github
Copy link
Contributor

Don't try to access the previous element of a regex seq if the vector is empty. Prevents segment violation failures for regexes like ^?[0-9]$ and ^(?:a|b)$.

Fixes: #13390

@CISC
Copy link
Collaborator

CISC commented May 9, 2025

Don't try to access the previous element of a regex seq if the vector is empty. Prevents segment violation failures for regexes like ^?[0-9]$ and ^(?:a|b)$.

The latter should have been handled (non-capturing groups are unsupported) here, but seems to just pass it on as if it was a normal pattern:

} else if (c == '(') {
i++;
if (i < length) {
if (sub_pattern[i] == '?') {
_warnings.push_back("Unsupported pattern syntax");
}
}
seq.emplace_back("(" + to_rule(transform()) + ")", false);

@CISC
Copy link
Collaborator

CISC commented May 9, 2025

@rick-github I think both examples should report errors instead of quietly eating the expression characters.

The first example is a clearly invalid expression and should be reported as such, and the second is unsupported, preferably it should include the whole unsupported group in its error message.

@rick-github
Copy link
Contributor Author

This PR is just for removing a DoS vector for projects using the llama.cpp codebase. Improving the parser is out of scope.

@CISC
Copy link
Collaborator

CISC commented May 9, 2025

I'm not talking about improving the parser, just reporting the error instead of quietly mangling the expression.

@CISC
Copy link
Collaborator

CISC commented May 9, 2025

Just adding an error to the _errors stack and returning an empty pair might do the trick?

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.

Misc. bug: invalid regex grammar causes segment violation
3 participants