From 77ac1e8592d6cc14863ffea3c03078e28eb2c3e5 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:47:45 +0100 Subject: [PATCH] Fix GH-12974: Apache crashes on shutdown when using pg_pconnect() On ZTS, the global variables are stored in dynamically allocated memory. When the module gets shut down this memory is released. After the module is shut down, only then are the persistent resources cleared. Normally this isn't an issue, but pgsql and odbc refer to the globals to modify some counters, after the globals have been freed. Fix this by guarding the modification. Closes GH-13032. --- NEWS | 5 +++++ ext/odbc/php_odbc.c | 8 +++++++- ext/pgsql/pgsql.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index dc21c25772f..3dbd6980e74 100644 --- a/NEWS +++ b/NEWS @@ -25,6 +25,9 @@ PHP NEWS . Fixed bug GH-12936 (hash() function hangs endlessly if using sha512 on strings >= 4GiB). (nielsdos) +- ODBC: + . Fix crash on Apache shutdown with persistent connections. (nielsdos) + - Opcache: . Fixed oss-fuzz #64727 (JIT undefined array key warning may overwrite DIM with NULL when DIM is the same var as result). (ilutov) @@ -45,6 +48,8 @@ PHP NEWS - PGSQL: . Fixed auto_reset_persistent handling and allow_persistent type. (David Carlier) + . Fixed bug GH-12974 (Apache crashes on shutdown when using pg_pconnect()). + (nielsdos) - PHPDBG: . Fixed bug GH-12962 (Double free of init_file in phpdbg_prompt.c). (nielsdos) diff --git a/ext/odbc/php_odbc.c b/ext/odbc/php_odbc.c index d03a0790a72..e164ca9378d 100644 --- a/ext/odbc/php_odbc.c +++ b/ext/odbc/php_odbc.c @@ -168,7 +168,13 @@ static void _close_odbc_conn(zend_resource *rsrc) SQLFreeEnv(conn->henv); } efree(conn); - ODBCG(num_links)--; + /* See https://github.com/php/php-src/issues/12974 why we need to check the if */ +#ifdef ZTS + if (odbc_module_entry.module_started) +#endif + { + ODBCG(num_links)--; + } } /* }}} */ diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index a437c1620de..8b9da44a5b1 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -314,8 +314,14 @@ static void _close_pgsql_plink(zend_resource *rsrc) PQclear(res); } PQfinish(link); - PGG(num_persistent)--; - PGG(num_links)--; + /* See https://github.com/php/php-src/issues/12974 why we need to check the if */ +#ifdef ZTS + if (pgsql_module_entry.module_started) +#endif + { + PGG(num_persistent)--; + PGG(num_links)--; + } rsrc->ptr = NULL; }