Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anonymous class reference in trigger_error / thrown Exception #13097

Closed
henzeb opened this issue Jan 9, 2024 · 8 comments
Closed

Anonymous class reference in trigger_error / thrown Exception #13097

henzeb opened this issue Jan 9, 2024 · 8 comments

Comments

@henzeb
Copy link

henzeb commented Jan 9, 2024

Description

The following code is a bug since the introduction of anonymous classes in PHP 7.0:

https://3v4l.org/0OTZ9#v7.0.0 // for reference
https://3v4l.org/0OTZ9#v8.3.1

<?php

$anonymous = new class(){};

echo get_class($anonymous).': now you see me...';

trigger_error(
    get_class($anonymous).' ...now you don\'t!', 
    E_USER_ERROR
);

Note: it doesn't matter if get_class or $anonymous::class is used. As soon as it is an anonymous class name, it fails.

It also fails when throwing an Exception:

<?php

$anonymous = new class(){};

echo get_class($anonymous).': now you see me...';

throw new Exception(
    get_class($anonymous).' ...now you don\'t!', 
    E_USER_ERROR
);

Resulted in this output:

class@anonymous/in/0OTZ9:3$0: now you see me...
Fatal error: class@anonymous in /in/0OTZ9 on line 7

Process exited with code 255.

But I expected this output instead:

class@anonymous/in/0OTZ9:3$0: now you see me...
Fatal error: class@anonymous ... now you don't in /in/0OTZ9 on line 7

Process exited with code 255.

PHP Version

PHP 8.3.1

Operating System

No response

@henzeb henzeb changed the title Anonymous class reference in trigger_error Anonymous class reference in trigger_error / thrown Exception Jan 9, 2024
@iluuu1994
Copy link
Member

Anonymous classes contain a non printable \0 character. trigger_error apparently ignores the message length and as such misses the latter half of the message.

@henzeb
Copy link
Author

henzeb commented Jan 10, 2024

I guess a workaround would be:

preg_replace('/[\x00]/', '',$class::class);

@iluuu1994
Copy link
Member

I quickly had a look yesterday, but we're relying on some API that doesn't support \0 in messages, which is annoying (printf etc).

@nielsdos
Copy link
Member

nielsdos commented Jan 13, 2024

This seems to work: https://gist.github.com/nielsdos/79419e56941ae80062e1b382c7e275f9
But I didn't test that very thoroughly, also there are two remaining issues:

  • One related to syslog (marked with XXX)
  • The compiler gives warnings because the format attribute is used on the Zend's print-family functions and the compiler doesn't know about the %Z format that we implement

@iluuu1994
Copy link
Member

The fix looks correct to me, although tbh I'm not sure it's worthwhile. For %Z, the _unchecked variants of these functions should be used. Also a nit: I think for fwrite, size and nmemb are supposed to be reversed.

@nielsdos
Copy link
Member

The fix looks correct to me, although tbh I'm not sure it's worthwhile.

Idk either, I don't like the complication.

For %Z, the _unchecked variants of these functions should be used.

TIL, thx

Also a nit: I think for fwrite, size and nmemb are supposed to be reversed.

Hm indeed, confusing function and confusing naming.

@bwoebi
Copy link
Member

bwoebi commented Jan 15, 2024

I would like to see this fixed as well, it's definitely weird and buggy to see error messages cut-off. From the perspective of a PHP user \0 isn't really special.

@iluuu1994
Copy link
Member

I don't object to fixing this. @nielsdos Feel free to create a PR.

nielsdos added a commit to nielsdos/php-src that referenced this issue Jan 15, 2024
nielsdos added a commit that referenced this issue Jan 16, 2024
* PHP-8.2:
  Fix GH-13097: Anonymous class reference in trigger_error / thrown Exception
nielsdos added a commit that referenced this issue Jan 16, 2024
* PHP-8.3:
  Fix GH-13097: Anonymous class reference in trigger_error / thrown Exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants