Skip to content

Commit 959bb00

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 b024012 commit 959bb00

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

Zend/zend_exceptions.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
594594
}
595595
/* }}} */
596596

597+
/* {{{ Gets the function arguments printed as a string from a backtrace frame. */
597598
ZEND_API zend_string *zend_trace_function_args_to_string(HashTable *frame) {
598599
zval *tmp;
599600
smart_str str = {0};
@@ -608,6 +609,29 @@ ZEND_API zend_string *zend_trace_function_args_to_string(HashTable *frame) {
608609
smart_str_0(&str);
609610
return str.s ? str.s : ZSTR_EMPTY_ALLOC();
610611
}
612+
/* }}} */
613+
614+
/* {{{ Gets the currently executing function's arguments as a string. Used by php_verror. */
615+
ZEND_API zend_string *zend_trace_current_function_args_string(void) {
616+
zend_string *dynamic_params = NULL;
617+
/* get a backtrace to snarf function args */
618+
zval backtrace, *first_frame;
619+
zend_fetch_debug_backtrace(&backtrace, /* skip_last */ 0, /* options */ 0, /* limit */ 1);
620+
/* can fail esp if low memory condition */
621+
if (Z_TYPE(backtrace) != IS_ARRAY) {
622+
return NULL; /* don't need to free */
623+
}
624+
first_frame = zend_hash_index_find(Z_ARRVAL(backtrace), 0);
625+
if (!first_frame) {
626+
goto free_backtrace;
627+
}
628+
dynamic_params = zend_trace_function_args_to_string(Z_ARRVAL_P(first_frame));
629+
free_backtrace:
630+
zval_ptr_dtor(&backtrace);
631+
/* free the string after we use it */
632+
return dynamic_params;
633+
}
634+
/* }}} */
611635

612636
ZEND_API zend_string *zend_trace_to_string(HashTable *trace, bool include_main) {
613637
zend_ulong index;

Zend/zend_exceptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern ZEND_API void (*zend_throw_exception_hook)(zend_object *ex);
7070
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *exception, int severity);
7171
ZEND_NORETURN void zend_exception_uncaught_error(const char *prefix, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);
7272
ZEND_API zend_string *zend_trace_function_args_to_string(HashTable *frame);
73+
ZEND_API zend_string *zend_trace_current_function_args_string(void);
7374
ZEND_API zend_string *zend_trace_to_string(HashTable *trace, bool include_main);
7475

7576
ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void);

main/main.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#include "ext/standard/flock_compat.h"
5858
#endif
5959
#include "php_syslog.h"
60-
#include "Zend/zend_builtin_functions.h"
6160
#include "Zend/zend_exceptions.h"
6261

6362
#if PHP_SIGCHILD
@@ -937,26 +936,6 @@ static zend_string *escape_html(const char *buffer, size_t buffer_len) {
937936
return result;
938937
}
939938

940-
static zend_string *build_dynamic_parameters(void) {
941-
zend_string *dynamic_params = NULL;
942-
/* get a backtrace to snarf function args */
943-
zval backtrace, *first_frame;
944-
zend_fetch_debug_backtrace(&backtrace, /* skip_last */ 0, /* options */ 0, /* limit */ 1);
945-
/* can fail esp if low memory condition */
946-
if (Z_TYPE(backtrace) != IS_ARRAY) {
947-
return NULL; /* don't need to free */
948-
}
949-
first_frame = zend_hash_index_find(Z_ARRVAL(backtrace), 0);
950-
if (!first_frame) {
951-
goto free_backtrace;
952-
}
953-
dynamic_params = zend_trace_function_args_to_string(Z_ARRVAL_P(first_frame));
954-
free_backtrace:
955-
zval_ptr_dtor(&backtrace);
956-
/* free the string after we use it */
957-
return dynamic_params;
958-
}
959-
960939
/* {{{ php_verror */
961940
/* php_verror is called from php_error_docref<n> functions.
962941
* Its purpose is to unify error messages and automatically generate clickable
@@ -1043,7 +1022,7 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
10431022
/* if we still have memory then format the origin */
10441023
if (is_function) {
10451024
zend_string *dynamic_params = NULL;
1046-
dynamic_params = build_dynamic_parameters();
1025+
dynamic_params = zend_trace_current_function_args_string();
10471026
origin_len = (int)spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, dynamic_params ? ZSTR_VAL(dynamic_params) : params);
10481027
if (dynamic_params) {
10491028
zend_string_release(dynamic_params);

0 commit comments

Comments
 (0)