diff --git a/extensions/regex/CRegEx.cpp b/extensions/regex/CRegEx.cpp index 3e3f6a0afa..3b16f20a59 100644 --- a/extensions/regex/CRegEx.cpp +++ b/extensions/regex/CRegEx.cpp @@ -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; } diff --git a/extensions/regex/CRegEx.h b/extensions/regex/CRegEx.h index 66ed111756..8a0885759d 100644 --- a/extensions/regex/CRegEx.h +++ b/extensions/regex/CRegEx.h @@ -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); diff --git a/extensions/regex/extension.cpp b/extensions/regex/extension.cpp index c903d96a76..54bda12a1f 100644 --- a/extensions/regex/extension.cpp +++ b/extensions/regex/extension.cpp @@ -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(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. */ diff --git a/plugins/include/regex.inc b/plugins/include/regex.inc index c291f685d9..a62bbcbcd2 100644 --- a/plugins/include/regex.inc +++ b/plugins/include/regex.inc @@ -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.