From 7a9f0cc3d05f860268e3fd28fff17d12c5671f97 Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Wed, 27 May 2020 06:01:22 +0200 Subject: Simplify `_crypt_extended_init_r`, and fix redundant initialization on Win32/Solaris Looking at the history of this function, the original implementation had a bug where it would return from the middle of the function without unlocking the mutex first. The author attempted to fix this by incrementing the `initialized` flag atomically, which is not necessary, since the section which modifies the flag is protected by a mutex. Coincidentally, at the same time that all this unnecessary 'atomic' machinery was introduced, the code was also changed so that it didn't return without unlocking the mutex. So it looks like the bug was fixed by accident. It's not necessary to declare the flag as `volatile` either, since it is protected by a mutex. Further, the 'fixed' implementation was also wrong in another respect: on Windows and Solaris, the `initialized` flag was not even declared as `static`!! So the initialization of the static tables for S-boxes, P-boxes, etc. was repeated on each call to `php_crypt`, completely defeating the purpose of this function. --- ext/standard/php_crypt_r.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) (limited to 'ext/standard/php_crypt_r.c') diff --git a/ext/standard/php_crypt_r.c b/ext/standard/php_crypt_r.c index 96e5905e5a..432657cf47 100644 --- a/ext/standard/php_crypt_r.c +++ b/ext/standard/php_crypt_r.c @@ -39,11 +39,6 @@ # include #endif -#ifdef HAVE_ATOMIC_H /* Solaris 10 defines atomic API within */ -# include -#else -# include -#endif #include "php_crypt_r.h" #include "crypt_freesec.h" @@ -76,29 +71,17 @@ void php_shutdown_crypt_r() void _crypt_extended_init_r(void) { -#ifdef PHP_WIN32 - LONG volatile initialized = 0; -#elif defined(HAVE_ATOMIC_H) /* Solaris 10 defines atomic API within */ - volatile unsigned int initialized = 0; -#else - static volatile sig_atomic_t initialized = 0; -#endif + static int initialized = 0; #ifdef ZTS tsrm_mutex_lock(php_crypt_extended_init_lock); #endif if (!initialized) { -#ifdef PHP_WIN32 - InterlockedIncrement(&initialized); -#elif defined(HAVE_SYNC_FETCH_AND_ADD) - __sync_fetch_and_add(&initialized, 1); -#elif defined(HAVE_ATOMIC_H) /* Solaris 10 defines atomic API within */ - membar_producer(); - atomic_add_int(&initialized, 1); -#endif + initialized = 1; _crypt_extended_init(); } + #ifdef ZTS tsrm_mutex_unlock(php_crypt_extended_init_lock); #endif -- cgit v1.2.1