Skip to content
Closed
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).

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", 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