From 24791440b20b124b90bf4cc81920db248eeb2a71 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Fri, 11 Apr 2025 23:02:30 +0100 Subject: [PATCH 1/5] ext/iconv: Reduce scope of variables --- ext/iconv/iconv.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index b61d253c5d4be..fb3741f0dd892 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -298,17 +298,18 @@ static php_output_handler *php_iconv_output_handler_init(const char *handler_nam static zend_result php_iconv_output_handler(void **nothing, php_output_context *output_context) { - char *s, *content_type, *mimetype = NULL; - int output_status, mimetype_len = 0; + char *content_type, *mimetype = NULL; if (output_context->op & PHP_OUTPUT_HANDLER_START) { - output_status = php_output_get_status(); + int output_status = php_output_get_status(); if (output_status & PHP_OUTPUT_SENT) { return FAILURE; } + int mimetype_len = 0; if (SG(sapi_headers).mimetype && !strncasecmp(SG(sapi_headers).mimetype, "text/", 5)) { - if ((s = strchr(SG(sapi_headers).mimetype,';')) == NULL){ + char *s = strchr(SG(sapi_headers).mimetype,';'); + if (s == NULL){ mimetype = SG(sapi_headers).mimetype; } else { mimetype = SG(sapi_headers).mimetype; @@ -775,7 +776,6 @@ static php_iconv_err_t _php_iconv_strpos(size_t *pretval, size_t match_ofs; int more; - size_t iconv_ret; *pretval = (size_t)-1; @@ -812,7 +812,7 @@ static php_iconv_err_t _php_iconv_strpos(size_t *pretval, more = in_left > 0; - iconv_ret = iconv(cd, more ? (ICONV_CONST char **)&in_p : NULL, more ? &in_left : NULL, (char **) &out_p, &out_left); + size_t iconv_ret = iconv(cd, more ? (ICONV_CONST char **)&in_p : NULL, more ? &in_left : NULL, (char **) &out_p, &out_left); if (out_left == sizeof(buf)) { break; } From 5490e96e0b603233ebc68a236298960f0b7fe274 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Fri, 11 Apr 2025 22:59:59 +0100 Subject: [PATCH 2/5] ext/iconv: Add const specifier --- ext/iconv/iconv.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index fb3741f0dd892..641a0517dad0d 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -63,15 +63,15 @@ #define PHP_ICONV_IMPL_VALUE "unknown" #endif -static char *get_iconv_version(void) { - char *version = "unknown"; +static const char *get_iconv_version(void) { + const char *version = "unknown"; #ifdef HAVE_LIBICONV static char buf[16]; snprintf(buf, sizeof(buf), "%d.%d", _libiconv_version >> 8, _libiconv_version & 0xff); version = buf; #elif defined(HAVE_GLIBC_ICONV) - version = (char *) gnu_get_libc_version(); + version = gnu_get_libc_version(); #endif return version; @@ -243,7 +243,7 @@ PHP_MSHUTDOWN_FUNCTION(miconv) /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(miconv) { - zval *iconv_impl, *iconv_ver; + const zval *iconv_impl, *iconv_ver; iconv_impl = zend_get_constant_str("ICONV_IMPL", sizeof("ICONV_IMPL")-1); iconv_ver = zend_get_constant_str("ICONV_VERSION", sizeof("ICONV_VERSION")-1); @@ -308,7 +308,7 @@ static zend_result php_iconv_output_handler(void **nothing, php_output_context * int mimetype_len = 0; if (SG(sapi_headers).mimetype && !strncasecmp(SG(sapi_headers).mimetype, "text/", 5)) { - char *s = strchr(SG(sapi_headers).mimetype,';'); + const char *s = strchr(SG(sapi_headers).mimetype,';'); if (s == NULL){ mimetype = SG(sapi_headers).mimetype; } else { @@ -321,7 +321,7 @@ static zend_result php_iconv_output_handler(void **nothing, php_output_context * if (mimetype != NULL && (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || ((output_context->op & PHP_OUTPUT_HANDLER_START) && !(output_context->op & PHP_OUTPUT_HANDLER_FINAL)))) { size_t len; - char *p = strstr(get_output_encoding(), "//"); + const char *p = strstr(get_output_encoding(), "//"); if (p) { len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%.*s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, (int) (p - get_output_encoding()), get_output_encoding()); @@ -2577,7 +2577,7 @@ static php_stream_filter *php_iconv_stream_filter_factory_create(const char *nam { php_stream_filter *retval = NULL; php_iconv_stream_filter *inst; - char *from_charset = NULL, *to_charset = NULL; + const char *from_charset = NULL, *to_charset = NULL; size_t from_charset_len, to_charset_len; if ((from_charset = strchr(name, '.')) == NULL) { From dbf9b8c0440e4116ea79b55db3a05c0a133f2525 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Fri, 11 Apr 2025 23:05:42 +0100 Subject: [PATCH 3/5] ext/iconv: Use bool type instead of int type --- ext/iconv/iconv.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 641a0517dad0d..6119fe8c3a336 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -418,16 +418,16 @@ static php_iconv_err_t _php_iconv_appendc(smart_str *d, const char c, iconv_t cd /* {{{ */ #ifdef ICONV_BROKEN_IGNORE -static int _php_check_ignore(const char *charset) +static bool _php_check_ignore(const char *charset) { size_t clen = strlen(charset); if (clen >= 9 && strcmp("//IGNORE", charset+clen-8) == 0) { - return 1; + return true; } if (clen >= 19 && strcmp("//IGNORE//TRANSLIT", charset+clen-18) == 0) { - return 1; + return true; } - return 0; + return false; } #else #define _php_check_ignore(x) (0) @@ -443,7 +443,7 @@ PHP_ICONV_API php_iconv_err_t php_iconv_string(const char *in_p, size_t in_len, size_t bsz, result = 0; php_iconv_err_t retval = PHP_ICONV_ERR_SUCCESS; zend_string *out_buf; - int ignore_ilseq = _php_check_ignore(out_charset); + bool ignore_ilseq = _php_check_ignore(out_charset); *out = NULL; @@ -560,7 +560,7 @@ static php_iconv_err_t _php_iconv_strlen(size_t *pretval, const char *str, size_ size_t out_left; size_t cnt; - int more; + bool more; *pretval = (size_t)-1; @@ -637,7 +637,7 @@ static php_iconv_err_t _php_iconv_substr(smart_str *pretval, size_t cnt; size_t total_len; - int more; + bool more; err = _php_iconv_strlen(&total_len, str, nbytes, enc); if (err != PHP_ICONV_ERR_SUCCESS) { @@ -775,7 +775,7 @@ static php_iconv_err_t _php_iconv_strpos(size_t *pretval, size_t ndl_buf_left; size_t match_ofs; - int more; + bool more; *pretval = (size_t)-1; From 2e6ab1b0fde8782bf10d0ddf6338d02df53171a5 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Fri, 11 Apr 2025 23:09:17 +0100 Subject: [PATCH 4/5] ext/iconv: Hardcode 0 for PHP_ICONV_ERR_SUCCESS value Instead of relying on the zend_result SUCCESS value which doesn't have much to do with it here. --- ext/iconv/php_iconv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/iconv/php_iconv.h b/ext/iconv/php_iconv.h index 7bc1cce0df9f5..fcabdfaf6449e 100644 --- a/ext/iconv/php_iconv.h +++ b/ext/iconv/php_iconv.h @@ -67,7 +67,7 @@ ZEND_TSRMLS_CACHE_EXTERN() /* {{{ typedef enum php_iconv_err_t */ typedef enum _php_iconv_err_t { - PHP_ICONV_ERR_SUCCESS = SUCCESS, + PHP_ICONV_ERR_SUCCESS = 0, PHP_ICONV_ERR_CONVERTER = 1, PHP_ICONV_ERR_WRONG_CHARSET = 2, PHP_ICONV_ERR_TOO_BIG = 3, From b55572b6c65be1cf16bea522e6cf8fb1759a029a Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Fri, 11 Apr 2025 23:36:36 +0100 Subject: [PATCH 5/5] ext/iconv: Use RETURN_BOOL() instead of if/else --- ext/iconv/iconv.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 6119fe8c3a336..157ee41a11a05 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -2255,11 +2255,7 @@ PHP_FUNCTION(iconv_set_encoding) retval = zend_alter_ini_entry(name, charset, PHP_INI_USER, PHP_INI_STAGE_RUNTIME); zend_string_release_ex(name, 0); - if (retval == SUCCESS) { - RETURN_TRUE; - } else { - RETURN_FALSE; - } + RETURN_BOOL(retval == SUCCESS); } /* }}} */