var_dump() and debug_zval_dump() print the full anonymous class name with a raw NUL byte
Description
Since 5ed6625e4c0 (#23143, %pS migration of ext/standard), var_dump() and debug_zval_dump() print the internal name of an anonymous class, including the NUL byte and the defining file path. PHP 8.5 printed class@anonymous.
The following code:
<?php
$o = new class { public $a = 1; private $p; };
var_dump($o);
print_r($o);
Resulted in this output (\0 is a raw NUL byte):
object(class@anonymous\0/in/test.php:2$0)#1 (2) {
["a"]=>
int(1)
["p":"class@anonymous":private]=>
NULL
}
class@anonymous Object
(
[a] => 1
[p:class@anonymous:private] =>
)
But I expected this output instead (as in PHP 8.5):
object(class@anonymous)#1 (2) {
["a"]=>
int(1)
["p":"class@anonymous":private]=>
NULL
}
class@anonymous Object
(
[a] => 1
[p:class@anonymous:private] =>
)
The commit changed php_printf("...object(%s)...", ZSTR_VAL(class_name)) to %pS, which prints the whole zend_string. The private property label (zend_unmangle_property_name() + %s) and print_r() still stop at the NUL, so the same dump now shows two different names for one class. The change is in 8.6.0RC6/RC7.
The full name is a deliberate consequence of treating NUL truncation as a bug (#23142), but for dumpers it was not discussed: #23143 had no review, the change is not in NEWS or UPGRADING, and labels and print_r() were left truncated. For 8.6 I suggest restoring %s for the two object headers in ext/standard/var.c (8.5 output); printing the full name consistently in dumps could be discussed for master. @DanielEScherzer, what do you think?
PHP Version
PHP 8.6.0RC7 (PHP-8.6 and master also affected)
Operating System
No response
I created suggested fix https://github.com/php/php-src/pull/25419 but feel free to apply your changes. I wanted only to point up regressions in PHP 8.6.
I'm in favour of just reverting for 8.6, as this change wasn't really discussed and the fact it was hidden might break quite a few assumptions?
Daniel said he'll respond to this later today and would like that any revert be postponed until he has time to reply.
Sure, it's not up to me to make any decisions in this matter. That's not really my area of expertise.
"later today" loosely defined
I think any case where a NUL byte was implicitly causing truncation without being intended is a bug, and print_r() should be changed to show the full name, rather than reverting the others. Would that work for you @jorgsowa?
Personally I think it should be reverted, because of unforeseen side effects. I suspect many of those may be used in test assertions. And it may not be trivial to have simple workaround for such tests during RC phase.
How often do userland tests use .phpt tests rather than PHPUnit assertions that would start to fail? For any %pS change where the output changes, I would argue that it is a bug fix, and only the ones where %pS doesn't change things and just improves performance are refactorings that warrant wider discussion and/or targeting master
PHPUnit assertions are not only ones that are used in production test suites. This should be the question to release managers if they approve such change. Honestly, I don't want to defend it, neither be against it. According to the policy such changes shouldn't be included in RC phase, and that's why I reported this case.
Okay, lets ask @php/release-managers-86 for thoughts
IMHO it's too late for 8.6. The same commit changed the __sleep() warnings of serialize(), eg "\0*\0b" is returned from __sleep() multiple times instead of "" is returned from __sleep() multiple times. Code that mirrors these warnings has to follow during the RC phase, see symfony/polyfill#728 and symfony/php-ext-deepclone#67.
I suppose it is definitely too late for 8.6.0. Is it something we can fix in 8.6.1?
Unless there's a specific reason to hurry, this has been as such since years so it can wait 8.7 I suppose?
@nicolas-grekas I'm a bit confused, I thought the breaking change was introduced in PHP 8.6.0RC5.
By "this" I meant the truncation at the NUL byte, which has been the behavior for years. The full names are what's new, since 8.6.0RC5.
To clarify, I'd revert for 8.6.0, and reapply in 8.7 rather than in 8.6.1. Code that mirrors these messages can follow such a change more easily in a minor version than in a patch one.
@nicolas-grekas Thanks for the clarification and your input.
Just thinking out loud here. If this is considered a bug and the behaviour is reverted in 8.6.1, code that implements a workaround could still target a specific range of PHP_VERSION_ID. Whoever doesn't want to deal with the workaround could then block 8.6.0 specifically in their composer.json.
@nicolas-grekas Personally, I wouldn't delay PHP 8.6.0 by a whole month for this. Only security issues are allowed to go in after the last RC...
Definitely!
This went out without a revert. For var_dump(), get_debug_type should be used to compute the dumped class name.