Skip to content

Commit 148f98d

Browse files
committed
Minor refactorings to zend_exceptions()
1 parent 9afc66f commit 148f98d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

Zend/zend_exceptions.c

Lines changed: 13 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,16 +539,18 @@ 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)) {
543+
/* This is a typed property and can only happen if modified via ArrayObject */
543544
zend_error(E_WARNING, "File name is not a string");
544545
smart_str_appends(str, "[unknown file]: ");
545546
} else{
546547
zend_long line = 0;
547548
tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
548549
if (tmp) {
549-
if (Z_TYPE_P(tmp) == IS_LONG) {
550+
if (EXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
550551
line = Z_LVAL_P(tmp);
551552
} else {
553+
/* This is a typed property and can only happen if modified via ArrayObject */
552554
zend_error(E_WARNING, "Line is not an int");
553555
}
554556
}
@@ -566,7 +568,7 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
566568
smart_str_appendc(str, '(');
567569
tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
568570
if (tmp) {
569-
if (Z_TYPE_P(tmp) == IS_ARRAY) {
571+
if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
570572
size_t last_len = ZSTR_LEN(str->s);
571573
zend_string *name;
572574
zval *arg;
@@ -583,21 +585,22 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
583585
ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
584586
}
585587
} else {
588+
/* The trace property is typed and private */
586589
zend_error(E_WARNING, "args element is not an array");
587590
}
588591
}
589592
smart_str_appends(str, ")\n");
590593
}
591594
/* }}} */
592595

593-
ZEND_API zend_string *zend_trace_to_string(HashTable *trace, bool include_main) {
596+
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
594597
zend_ulong index;
595598
zval *frame;
596599
uint32_t num = 0;
597600
smart_str str = {0};
598601

599602
ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
600-
if (Z_TYPE_P(frame) != IS_ARRAY) {
603+
if (UNEXPECTED(Z_TYPE_P(frame) != IS_ARRAY)) {
601604
zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
602605
continue;
603606
}
@@ -624,7 +627,7 @@ ZEND_METHOD(Exception, getTraceAsString)
624627
zval *object = ZEND_THIS;
625628
zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
626629
zval rv;
627-
zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
630+
const zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
628631
if (EG(exception)) {
629632
RETURN_THROWS();
630633
}
@@ -859,14 +862,13 @@ ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception
859862
ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
860863
{
861864
va_list arg;
862-
char *message;
863865
zend_object *obj;
864866

865867
va_start(arg, format);
866-
zend_vspprintf(&message, 0, format, arg);
868+
zend_string *msg_str = zend_vstrpprintf(0, format, arg);
867869
va_end(arg);
868-
obj = zend_throw_exception(exception_ce, message, code);
869-
efree(message);
870+
obj = zend_throw_exception_zstr(exception_ce, msg_str, code);
871+
zend_string_release(msg_str);
870872
return obj;
871873
}
872874
/* }}} */

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)