diff --git a/UPGRADING b/UPGRADING index 7b264b0703b..0f9762319f9 100644 --- a/UPGRADING +++ b/UPGRADING @@ -265,6 +265,12 @@ PHP 8.5 UPGRADE NOTES it is visible; if there are nested output handlers the next one will still be used. RFC: https://wiki.php.net/rfc/deprecations_php_8_4 + . Trying to produce output (e.g. with `echo`) within a user output handler + is deprecated. The deprecation warning will bypass the handler producing the + output to ensure it is visible; if there are nested output handlers the next + one will still be used. If a user output handler returns a non-string and + produces output, the warning about producing an output is emitted first. + RFC: https://wiki.php.net/rfc/deprecations_php_8_4 - Hash: . The MHASH_* constants have been deprecated. These have been overlooked diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index 88f9b706ed2..9c1759f05db 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -1584,10 +1584,22 @@ PHP_FUNCTION(mb_output_handler) if (SG(sapi_headers).send_default_content_type || free_mimetype) { const char *charset = encoding->mime_name; if (charset) { - char *p; - size_t len = spprintf(&p, 0, "Content-Type: %s; charset=%s", mimetype, charset); - if (sapi_add_header(p, len, 0) != FAILURE) { - SG(sapi_headers).send_default_content_type = 0; + /* Don't try to add a header if we are in an output handler; + * we aren't supposed to directly access the output globals + * from outside of main/output.c, so just try to get the flags + * for the currently running handler, will only succeed if + * there is a handler running. */ + int unused; + bool in_handler = php_output_handler_hook( + PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, + &unused + ) == SUCCESS; + if (!in_handler) { + char *p; + size_t len = spprintf(&p, 0, "Content-Type: %s; charset=%s", mimetype, charset); + if (sapi_add_header(p, len, 0) != FAILURE) { + SG(sapi_headers).send_default_content_type = 0; + } } } diff --git a/main/output.c b/main/output.c index 7232044ebeb..c75b09e86c1 100644 --- a/main/output.c +++ b/main/output.c @@ -934,6 +934,13 @@ static inline php_output_handler_status_t php_output_handler_op(php_output_handl return PHP_OUTPUT_HANDLER_FAILURE; } + /* php_output_lock_error() doesn't fail for PHP_OUTPUT_HANDLER_WRITE but + * anything that gets written will silently be discarded, remember that we + * tried to write so a deprecation warning can be emitted at the end. */ + if (context->op == PHP_OUTPUT_HANDLER_WRITE && OG(active) && OG(running)) { + handler->flags |= PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT; + } + bool still_have_handler = true; /* storable? */ if (php_output_handler_append(handler, &context->in) && !context->op) { @@ -962,16 +969,37 @@ static inline php_output_handler_status_t php_output_handler_op(php_output_handl handler->func.user->fci.retval = &retval; if (SUCCESS == zend_call_function(&handler->func.user->fci, &handler->func.user->fcc) && Z_TYPE(retval) != IS_UNDEF) { - if (Z_TYPE(retval) != IS_STRING) { + if (Z_TYPE(retval) != IS_STRING || handler->flags & PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT) { // Make sure that we don't get lost in the current output buffer // by disabling it handler->flags |= PHP_OUTPUT_HANDLER_DISABLED; - php_error_docref( - NULL, - E_DEPRECATED, - "Returning a non-string result from user output handler %s is deprecated", - ZSTR_VAL(handler->name) - ); + // Make sure we keep a reference to the handler name in + // case + // * The handler produced output *and* returned a non-string + // * The first deprecation message causes the handler to + // be removed + zend_string *handler_name = handler->name; + zend_string_addref(handler_name); + if (handler->flags & PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT) { + // The handler might not always produce output + handler->flags &= ~PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT; + php_error_docref( + NULL, + E_DEPRECATED, + "Producing output from user output handler %s is deprecated", + ZSTR_VAL(handler_name) + ); + } + if (Z_TYPE(retval) != IS_STRING) { + php_error_docref( + NULL, + E_DEPRECATED, + "Returning a non-string result from user output handler %s is deprecated", + ZSTR_VAL(handler_name) + ); + } + zend_string_release(handler_name); + // Check if the handler is still in the list of handlers to // determine if the PHP_OUTPUT_HANDLER_DISABLED flag can // be removed diff --git a/main/php_output.h b/main/php_output.h index 55bf62f6223..896f1e0a8fe 100644 --- a/main/php_output.h +++ b/main/php_output.h @@ -42,6 +42,7 @@ #define PHP_OUTPUT_HANDLER_STARTED 0x1000 #define PHP_OUTPUT_HANDLER_DISABLED 0x2000 #define PHP_OUTPUT_HANDLER_PROCESSED 0x4000 +#define PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT 0x8000 #define PHP_OUTPUT_HANDLER_ABILITY_FLAGS(bitmask) ((bitmask) & ~0xf00f) diff --git a/tests/output/ob_start_callback_bad_return/exception_handler.phpt b/tests/output/ob_start_callback_bad_return/exception_handler.phpt index 3a3ace427bb..eef3fccc77e 100644 --- a/tests/output/ob_start_callback_bad_return/exception_handler.phpt +++ b/tests/output/ob_start_callback_bad_return/exception_handler.phpt @@ -1,5 +1,5 @@ --TEST-- -ob_start(): Check behaviour with deprecation converted to exception +ob_start(): Check behaviour with deprecation converted to exception [bad return] --FILE-- --EXPECTF-- +Deprecated: ob_end_flush(): Producing output from user output handler return_given_string is deprecated in %s on line %d3 + +Deprecated: ob_end_flush(): Producing output from user output handler return_empty_string is deprecated in %s on line %d2 + + Log: return_zero: <<>> return_string: <<< Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated in %s on line %d 0>>> -return_null: <<>> +return_null: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_string is deprecated in %s on line %d +I stole your output.>>> return_true: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_null is deprecated in %s on line %d + Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_null is deprecated in %s on line %d >>> return_false: <<< -Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d ->>> -return_empty_string: <<< -Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated in %s on line %d +Deprecated: ob_end_flush(): Producing output from user output handler return_true is deprecated in %s on line %d Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d >>> -return_given_string: <<<>>> +return_empty_string: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_false is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Producing output from user output handler return_true is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d +>>> +return_given_string: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_empty_string is deprecated in %s on line %d2 +>>> diff --git a/tests/output/ob_start_callback_output/exception_handler.phpt b/tests/output/ob_start_callback_output/exception_handler.phpt new file mode 100644 index 00000000000..2049afc81ef --- /dev/null +++ b/tests/output/ob_start_callback_output/exception_handler.phpt @@ -0,0 +1,88 @@ +--TEST-- +ob_start(): Check behaviour with deprecation converted to exception [produce output] +--FILE-- +>>"; + echo __FUNCTION__; + return "FIRST\n"; +} + +function second_handler($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "SECOND\n"; +} + +function third_handler($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "THIRD\n"; +} + +$cases = [ + 'first_handler', + 'second_handler', + 'third_handler', +]; +foreach ($cases as $case) { + $log = []; + echo "\n\nTesting: $case\n"; + ob_start($case); + echo "Inside of $case\n"; + try { + ob_end_flush(); + } catch (\ErrorException $e) { + echo $e . "\n"; + } + echo "\nEnd of $case, log was:\n"; + echo implode("\n", $log); +} + +?> +--EXPECTF-- +Testing: first_handler +FIRST +ErrorException: ob_end_flush(): Producing output from user output handler first_handler is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, 41) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of first_handler, log was: +first_handler: <<>> + +Testing: second_handler +SECOND +ErrorException: ob_end_flush(): Producing output from user output handler second_handler is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, 41) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of second_handler, log was: +second_handler: <<>> + +Testing: third_handler +THIRD +ErrorException: ob_end_flush(): Producing output from user output handler third_handler is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, 41) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of third_handler, log was: +third_handler: <<>> diff --git a/tests/output/ob_start_callback_output/exception_handler_nested.phpt b/tests/output/ob_start_callback_output/exception_handler_nested.phpt new file mode 100644 index 00000000000..562abd63aa6 --- /dev/null +++ b/tests/output/ob_start_callback_output/exception_handler_nested.phpt @@ -0,0 +1,82 @@ +--TEST-- +ob_start(): Check behaviour with nested deprecation converted to exception [produce output] +--FILE-- +>>"; + echo __FUNCTION__; + return "FIRST\n"; +} + +function second_handler($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "SECOND\n"; +} + +function third_handler($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "THIRD\n"; +} + +ob_start('first_handler'); +ob_start('second_handler'); +ob_start('third_handler'); + +echo "In all of them\n\n"; +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended third_handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended second_handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended first_handler handler\n\n"; + +echo "All handlers are over\n\n"; +echo implode("\n", $log); + +?> +--EXPECT-- +FIRST +ob_end_flush(): Producing output from user output handler first_handler is deprecated +Ended first_handler handler + +All handlers are over + +third_handler: <<>> +second_handler: <<>> +first_handler: <<>> diff --git a/tests/output/ob_start_callback_output/functions_that_output.phpt b/tests/output/ob_start_callback_output/functions_that_output.phpt new file mode 100644 index 00000000000..353a7d9752b --- /dev/null +++ b/tests/output/ob_start_callback_output/functions_that_output.phpt @@ -0,0 +1,84 @@ +--TEST-- +ob_start(): Check behaviour with functions that trigger output (nested) +--FILE-- +>>"; + echo __FUNCTION__; + return "echo\n"; +} + +function handle_var_dump($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + var_dump(__FUNCTION__); + return "var_dump\n"; +} + +function handle_var_export($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + var_export(__FUNCTION__); + return "var_export\n"; +} + +function handle_phpcredits($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + phpcredits(); + return "phpcredits\n"; +} + +$cases = [ + 'handle_echo', + 'handle_var_dump', + 'handle_var_export', + 'handle_phpcredits', +]; +foreach ($cases as $case) { + $log = []; + echo "\n\nTesting: $case"; + ob_start($case); + echo "Inside of $case\n"; + ob_end_flush(); + echo "\nEnd of $case, log was:\n"; + echo implode("\n", $log); +} + +?> +--EXPECTF-- +Testing: handle_echo +Deprecated: ob_end_flush(): Producing output from user output handler handle_echo is deprecated in %s on line %d +echo + +End of handle_echo, log was: +handle_echo: <<>> + +Testing: handle_var_dump +Deprecated: ob_end_flush(): Producing output from user output handler handle_var_dump is deprecated in %s on line %d +var_dump + +End of handle_var_dump, log was: +handle_var_dump: <<>> + +Testing: handle_var_export +Deprecated: ob_end_flush(): Producing output from user output handler handle_var_export is deprecated in %s on line %d +var_export + +End of handle_var_export, log was: +handle_var_export: <<>> + +Testing: handle_phpcredits +Deprecated: ob_end_flush(): Producing output from user output handler handle_phpcredits is deprecated in %s on line %d +phpcredits + +End of handle_phpcredits, log was: +handle_phpcredits: <<>> diff --git a/tests/output/ob_start_callback_output/functions_that_output_nested.phpt b/tests/output/ob_start_callback_output/functions_that_output_nested.phpt new file mode 100644 index 00000000000..976ec46f880 --- /dev/null +++ b/tests/output/ob_start_callback_output/functions_that_output_nested.phpt @@ -0,0 +1,69 @@ +--TEST-- +ob_start(): Check behaviour with functions that trigger output (nested) +--FILE-- +>>"; + echo __FUNCTION__; + return "echo\n"; +} + +function handle_var_dump($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + var_dump(__FUNCTION__); + return "var_dump\n"; +} + +function handle_var_export($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + var_export(__FUNCTION__); + return "var_export\n"; +} + +function handle_phpcredits($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + phpcredits(); + return "phpcredits\n"; +} + +ob_start('handle_echo'); +ob_start('handle_var_dump'); +ob_start('handle_var_export'); +ob_start('handle_phpcredits'); + +echo "Testing..."; + +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); + +echo "\n\nLog:\n"; +echo implode("\n", $log); +?> +--EXPECTF-- +Deprecated: ob_end_flush(): Producing output from user output handler handle_echo is deprecated in %s on line %d +echo + + +Log: +handle_phpcredits: <<>> +handle_var_export: <<< +Deprecated: ob_end_flush(): Producing output from user output handler handle_phpcredits is deprecated in %s on line %d +phpcredits +>>> +handle_var_dump: <<< +Deprecated: ob_end_flush(): Producing output from user output handler handle_var_export is deprecated in %s on line %d +var_export +>>> +handle_echo: <<< +Deprecated: ob_end_flush(): Producing output from user output handler handle_var_dump is deprecated in %s on line %d +var_dump +>>> \ No newline at end of file diff --git a/tests/output/ob_start_callback_output/handler_inconsistent_echo.phpt b/tests/output/ob_start_callback_output/handler_inconsistent_echo.phpt new file mode 100644 index 00000000000..4ab0d9a1f9f --- /dev/null +++ b/tests/output/ob_start_callback_output/handler_inconsistent_echo.phpt @@ -0,0 +1,49 @@ +--TEST-- +ob_start(): Check behaviour with handler that doesn't always trigger output +--FILE-- +>>"; + if ($string === "DO ECHO\n") { + echo __FUNCTION__; + } + return $string; +} + +ob_start('handler'); +echo "DO ECHO\n"; +ob_flush(); +echo "NO ECHO\n"; +ob_flush(); +echo "DO ECHO\n"; +ob_flush(); +echo "LAST ONE\n"; +ob_end_flush(); + +echo "\n\nLog:\n"; +echo implode("\n", $log); + +?> +--EXPECTF-- +Deprecated: ob_flush(): Producing output from user output handler handler is deprecated in %s on line %d +DO ECHO +NO ECHO + +Deprecated: ob_flush(): Producing output from user output handler handler is deprecated in %s on line %d +DO ECHO +LAST ONE + + +Log: +handler: <<>> +handler: <<>> +handler: <<>> +handler: <<>> diff --git a/tests/output/ob_start_callback_output/multiple_handlers.phpt b/tests/output/ob_start_callback_output/multiple_handlers.phpt new file mode 100644 index 00000000000..b951cb22da7 --- /dev/null +++ b/tests/output/ob_start_callback_output/multiple_handlers.phpt @@ -0,0 +1,56 @@ +--TEST-- +ob_start(): Check behaviour with multiple nested handlers with output +--FILE-- +>>"; + echo __FUNCTION__; + return "FIRST\n"; +} + +function second_handler($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "SECOND\n"; +} + +function third_handler($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "THIRD\n"; +} + +ob_start('first_handler'); +ob_start('second_handler'); +ob_start('third_handler'); + +echo "Testing..."; + +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); + +echo "\n\nLog:\n"; +echo implode("\n", $log); +?> +--EXPECTF-- +Deprecated: ob_end_flush(): Producing output from user output handler first_handler is deprecated in %s on line %d +FIRST + + +Log: +third_handler: <<>> +second_handler: <<< +Deprecated: ob_end_flush(): Producing output from user output handler third_handler is deprecated in %s on line %d +THIRD +>>> +first_handler: <<< +Deprecated: ob_end_flush(): Producing output from user output handler second_handler is deprecated in %s on line %d +SECOND +>>> diff --git a/tests/output/ob_start_callback_output_and_bad_return/exception_handler.phpt b/tests/output/ob_start_callback_output_and_bad_return/exception_handler.phpt new file mode 100644 index 00000000000..2b018c792a5 --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/exception_handler.phpt @@ -0,0 +1,153 @@ +--TEST-- +ob_start(): Check behaviour with deprecation converted to exception [bad return + produce output] +--FILE-- +val; + } +} + +$log = []; + +set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) { + throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); +}); + +function return_null($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return null; +} + +function return_false($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return false; +} + +function return_true($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return true; +} + +function return_zero($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return 0; +} + +function return_non_stringable($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return new NotStringable($string); +} + +function return_stringable($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return new IsStringable($string); +} + +$cases = [ + 'return_null', + 'return_false', + 'return_true', + 'return_zero', + 'return_non_stringable', + 'return_stringable', +]; +foreach ($cases as $case) { + $log = []; + echo "\n\nTesting: $case\n"; + ob_start($case); + echo "Inside of $case\n"; + try { + ob_end_flush(); + } catch (\ErrorException $e) { + echo $e . "\n"; + } + echo "\nEnd of $case, log was:\n"; + echo implode("\n", $log); +} + +?> +--EXPECTF-- +Testing: return_null +ErrorException: ob_end_flush(): Producing output from user output handler return_null is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of return_null, log was: +return_null: <<>> + +Testing: return_false +Inside of return_false +return_falseErrorException: ob_end_flush(): Producing output from user output handler return_false is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of return_false, log was: +return_false: <<>> + +Testing: return_true +ErrorException: ob_end_flush(): Producing output from user output handler return_true is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of return_true, log was: +return_true: <<>> + +Testing: return_zero +0ErrorException: ob_end_flush(): Producing output from user output handler return_zero is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of return_zero, log was: +return_zero: <<>> + +Testing: return_non_stringable +ErrorException: ob_end_flush(): Producing output from user output handler return_non_stringable is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of return_non_stringable, log was: +return_non_stringable: <<>> + +Testing: return_stringable +ErrorException: ob_end_flush(): Producing output from user output handler return_stringable is deprecated in %s:%d +Stack trace: +#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', %s, %d) +#1 %s(%d): ob_end_flush() +#2 {main} + +End of return_stringable, log was: +return_stringable: <<>> diff --git a/tests/output/ob_start_callback_output_and_bad_return/exception_handler_nested.phpt b/tests/output/ob_start_callback_output_and_bad_return/exception_handler_nested.phpt new file mode 100644 index 00000000000..7eb060acc21 --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/exception_handler_nested.phpt @@ -0,0 +1,149 @@ +--TEST-- +ob_start(): Check behaviour with nested deprecation converted to exception [bad return + produce output] +--FILE-- +val; + } +} + +$log = []; + +set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) { + throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); +}); + +function return_null($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return null; +} + +function return_false($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return false; +} + +function return_true($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return true; +} + +function return_zero($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return 0; +} + +function return_non_stringable($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return new NotStringable($string); +} + +function return_stringable($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return new IsStringable($string); +} + +ob_start('return_null'); +ob_start('return_false'); +ob_start('return_true'); +ob_start('return_zero'); +ob_start('return_non_stringable'); +ob_start('return_stringable'); + +echo "In all of them\n\n"; +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended return_stringable handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended return_non_stringable handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended return_zero handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended return_true handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended return_false handler\n\n"; + +try { + ob_end_flush(); +} catch (\ErrorException $e) { + echo $e->getMessage() . "\n"; +} +echo "Ended return_null handler\n\n"; + +echo "All handlers are over\n\n"; +echo implode("\n", $log); + +?> +--EXPECT-- +ob_end_flush(): Producing output from user output handler return_null is deprecated +Ended return_null handler + +All handlers are over + +return_stringable: <<>> +return_non_stringable: <<>> +return_zero: <<>> +return_true: <<<0ob_end_flush(): Producing output from user output handler return_zero is deprecated +Ended return_zero handler + +>>> +return_false: <<>> +return_null: <<>> diff --git a/tests/output/ob_start_callback_output_and_bad_return/handler_false_removed.phpt b/tests/output/ob_start_callback_output_and_bad_return/handler_false_removed.phpt new file mode 100644 index 00000000000..78c736b80cd --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/handler_false_removed.phpt @@ -0,0 +1,26 @@ +--TEST-- +ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns false + produces output) +--INI-- +memory_limit=2M +--FILE-- + +--EXPECTF-- +Deprecated: main(): Producing output from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d diff --git a/tests/output/ob_start_callback_output_and_bad_return/handler_is_stringable_removed.phpt b/tests/output/ob_start_callback_output_and_bad_return/handler_is_stringable_removed.phpt new file mode 100644 index 00000000000..9da82bc147e --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/handler_is_stringable_removed.phpt @@ -0,0 +1,33 @@ +--TEST-- +ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns stringable object + produces output) +--INI-- +memory_limit=2M +--FILE-- +val; + } +} + +ob_start(function() { + // We are out of memory, now trigger a deprecation + echo "IN HANDLER\n"; + return new IsStringable(""); +}); + +$a = []; +// trigger OOM in a resize operation +while (1) { + $a[] = 1; +} + +?> +--EXPECTF-- +Deprecated: main(): Producing output from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d diff --git a/tests/output/ob_start_callback_output_and_bad_return/handler_non_stringable_removed.phpt b/tests/output/ob_start_callback_output_and_bad_return/handler_non_stringable_removed.phpt new file mode 100644 index 00000000000..476acaee9c3 --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/handler_non_stringable_removed.phpt @@ -0,0 +1,35 @@ +--TEST-- +ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns non-stringable object + produces output) +--INI-- +memory_limit=2M +--FILE-- + +--EXPECTF-- +Deprecated: main(): Producing output from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d + +Fatal error: Uncaught Error: Object of class NotStringable could not be converted to string in %s:%d +Stack trace: +#0 {main} + thrown in %s on line %d diff --git a/tests/output/ob_start_callback_output_and_bad_return/handler_true_removed.phpt b/tests/output/ob_start_callback_output_and_bad_return/handler_true_removed.phpt new file mode 100644 index 00000000000..2b0218341c9 --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/handler_true_removed.phpt @@ -0,0 +1,26 @@ +--TEST-- +ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns true + produces output) +--INI-- +memory_limit=2M +--FILE-- + +--EXPECTF-- +Deprecated: main(): Producing output from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d diff --git a/tests/output/ob_start_callback_output_and_bad_return/handler_zero_removed.phpt b/tests/output/ob_start_callback_output_and_bad_return/handler_zero_removed.phpt new file mode 100644 index 00000000000..8681a846a36 --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/handler_zero_removed.phpt @@ -0,0 +1,26 @@ +--TEST-- +ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns zero + produces output) +--INI-- +memory_limit=2M +--FILE-- + +--EXPECTF-- +Deprecated: main(): Producing output from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d + +Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d diff --git a/tests/output/ob_start_callback_output_and_bad_return/multiple_handlers.phpt b/tests/output/ob_start_callback_output_and_bad_return/multiple_handlers.phpt new file mode 100644 index 00000000000..94d5d34c038 --- /dev/null +++ b/tests/output/ob_start_callback_output_and_bad_return/multiple_handlers.phpt @@ -0,0 +1,115 @@ +--TEST-- +ob_start(): Check behaviour with multiple nested handlers with bad return values and output +--FILE-- +>>"; + echo __FUNCTION__; + return $string; +} + +function return_empty_string($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return ""; +} + +function return_false($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return false; +} + +function return_true($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return true; +} + +function return_null($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return null; +} + +function return_string($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return "I stole your output."; +} + +function return_zero($string) { + global $log; + $log[] = __FUNCTION__ . ": <<<" . $string . ">>>"; + echo __FUNCTION__; + return 0; +} + +ob_start('return_given_string'); +ob_start('return_empty_string'); +ob_start('return_false'); +ob_start('return_true'); +ob_start('return_null'); +ob_start('return_string'); +ob_start('return_zero'); + +echo "Testing..."; + +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); +ob_end_flush(); + +echo "\n\nLog:\n"; +echo implode("\n", $log); +?> +--EXPECTF-- +Deprecated: ob_end_flush(): Producing output from user output handler return_given_string is deprecated in %s on line %d0 + +Deprecated: ob_end_flush(): Producing output from user output handler return_empty_string is deprecated in %s on line %d9 + + +Log: +return_zero: <<>> +return_string: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_zero is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated in %s on line %d +0>>> +return_null: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_string is deprecated in %s on line %d5 +I stole your output.>>> +return_true: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_null is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_null is deprecated in %s on line %d +>>> +return_false: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_true is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d +>>> +return_empty_string: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_false is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_false is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Producing output from user output handler return_true is deprecated in %s on line %d + +Deprecated: ob_end_flush(): Returning a non-string result from user output handler return_true is deprecated in %s on line %d +return_false>>> +return_given_string: <<< +Deprecated: ob_end_flush(): Producing output from user output handler return_empty_string is deprecated in %s on line %d9 +>>>