Skip to content

Commit 838951c

Browse files
committed
Fix #80889: amendment
`session_set_save_handler()` may be called with callables instead of an object; we need to cater to that as well. We also extract a set_user_save_handler_ini() function to avoid code duplication. Closes GH-6796.
1 parent bc7ea73 commit 838951c

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

ext/session/session.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,13 +1923,24 @@ static PHP_FUNCTION(session_module_name)
19231923
}
19241924
/* }}} */
19251925

1926+
static inline void set_user_save_handler_ini(void) {
1927+
zend_string *ini_name, *ini_val;
1928+
1929+
ini_name = zend_string_init("session.save_handler", sizeof("session.save_handler") - 1, 0);
1930+
ini_val = zend_string_init("user", sizeof("user") - 1, 0);
1931+
PS(set_handler) = 1;
1932+
zend_alter_ini_entry(ini_name, ini_val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1933+
PS(set_handler) = 0;
1934+
zend_string_release_ex(ini_val, 0);
1935+
zend_string_release_ex(ini_name, 0);
1936+
}
1937+
19261938
/* {{{ proto bool session_set_save_handler(string open, string close, string read, string write, string destroy, string gc, string create_sid)
19271939
Sets user-level functions */
19281940
static PHP_FUNCTION(session_set_save_handler)
19291941
{
19301942
zval *args = NULL;
19311943
int i, num_args, argc = ZEND_NUM_ARGS();
1932-
zend_string *ini_name, *ini_val;
19331944

19341945
if (PS(session_status) == php_session_active) {
19351946
php_error_docref(NULL, E_WARNING, "Cannot change save handler when session is active");
@@ -2032,13 +2043,7 @@ static PHP_FUNCTION(session_set_save_handler)
20322043
}
20332044

20342045
if (PS(session_status) != php_session_active && (!PS(mod) || PS(mod) != &ps_mod_user)) {
2035-
ini_name = zend_string_init("session.save_handler", sizeof("session.save_handler") - 1, 0);
2036-
ini_val = zend_string_init("user", sizeof("user") - 1, 0);
2037-
PS(set_handler) = 1;
2038-
zend_alter_ini_entry(ini_name, ini_val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
2039-
PS(set_handler) = 0;
2040-
zend_string_release_ex(ini_val, 0);
2041-
zend_string_release_ex(ini_name, 0);
2046+
set_user_save_handler_ini();
20422047
}
20432048

20442049
RETURN_TRUE;
@@ -2066,14 +2071,8 @@ static PHP_FUNCTION(session_set_save_handler)
20662071
}
20672072
}
20682073

2069-
if (PS(mod) && PS(mod) != &ps_mod_user) {
2070-
ini_name = zend_string_init("session.save_handler", sizeof("session.save_handler") - 1, 0);
2071-
ini_val = zend_string_init("user", sizeof("user") - 1, 0);
2072-
PS(set_handler) = 1;
2073-
zend_alter_ini_entry(ini_name, ini_val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
2074-
PS(set_handler) = 0;
2075-
zend_string_release_ex(ini_val, 0);
2076-
zend_string_release_ex(ini_name, 0);
2074+
if (!PS(mod) || PS(mod) != &ps_mod_user) {
2075+
set_user_save_handler_ini();
20772076
}
20782077

20792078
for (i = 0; i < argc; i++) {

ext/session/tests/bug80889a.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Bug #80889 (Cannot set save handler when save_handler is invalid)
3+
--SKIPIF--
4+
<?php include('skipif.inc'); ?>
5+
--INI--
6+
session.save_handler=whatever
7+
--FILE--
8+
<?php
9+
$initHandler = ini_get('session.save_handler');
10+
session_set_save_handler(
11+
function ($savePath, $sessionName) {
12+
return true;
13+
},
14+
function () {
15+
return true;
16+
},
17+
function ($id) {
18+
return '';
19+
},
20+
function ($id, $data) {
21+
return true;
22+
},
23+
function ($id) {
24+
return true;
25+
},
26+
function ($maxlifetime) {
27+
return true;
28+
}
29+
);
30+
$setHandler = ini_get('session.save_handler');
31+
var_dump($initHandler, $setHandler);
32+
?>
33+
--EXPECT--
34+
string(8) "whatever"
35+
string(4) "user"

0 commit comments

Comments
 (0)