Skip to content

Commit 6819ad9

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 5281590 commit 6819ad9

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
@@ -597,6 +597,7 @@ static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t nu
597597
}
598598
/* }}} */
599599

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

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

Zend/zend_exceptions.h

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

7879
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
@@ -59,7 +59,6 @@
5959
#include "ext/standard/flock_compat.h"
6060
#endif
6161
#include "php_syslog.h"
62-
#include "Zend/zend_builtin_functions.h"
6362
#include "Zend/zend_exceptions.h"
6463

6564
#if PHP_SIGCHILD
@@ -986,26 +985,6 @@ static zend_string *escape_html(const char *buffer, size_t buffer_len) {
986985
return result;
987986
}
988987

989-
static zend_string *build_dynamic_parameters(void) {
990-
zend_string *dynamic_params = NULL;
991-
/* get a backtrace to snarf function args */
992-
zval backtrace, *first_frame;
993-
zend_fetch_debug_backtrace(&backtrace, /* skip_last */ 0, /* options */ 0, /* limit */ 1);
994-
/* can fail esp if low memory condition */
995-
if (Z_TYPE(backtrace) != IS_ARRAY) {
996-
return NULL; /* don't need to free */
997-
}
998-
first_frame = zend_hash_index_find(Z_ARRVAL(backtrace), 0);
999-
if (!first_frame) {
1000-
goto free_backtrace;
1001-
}
1002-
dynamic_params = zend_trace_function_args_to_string(Z_ARRVAL_P(first_frame));
1003-
free_backtrace:
1004-
zval_ptr_dtor(&backtrace);
1005-
/* free the string after we use it */
1006-
return dynamic_params;
1007-
}
1008-
1009988
/* {{{ php_verror */
1010989
/* php_verror is called from php_error_docref<n> functions.
1011990
* Its purpose is to unify error messages and automatically generate clickable
@@ -1089,7 +1068,7 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ
10891068
/* if we still have memory then format the origin */
10901069
if (is_function) {
10911070
zend_string *dynamic_params = NULL;
1092-
dynamic_params = build_dynamic_parameters();
1071+
dynamic_params = zend_trace_current_function_args_string();
10931072
origin_len = (int)spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, dynamic_params ? ZSTR_VAL(dynamic_params) : params);
10941073
if (dynamic_params) {
10951074
zend_string_release(dynamic_params);

0 commit comments

Comments
 (0)