Skip to content

Commit 23b8d64

Browse files
authored
Zend: Minor refactorings to zend_exceptions() (#16684)
1 parent b48fdcb commit 23b8d64

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Zend/zend_exceptions.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
529529
}
530530
/* }}} */
531531

532-
static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /* {{{ */
532+
static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t num) /* {{{ */
533533
{
534534
zval *file, *tmp;
535535

@@ -539,14 +539,14 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
539539

540540
file = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_FILE));
541541
if (file) {
542-
if (Z_TYPE_P(file) != IS_STRING) {
542+
if (UNEXPECTED(Z_TYPE_P(file) != IS_STRING)) {
543543
zend_error(E_WARNING, "File name is not a string");
544544
smart_str_appends(str, "[unknown file]: ");
545545
} else{
546546
zend_long line = 0;
547547
tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
548548
if (tmp) {
549-
if (Z_TYPE_P(tmp) == IS_LONG) {
549+
if (EXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
550550
line = Z_LVAL_P(tmp);
551551
} else {
552552
zend_error(E_WARNING, "Line is not an int");
@@ -566,7 +566,7 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
566566
smart_str_appendc(str, '(');
567567
tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
568568
if (tmp) {
569-
if (Z_TYPE_P(tmp) == IS_ARRAY) {
569+
if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
570570
size_t last_len = ZSTR_LEN(str->s);
571571
zend_string *name;
572572
zval *arg;
@@ -590,14 +590,14 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
590590
}
591591
/* }}} */
592592

593-
ZEND_API zend_string *zend_trace_to_string(HashTable *trace, bool include_main) {
593+
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
594594
zend_ulong index;
595595
zval *frame;
596596
uint32_t num = 0;
597597
smart_str str = {0};
598598

599599
ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
600-
if (Z_TYPE_P(frame) != IS_ARRAY) {
600+
if (UNEXPECTED(Z_TYPE_P(frame) != IS_ARRAY)) {
601601
zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
602602
continue;
603603
}
@@ -624,7 +624,7 @@ ZEND_METHOD(Exception, getTraceAsString)
624624
zval *object = ZEND_THIS;
625625
zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
626626
zval rv;
627-
zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
627+
const zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
628628
if (EG(exception)) {
629629
RETURN_THROWS();
630630
}
@@ -859,14 +859,13 @@ ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception
859859
ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
860860
{
861861
va_list arg;
862-
char *message;
863862
zend_object *obj;
864863

865864
va_start(arg, format);
866-
zend_vspprintf(&message, 0, format, arg);
865+
zend_string *msg_str = zend_vstrpprintf(0, format, arg);
867866
va_end(arg);
868-
obj = zend_throw_exception(exception_ce, message, code);
869-
efree(message);
867+
obj = zend_throw_exception_zstr(exception_ce, msg_str, code);
868+
zend_string_release(msg_str);
870869
return obj;
871870
}
872871
/* }}} */

Zend/zend_exceptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extern ZEND_API void (*zend_throw_exception_hook)(zend_object *ex);
7272
/* show an exception using zend_error(severity,...), severity should be E_ERROR */
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);
75-
ZEND_API zend_string *zend_trace_to_string(HashTable *trace, bool include_main);
75+
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main);
7676

7777
ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void);
7878
ZEND_API ZEND_COLD zend_object *zend_create_graceful_exit(void);

0 commit comments

Comments
 (0)