Skip to content

Zend: Exception::__toString() no need to allocate the method name #19025

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,32 +695,41 @@ ZEND_METHOD(Exception, __toString)
zval trace, *exception;
zend_class_entry *base_ce;
zend_string *str;
zend_fcall_info fci;
zval rv, tmp;
zend_string *fname;

ZEND_PARSE_PARAMETERS_NONE();

str = ZSTR_EMPTY_ALLOC();

exception = ZEND_THIS;
fname = ZSTR_INIT_LITERAL("gettraceasstring", 0);
/* As getTraceAsString method is final we can grab it once */
zend_function *getTraceAsString = zend_hash_str_find_ptr(&zend_ce_exception->function_table, ZEND_STRL("gettraceasstring"));
ZEND_ASSERT(getTraceAsString && "Method getTraceAsString must exist");

zend_fcall_info fci = {
.size = sizeof(fci),
.function_name = {0},
.retval = &trace,
.param_count = 0,
.params = NULL,
.object = Z_OBJ_P(exception),
.named_params = NULL,
};
zend_fcall_info_cache fcc = {
.function_handler = getTraceAsString,
.calling_scope = Z_OBJCE_P(exception),
.called_scope = zend_ce_exception,
.object = Z_OBJ_P(exception),
.closure = NULL,
};

while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
zend_string *prev_str = str;
zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));

fci.size = sizeof(fci);
ZVAL_STR(&fci.function_name, fname);
fci.object = Z_OBJ_P(exception);
fci.retval = &trace;
fci.param_count = 0;
fci.params = NULL;
fci.named_params = NULL;

zend_call_function(&fci, NULL);
zend_call_function(&fci, &fcc);

if (Z_TYPE(trace) != IS_STRING) {
zval_ptr_dtor(&trace);
Expand Down Expand Up @@ -765,7 +774,6 @@ ZEND_METHOD(Exception, __toString)
break;
}
}
zend_string_release_ex(fname, 0);

exception = ZEND_THIS;
/* Reset apply counts */
Expand Down
Loading