Merge pull request #27512 from taosdata/fix/TD-31678/regexCrashOnWindows

fix: regex crashes on windows
This commit is contained in:
Pan Wei 2024-08-29 09:20:23 +08:00 committed by GitHub
commit f6bb005fbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 1 deletions

View File

@ -1253,6 +1253,9 @@ void regexCacheFree(void *ppUsingRegex) {
}
int32_t InitRegexCache() {
#ifdef WINDOWS
return 0;
#endif
sRegexCache.regexHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
if (sRegexCache.regexHash == NULL) {
uError("failed to create RegexCache");
@ -1277,6 +1280,9 @@ int32_t InitRegexCache() {
}
void DestroyRegexCache(){
#ifdef WINDOWS
return;
#endif
int32_t code = 0;
uInfo("[regex cache] destory regex cache");
(void)taosTmrStopA(&sRegexCache.timer);
@ -1359,6 +1365,46 @@ void releaseRegComp(UsingRegex **regex){
static threadlocal UsingRegex ** ppUsingRegex;
static threadlocal regex_t * pRegex;
static threadlocal char *pOldPattern = NULL;
#ifdef WINDOWS
static threadlocal regex_t gRegex;
void DestoryThreadLocalRegComp() {
if (NULL != pOldPattern) {
regfree(&gRegex);
taosMemoryFree(pOldPattern);
pOldPattern = NULL;
}
}
int32_t threadGetRegComp(regex_t **regex, const char *pPattern) {
if (NULL != pOldPattern) {
if (strcmp(pOldPattern, pPattern) == 0) {
*regex = &gRegex;
return 0;
} else {
DestoryThreadLocalRegComp();
}
}
pOldPattern = taosStrdup(pPattern);
if (NULL == pOldPattern) {
uError("Failed to Malloc when compile regex pattern %s.", pPattern);
return terrno;
}
int32_t cflags = REG_EXTENDED;
int32_t ret = regcomp(&gRegex, pPattern, cflags);
if (ret != 0) {
char msgbuf[256] = {0};
(void)regerror(ret, &gRegex, msgbuf, tListLen(msgbuf));
uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
taosMemoryFree(pOldPattern);
pOldPattern = NULL;
return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
}
*regex = &gRegex;
return 0;
}
#else
void DestoryThreadLocalRegComp() {
if (NULL != pOldPattern) {
releaseRegComp(ppUsingRegex);
@ -1390,10 +1436,11 @@ int32_t threadGetRegComp(regex_t **regex, const char *pPattern) {
return terrno;
}
ppUsingRegex = ppRegex;
pRegex = &((*ppUsingRegex)->pRegex);
pRegex = &((*ppUsingRegex)->pRegex);
*regex = &(*ppRegex)->pRegex;
return 0;
}
#endif
static int32_t doExecRegexMatch(const char *pString, const char *pPattern) {
int32_t ret = 0;

View File

@ -66,6 +66,7 @@ TEST(testCase, regexCacheTest1) {
char s1[] = "abc";
auto start = std::chrono::high_resolution_clock::now();
#ifndef WINDOWS
uint64_t t0 = taosGetTimestampUs();
for (int i = 0; i < times; i++) {
HashRegexPtr* ret = NULL;
@ -77,6 +78,7 @@ TEST(testCase, regexCacheTest1) {
uint64_t t1 = taosGetTimestampUs();
printf("%s regex(current) %d times:%" PRIu64 " us.\n", s1, times, t1 - t0);
#endif
uint64_t t2 = taosGetTimestampUs();
for(int i = 0; i < times; i++) {
@ -101,6 +103,7 @@ TEST(testCase, regexCacheTest2) {
char s1[] = "abc%*";
auto start = std::chrono::high_resolution_clock::now();
#ifndef WINDOWS
uint64_t t0 = taosGetTimestampUs();
for (int i = 0; i < times; i++) {
HashRegexPtr* ret = NULL;
@ -112,6 +115,7 @@ TEST(testCase, regexCacheTest2) {
uint64_t t1 = taosGetTimestampUs();
printf("%s regex(current) %d times:%" PRIu64 " us.\n", s1, times, t1 - t0);
#endif
uint64_t t2 = taosGetTimestampUs();
for(int i = 0; i < times; i++) {
@ -137,6 +141,7 @@ TEST(testCase, regexCacheTest3) {
char s2[] = "abc";
auto start = std::chrono::high_resolution_clock::now();
#ifndef WINDOWS
uint64_t t0 = taosGetTimestampUs();
for (int i = 0; i < times; i++) {
HashRegexPtr* ret = NULL;
@ -148,6 +153,7 @@ TEST(testCase, regexCacheTest3) {
uint64_t t1 = taosGetTimestampUs();
printf("'%s' and '%s' take place by turn regex(current) %d times:%" PRIu64 " us.\n", s1, s2, times, t1 - t0);
#endif
uint64_t t2 = taosGetTimestampUs();
for(int i = 0; i < times; i++) {
@ -176,6 +182,7 @@ TEST(testCase, regexCacheTest4) {
char s2[] = "abc";
auto start = std::chrono::high_resolution_clock::now();
#ifndef WINDOWS
uint64_t t0 = taosGetTimestampUs();
for (int i = 0; i < times; i++) {
for (int j = 0; j < count; ++j) {
@ -196,6 +203,7 @@ TEST(testCase, regexCacheTest4) {
uint64_t t1 = taosGetTimestampUs();
printf("'%s' and '%s' take place by turn(per %d count) regex(current) %d times:%" PRIu64 " us.\n", s1, s2, count, times, t1 - t0);
#endif
uint64_t t2 = taosGetTimestampUs();
for (int i = 0; i < times; i++) {