From 34e2d3f870d3ab80187777fa45c42942a5baedf1 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 20 Apr 2025 02:30:25 +0200 Subject: [PATCH 1/2] Avoid pointless refcounting in php_intl_idn_to_46() --- ext/intl/idn/idn.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ext/intl/idn/idn.c b/ext/intl/idn/idn.c index 62ee83f62f9ba..a0ea0795bf563 100644 --- a/ext/intl/idn/idn.c +++ b/ext/intl/idn/idn.c @@ -90,12 +90,6 @@ static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS, ZSTR_VAL(buffer)[len] = '\0'; ZSTR_LEN(buffer) = len; - if (info.errors == 0) { - RETVAL_STR_COPY(buffer); - } else { - RETVAL_FALSE; - } - if (idna_info) { add_assoc_str_ex(idna_info, "result", sizeof("result")-1, zend_string_copy(buffer)); add_assoc_bool_ex(idna_info, "isTransitionalDifferent", @@ -103,7 +97,13 @@ static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS, add_assoc_long_ex(idna_info, "errors", sizeof("errors")-1, (zend_long)info.errors); } - zend_string_release(buffer); + if (info.errors == 0) { + RETVAL_STR(buffer); + } else { + zend_string_release_ex(buffer, false); + RETVAL_FALSE; + } + uidna_close(uts46); } From 358114e72ec93e6f5f1bb987a7acd2d944d6c11f Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 20 Apr 2025 02:31:15 +0200 Subject: [PATCH 2/2] Factor out duplicated code --- ext/intl/idn/idn.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/ext/intl/idn/idn.c b/ext/intl/idn/idn.c index a0ea0795bf563..cd4546ad7f8bb 100644 --- a/ext/intl/idn/idn.c +++ b/ext/intl/idn/idn.c @@ -57,6 +57,7 @@ static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS, UErrorCode status = U_ZERO_ERROR; UIDNA *uts46; int32_t len; + int32_t buffer_capac; zend_string *buffer; UIDNAInfo info = UIDNA_INFO_INITIALIZER; @@ -66,25 +67,20 @@ static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS, } if (mode == INTL_IDN_TO_ASCII) { - const int32_t buffer_capac = 255; + buffer_capac = 255; buffer = zend_string_alloc(buffer_capac, 0); len = uidna_nameToASCII_UTF8(uts46, ZSTR_VAL(domain), ZSTR_LEN(domain), ZSTR_VAL(buffer), buffer_capac, &info, &status); - if (len >= buffer_capac || php_intl_idn_check_status(status, "failed to convert name") == FAILURE) { - uidna_close(uts46); - zend_string_efree(buffer); - RETURN_FALSE; - } } else { - const int32_t buffer_capac = 252*4; + buffer_capac = 252*4; buffer = zend_string_alloc(buffer_capac, 0); len = uidna_nameToUnicodeUTF8(uts46, ZSTR_VAL(domain), ZSTR_LEN(domain), ZSTR_VAL(buffer), buffer_capac, &info, &status); - if (len >= buffer_capac || php_intl_idn_check_status(status, "failed to convert name") == FAILURE) { - uidna_close(uts46); - zend_string_efree(buffer); - RETURN_FALSE; - } + } + if (len >= buffer_capac || php_intl_idn_check_status(status, "failed to convert name") == FAILURE) { + uidna_close(uts46); + zend_string_efree(buffer); + RETURN_FALSE; } ZSTR_VAL(buffer)[len] = '\0';