Skip to content

Commit

Permalink
regex: add/document missing offset param (#1175)
Browse files Browse the repository at this point in the history
* Regex Little Changes

* Prevented to triple and double call `strlen`.
* More informative message on `if (offset >= len)`.
* Add missing parametr in navite `MatchRegex`.

* Regex Little Changes v2

* Using `strdup` instead `strcpy`.
* Replaced NULL to nullptr.
* Removed note about MatchOffset.

Co-Authored-By: Headline <headline@users.noreply.github.com>

* Removed padding.

Co-Authored-By: Headline <headline@users.noreply.github.com>

* Removed more padding.

Co-Authored-By: Headline <headline@users.noreply.github.com>

Co-authored-by: Headline <michaelwflaherty@me.com>
  • Loading branch information
TheByKotik and Headline committed Feb 26, 2020
1 parent 2b6833f commit ded3867
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
40 changes: 18 additions & 22 deletions extensions/regex/CRegEx.cpp
Expand Up @@ -39,25 +39,25 @@ RegEx::RegEx()
{
mErrorOffset = 0;
mErrorCode = 0;
mError = NULL;
re = NULL;
mError = nullptr;
re = nullptr;
mFree = true;
subject = NULL;
subject = nullptr;
mMatchCount = 0;
}

void RegEx::Clear ()
{
mErrorOffset = 0;
mErrorCode = 0;
mError = NULL;
mError = nullptr;
if (re)
pcre_free(re);
re = NULL;
re = nullptr;
mFree = true;
if (subject)
delete [] subject;
subject = NULL;
subject = nullptr;
mMatchCount = 0;
}

Expand All @@ -82,9 +82,9 @@ int RegEx::Compile(const char *pattern, int iFlags)
if (!mFree)
Clear();

re = pcre_compile2(pattern, iFlags, &mErrorCode, &mError, &mErrorOffset, NULL);
re = pcre_compile2(pattern, iFlags, &mErrorCode, &mError, &mErrorOffset, nullptr);

if (re == NULL)
if (re == nullptr)
{
return 0;
}
Expand All @@ -94,22 +94,19 @@ int RegEx::Compile(const char *pattern, int iFlags)
return 1;
}

int RegEx::Match(const char *str, unsigned int offset)
int RegEx::Match(const char *const str, const size_t offset)
{
int rc = 0;

if (mFree || re == NULL)
if (mFree || re == nullptr)
return -1;

this->ClearMatch();

//save str
subject = new char[strlen(str)+1];
strcpy(subject, str);
subject = strdup(str);

unsigned int len = strlen(subject);

rc = pcre_exec(re, NULL, subject, len, offset, 0, mMatches[0].mVector, MAX_CAPTURES);
rc = pcre_exec(re, nullptr, subject, strlen(subject), offset, 0, mMatches[0].mVector, MAX_CAPTURES);

if (rc < 0)
{
Expand All @@ -132,17 +129,16 @@ int RegEx::MatchAll(const char *str)
{
int rc = 0;

if (mFree || re == NULL)
if (mFree || re == nullptr)
return -1;

this->ClearMatch();

//save str
subject = new char[strlen(str) + 1];
strcpy(subject, str);
subject = strdup(str);
size_t len = strlen(subject);

unsigned int offset = 0;
unsigned int len = strlen(subject);
size_t offset = 0;
unsigned int matches = 0;

while (matches < MAX_MATCHES && offset < len && (rc = pcre_exec(re, 0, subject, len, offset, 0, mMatches[matches].mVector, MAX_CAPTURES)) >= 0)
Expand Down Expand Up @@ -175,10 +171,10 @@ void RegEx::ClearMatch()
// Clears match results
mErrorOffset = 0;
mErrorCode = 0;
mError = NULL;
mError = nullptr;
if (subject)
delete [] subject;
subject = NULL;
subject = nullptr;
mMatchCount = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/regex/CRegEx.h
Expand Up @@ -51,7 +51,7 @@ class RegEx
void Clear();

int Compile(const char *pattern, int iFlags);
int Match(const char *str, unsigned int offset);
int Match(const char *const str, const size_t offset);
int MatchAll(const char *str);
void ClearMatch();
bool GetSubstring(int s, char buffer[], int max, int match);
Expand Down
24 changes: 11 additions & 13 deletions extensions/regex/extension.cpp
Expand Up @@ -114,35 +114,33 @@ static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();

unsigned int offset = 0;

if (params[0] >= 4)
{
offset = (unsigned int)params[4];
}

RegEx *x;

if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
{
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
}

if (!x)
{
pCtx->ThrowNativeError("Regex data not found\n");

return 0;
}

size_t offset = 0;
if (params[0] >= 4)
{
offset = static_cast<size_t>(params[4]);
}

char *str;
pCtx->LocalToString(params[2], &str);

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

int e = x->Match(str, offset);

if (e == -1)
{
/* there was a match error. move on. */
Expand Down
3 changes: 2 additions & 1 deletion plugins/include/regex.inc
Expand Up @@ -199,12 +199,13 @@ native Regex CompileRegex(const char[] pattern, int flags = 0, char[] error="",
* @param regex Regex Handle from CompileRegex()
* @param str The string to check.
* @param ret Error code, if applicable.
* @param offset Offset in the string to start searching from.
* @return Number of captures found or -1 on failure.
*
* @note Use the regex handle passed to this function to extract
* matches with GetRegexSubString().
*/
native int MatchRegex(Handle regex, const char[] str, RegexError &ret = REGEX_ERROR_NONE);
native int MatchRegex(Handle regex, const char[] str, RegexError &ret = REGEX_ERROR_NONE, int offset = 0);

/**
* Returns a matched substring from a regex handle.
Expand Down

0 comments on commit ded3867

Please sign in to comment.