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

Fix matching Regex against an empty string #1253

Merged

Conversation

ErikMinekus
Copy link
Contributor

Matching a Regex against an empty string throws an exception:

Regex regex = new Regex(".*");

int matches = regex.Match("");

Which is caused by this check:

	size_t len = strlen(str);
	if (offset >= len)
	{
		return pCtx->ThrowNativeError("Offset greater or equal than string length\n");
	}

Because offset and len are both 0.

However, this check is redundant, because pcre_exec() already returns PCRE_ERROR_BADOFFSET if the offset is negative or greater than the passed length, and RegexError already supports this.

Removed the offset check from MatchRegex, as this
is already handled by pcre_exec.
@psychonic psychonic merged commit adcc0ef into alliedmodders:master Apr 29, 2020
fengjixuchui added a commit to fengjixuchui/sourcemod that referenced this pull request Apr 29, 2020
Fix matching Regex against an empty string (alliedmodders#1253)
@ErikMinekus ErikMinekus deleted the match-regex-empty-string-exception branch April 29, 2020 06:56
asherkin pushed a commit that referenced this pull request Apr 30, 2020
Removed the offset check from MatchRegex, as this
is already handled by pcre_exec.
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