Skip to content

Commit

Permalink
Don't leak header callback if headers already sent
Browse files Browse the repository at this point in the history
We need to avoid storing it in the first place, as we don't
really have a good place to release it later. If headers haven't
been sent yet, send_headers will do this. sapi_deactive happens
too late in the shutdown sequence and will result in leak reports.
  • Loading branch information
nikic committed Sep 16, 2021
1 parent 2172142 commit 9bff963
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion main/SAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ PHP_FUNCTION(header_register_callback)
SG(fci_cache) = empty_fcall_info_cache;
}

ZVAL_COPY(&SG(callback_func), &fci.function_name);
/* Don't store callback if headers have already been sent:
* It won't get used and we won't have a chance to release it. */
if (!SG(headers_sent)) {
ZVAL_COPY(&SG(callback_func), &fci.function_name);
}

RETURN_TRUE;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/basic/header_register_callback_after_output.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Call header_register_callback() after headers sent
--FILE--
<?php
echo "Send headers.\n";
header_register_callback(function() { echo "Too late";});
?>
--EXPECT--
Send headers.

0 comments on commit 9bff963

Please sign in to comment.