regex: add/document missing offset param (#1175)

* 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>
This commit is contained in:
Ҝờţأķ 2020-02-27 04:52:04 +05:00 committed by GitHub
parent 2b6833f65d
commit ded3867605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 37 deletions

View File

@ -39,10 +39,10 @@ RegEx::RegEx()
{
mErrorOffset = 0;
mErrorCode = 0;
mError = NULL;
re = NULL;
mError = nullptr;
re = nullptr;
mFree = true;
subject = NULL;
subject = nullptr;
mMatchCount = 0;
}
@ -50,14 +50,14 @@ 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;
}
@ -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;
}
@ -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)
{
@ -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)
@ -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;
}

View File

@ -51,7 +51,7 @@ public:
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);

View File

@ -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. */

View File

@ -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.