Skip to content

Fix matching Regex against an empty string#1253

Merged
psychonic merged 1 commit intoalliedmodders:masterfrom
ErikMinekus:match-regex-empty-string-exception
Apr 29, 2020
Merged

Fix matching Regex against an empty string#1253
psychonic merged 1 commit intoalliedmodders:masterfrom
ErikMinekus:match-regex-empty-string-exception

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