Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
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).

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)

);

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