Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ext/session: fix missing zval_ptr_dtor for retval in PS_GC_FUNC(user)
PS_GC_FUNC(user) did not call zval_ptr_dtor() on the return value of
the user GC callback, leaking memory when the callback returned a
reference-counted value. All other user handlers (write, destroy,
validate_sid, update_timestamp) already free retval correctly.
  • Loading branch information
jorgsowa committed Apr 15, 2026
commit 2330863f86ad53279a1381c2cb49f055cb92b7c9
1 change: 1 addition & 0 deletions ext/session/mod_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ PS_GC_FUNC(user)
/* Anything else is some kind of error */
*nrdels = -1; // Error
}
zval_ptr_dtor(&retval);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not only do this in the error case?
Besides there's a subtle additional problem in the callback code: it doesn't handle reference returns (this may be solved in ps_call_handler).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this comment. I need to still apply it, but in different PR. CC: @Girgias this hasn't been addressed yet, but I think it may be addressed in differnt PR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but that is a different bug so indeed can be done in a different PR.

It might make sense to have a way to test all callbacks if they return references as this seems to be quite a common bug.

return *nrdels;
}

Expand Down
33 changes: 33 additions & 0 deletions ext/session/tests/user_session_module/gh_gc_retval_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
session_gc(): user handler returning non-bool/non-int does not leak memory
--INI--
session.gc_probability=0
session.save_handler=files
--EXTENSIONS--
session
--FILE--
<?php
ob_start();

// Procedural API has no return type enforcement, so gc can return a string
// (reference-counted), which PS_GC_FUNC(user) previously did not free.
session_set_save_handler(
function(string $path, string $name) { return true; },
function() { return true; },
function(string $id): string|false { return ""; },
function(string $id, string $data) { return true; },
function(string $id) { return true; },
function(int $max) { return str_repeat("x", 100); }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI: this won't leak when the test runs with opcache, which nowadays is almost always. This is because of SCCP computing the string at compile time which causes the string to get interned.
To solve this, replace 100 by random_int(100, 100)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

);

session_start();
$result = session_gc();
var_dump($result);
session_write_close();

ob_end_flush();
?>
--EXPECTF--

Deprecated: session_set_save_handler(): Providing individual callbacks instead of an object implementing SessionHandlerInterface is deprecated in %s on line %d
bool(false)
Loading