Skip to content

Commit 63b7a4f

Browse files
committed
Make suggested refactors
- move build_dynamic_parameters to zend_exceptions, avoid the dependency on the BIF header in main.c - (though i'm not sure if exceptions is best place for this function) - add documentation comments to new API surface from this PR
1 parent 502acc3 commit 63b7a4f

3 files changed

Lines changed: 26 additions & 21 deletions

File tree

Zend/zend_exceptions.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t nu
620620
}
621621
/* }}} */
622622

623+
/* {{{ Gets the function arguments printed as a string from a backtrace frame. */
623624
ZEND_API zend_string *zend_trace_function_args_to_string(HashTable *frame) {
624625
zval *tmp;
625626
smart_str str = {0};
@@ -634,6 +635,29 @@ ZEND_API zend_string *zend_trace_function_args_to_string(HashTable *frame) {
634635
smart_str_0(&str);
635636
return str.s ? str.s : ZSTR_EMPTY_ALLOC();
636637
}
638+
/* }}} */
639+
640+
/* {{{ Gets the currently executing function's arguments as a string. Used by php_verror. */
641+
ZEND_API zend_string *zend_trace_current_function_args_string(void) {
642+
zend_string *dynamic_params = NULL;
643+
/* get a backtrace to snarf function args */
644+
zval backtrace, *first_frame;
645+
zend_fetch_debug_backtrace(&backtrace, /* skip_last */ 0, /* options */ 0, /* limit */ 1);
646+
/* can fail esp if low memory condition */
647+
if (Z_TYPE(backtrace) != IS_ARRAY) {
648+
return NULL; /* don't need to free */
649+
}
650+
first_frame = zend_hash_index_find(Z_ARRVAL(backtrace), 0);
651+
if (!first_frame) {
652+
goto free_backtrace;
653+
}
654+
dynamic_params = zend_trace_function_args_to_string(Z_ARRVAL_P(first_frame));
655+
free_backtrace:
656+
zval_ptr_dtor(&backtrace);
657+
/* free the string after we use it */
658+
return dynamic_params;
659+
}
660+
/* }}} */
637661

638662
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
639663
zend_ulong index;

Zend/zend_exceptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ ZEND_API zend_result zend_update_exception_properties(zend_execute_data *execute
6767
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *exception, int severity);
6868
ZEND_NORETURN void zend_exception_uncaught_error(const char *prefix, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);
6969
ZEND_API zend_string *zend_trace_function_args_to_string(HashTable *frame);
70+
ZEND_API zend_string *zend_trace_current_function_args_string(void);
7071
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main);
7172

7273
ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void);

main/main.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,26 +1053,6 @@ static zend_string *escape_html(const char *buffer, size_t buffer_len) {
10531053
return result;
10541054
}
10551055

1056-
static zend_string *build_dynamic_parameters(void) {
1057-
zend_string *dynamic_params = NULL;
1058-
/* get a backtrace to snarf function args */
1059-
zval backtrace, *first_frame;
1060-
zend_fetch_debug_backtrace(&backtrace, /* skip_last */ 0, /* options */ 0, /* limit */ 1);
1061-
/* can fail esp if low memory condition */
1062-
if (Z_TYPE(backtrace) != IS_ARRAY) {
1063-
return NULL; /* don't need to free */
1064-
}
1065-
first_frame = zend_hash_index_find(Z_ARRVAL(backtrace), 0);
1066-
if (!first_frame) {
1067-
goto free_backtrace;
1068-
}
1069-
dynamic_params = zend_trace_function_args_to_string(Z_ARRVAL_P(first_frame));
1070-
free_backtrace:
1071-
zval_ptr_dtor(&backtrace);
1072-
/* free the string after we use it */
1073-
return dynamic_params;
1074-
}
1075-
10761056
/* {{{ php_verror */
10771057
/* php_verror is called from php_error_docref<n> functions.
10781058
* Its purpose is to unify error messages and automatically generate clickable
@@ -1156,7 +1136,7 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
11561136
/* if we still have memory then format the origin */
11571137
if (is_function) {
11581138
zend_string *dynamic_params = NULL;
1159-
dynamic_params = build_dynamic_parameters();
1139+
dynamic_params = zend_trace_current_function_args_string();
11601140
origin_len = spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, dynamic_params ? ZSTR_VAL(dynamic_params) : params);
11611141
if (dynamic_params) {
11621142
zend_string_release(dynamic_params);

0 commit comments

Comments
 (0)