Skip to content

Commit d144158

Browse files
committed
ext/session/mod_mm: initialize mm instance in open handler rather than MINIT
This remove access to the globals that could very much change during the execution of the script and prior to opening the session handler
1 parent c995a6c commit d144158

1 file changed

Lines changed: 36 additions & 34 deletions

File tree

ext/session/mod_mm.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -264,28 +264,50 @@ static void ps_mm_destroy(ps_mm *data)
264264

265265
PHP_MINIT_FUNCTION(ps_mm)
266266
{
267-
size_t save_path_len = ZSTR_LEN(PS(save_path));
268-
size_t mod_name_len = strlen(sapi_module.name);
269-
size_t euid_len;
270-
char *ps_mm_path, euid[30];
271-
zend_result ret;
267+
ps_mm_instance = NULL;
268+
php_session_register_module(&ps_mod_mm);
269+
return SUCCESS;
270+
}
272271

273-
ps_mm_instance = calloc(sizeof(*ps_mm_instance), 1);
274-
if (!ps_mm_instance) {
272+
PHP_MSHUTDOWN_FUNCTION(ps_mm)
273+
{
274+
if (ps_mm_instance) {
275+
ps_mm_destroy(ps_mm_instance);
276+
return SUCCESS;
277+
}
278+
return FAILURE;
279+
}
280+
281+
PS_OPEN_FUNC(mm)
282+
{
283+
if (ps_mm_instance) {
284+
ps_mm_debug(("open: ps_mm_instance=%p\n", ps_mm_instance));
285+
PS_SET_MOD_DATA(ps_mm_instance);
286+
return SUCCESS;
287+
}
288+
289+
char euid[30];
290+
size_t euid_len = slprintf(euid, sizeof(euid), "%d", geteuid());
291+
if (!euid_len) {
275292
return FAILURE;
276293
}
277294

278-
if (!(euid_len = slprintf(euid, sizeof(euid), "%d", geteuid()))) {
279-
free(ps_mm_instance);
280-
ps_mm_instance = NULL;
295+
ps_mm_instance = calloc(1, sizeof(*ps_mm_instance));
296+
if (!ps_mm_instance) {
281297
return FAILURE;
282298
}
299+
ps_mm_debug(("open: ps_mm_instance=%p\n", ps_mm_instance));
300+
301+
const char *save_path_cstr = ZSTR_VAL(save_path);
302+
size_t save_path_len = ZSTR_LEN(save_path);
303+
size_t mod_name_len = strlen(sapi_module.name);
304+
283305

284306
/* Directory + '/' + File + Module Name + Effective UID + \0 */
285-
ps_mm_path = emalloc(save_path_len + 1 + (sizeof(PS_MM_FILE) - 1) + mod_name_len + euid_len + 1);
307+
char *ps_mm_path = emalloc(save_path_len + 1 + (sizeof(PS_MM_FILE) - 1) + mod_name_len + euid_len + 1);
286308

287-
memcpy(ps_mm_path, ZSTR_VAL(PS(save_path)), save_path_len);
288-
if (save_path_len && ZSTR_VAL(PS(save_path))[save_path_len - 1] != DEFAULT_SLASH) {
309+
memcpy(ps_mm_path, save_path_cstr, save_path_len);
310+
if (save_path_len && save_path_cstr[save_path_len - 1] != DEFAULT_SLASH) {
289311
ps_mm_path[save_path_len] = DEFAULT_SLASH;
290312
save_path_len++;
291313
}
@@ -296,7 +318,7 @@ PHP_MINIT_FUNCTION(ps_mm)
296318
memcpy(ps_mm_path + save_path_len, euid, euid_len);
297319
ps_mm_path[save_path_len + euid_len] = '\0';
298320

299-
ret = ps_mm_initialize(ps_mm_instance, ps_mm_path);
321+
zend_result ret = ps_mm_initialize(ps_mm_instance, ps_mm_path);
300322

301323
efree(ps_mm_path);
302324

@@ -306,26 +328,6 @@ PHP_MINIT_FUNCTION(ps_mm)
306328
return FAILURE;
307329
}
308330

309-
php_session_register_module(&ps_mod_mm);
310-
return SUCCESS;
311-
}
312-
313-
PHP_MSHUTDOWN_FUNCTION(ps_mm)
314-
{
315-
if (ps_mm_instance) {
316-
ps_mm_destroy(ps_mm_instance);
317-
return SUCCESS;
318-
}
319-
return FAILURE;
320-
}
321-
322-
PS_OPEN_FUNC(mm)
323-
{
324-
ps_mm_debug(("open: ps_mm_instance=%p\n", ps_mm_instance));
325-
326-
if (!ps_mm_instance) {
327-
return FAILURE;
328-
}
329331
PS_SET_MOD_DATA(ps_mm_instance);
330332

331333
return SUCCESS;

0 commit comments

Comments
 (0)