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

Add API functions for zend_exception #5157

Closed
wants to merge 2 commits into from

Conversation

morrisonlevi
Copy link
Contributor

Adds:

  • zend_exception_get_code
  • zend_exception_get_file
  • zend_exception_get_line
  • zend_exception_get_message
  • zend_exception_get_trace
  • zend_exception_get_trace_as_string. This accepts an arg for
    controlling whether the arguments will be included.

Also adds zend_obj_read_property and zend_obj_read_property_ex. These are just like zend_read_property/zend_read_property_ex except they take a zend_object * instead of a zval *.

I intended these to mostly be used by extensions, but with a quick look did find some places where they were useful in core too.

@morrisonlevi morrisonlevi force-pushed the levim/exception-api branch 2 times, most recently from b53bd3e to 971ffb3 Compare February 6, 2020 21:51
@morrisonlevi morrisonlevi marked this pull request as ready for review February 6, 2020 23:19
Zend/zend_API.c Outdated Show resolved Hide resolved
ZVAL_DEREF(prop);
if (Z_TYPE_P(prop) != IS_LONG) {
if (!silent) {
zend_type_error("Exception %s was not a long", ZSTR_VAL(ZSTR_KNOWN(id)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it make more sense to switch these to use typed properties, so this is enforced with the now built-in mechanism? Seems like something we can do in PHP 8...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, it makes no sense to check types during property reading. The wrong values must not be written there at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can typed properties be subverted via reflection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is certainly PDOException here where code is a string. Same could be true for any other userland exception which just writes to $this->code. Changing code to a typed string is a serious BC break.

 - zend_exception_get_code
 - zend_exception_get_file
 - zend_exception_get_line
 - zend_exception_get_message
 - zend_exception_get_trace
 - zend_exception_get_trace_as_string. This accepts an arg for
   controlling whether the arguments will be included.
@morrisonlevi
Copy link
Contributor Author

I have rebased on master and removed the zend_obj_read_property{,_ex} APIs.

Regarding using typed properties: can it be subverted via reflection? If not, are there any examples of typed properties in core already?

ZEND_API zend_long *zend_exception_get_line(zend_object *exception, zend_bool silent);
ZEND_API zend_string *zend_exception_get_message(zend_object *exception, zend_bool silent);
ZEND_API HashTable *zend_exception_get_trace(zend_object *exception, zend_bool silent);
ZEND_API zend_string *zend_exception_get_trace_as_string(zend_object *exception, zend_bool include_args, zend_bool silent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have an argument here and in zend_exception_get_trace that limits the number of returned items to $n?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limiting the stack depth seems like a good idea.

@php php deleted a comment from morrisonlevi Feb 14, 2020
@morrisonlevi
Copy link
Contributor Author

This PR doesn't change the status quo -- there was a runtime error before and it has one now. I'm wondering if we can agree to just merge it in. If we get time before 8 is released we can do the follow-up improvements:

  • Add stack depth limiting.
  • Use typed properties where applicable.

Any opposition?

@beberlei
Copy link
Contributor

beberlei commented Apr 9, 2020

I am +1 for merging this.

@morrisonlevi
Copy link
Contributor Author

Ran out of steam for 8.0; may revisit for 8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants