Fixed race condition that occurs when initializing the executable_allocator_is_working variable in the pcre2_jit_compile function (#91)

This commit is contained in:
larinsv 2022-05-18 13:16:00 +03:00 committed by GitHub
parent 187b7ba050
commit 45af1203bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 6 deletions

View File

@ -14384,7 +14384,7 @@ pcre2_jit_compile(pcre2_code *code, uint32_t options)
pcre2_real_code *re = (pcre2_real_code *)code; pcre2_real_code *re = (pcre2_real_code *)code;
#ifdef SUPPORT_JIT #ifdef SUPPORT_JIT
executable_functions *functions; executable_functions *functions;
static int executable_allocator_is_working = 0; static int executable_allocator_is_working = -1;
#endif #endif
if (code == NULL) if (code == NULL)
@ -14447,23 +14447,21 @@ return PCRE2_ERROR_JIT_BADOPTION;
if ((re->flags & PCRE2_NOJIT) != 0) return 0; if ((re->flags & PCRE2_NOJIT) != 0) return 0;
if (executable_allocator_is_working == 0) if (executable_allocator_is_working == -1)
{ {
/* Checks whether the executable allocator is working. This check /* Checks whether the executable allocator is working. This check
might run multiple times in multi-threaded environments, but the might run multiple times in multi-threaded environments, but the
result should not be affected by it. */ result should not be affected by it. */
void *ptr = SLJIT_MALLOC_EXEC(32, NULL); void *ptr = SLJIT_MALLOC_EXEC(32, NULL);
executable_allocator_is_working = -1;
if (ptr != NULL) if (ptr != NULL)
{ {
SLJIT_FREE_EXEC(((sljit_u8*)(ptr)) + SLJIT_EXEC_OFFSET(ptr), NULL); SLJIT_FREE_EXEC(((sljit_u8*)(ptr)) + SLJIT_EXEC_OFFSET(ptr), NULL);
executable_allocator_is_working = 1; executable_allocator_is_working = 1;
} }
else executable_allocator_is_working = 0;
} }
if (executable_allocator_is_working < 0) if (!executable_allocator_is_working)
return PCRE2_ERROR_NOMEMORY; return PCRE2_ERROR_NOMEMORY;
if ((re->overall_options & PCRE2_MATCH_INVALID_UTF) != 0) if ((re->overall_options & PCRE2_MATCH_INVALID_UTF) != 0)