From bf4b0f9d65e5493f34d68cdf3a529bea2adb90fc Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 3 Feb 2022 14:26:57 +0100 Subject: [PATCH] Fix GH-7980: Unexpected result for iconv_mime_decode We need to reset the shift state right after conversion, to cater to potenially following plain encodings. Also, there is no need to reset the shift for plain encodings, because these are not state-dependent. --- ext/iconv/iconv.c | 10 +++------- ext/iconv/tests/gh7980.phpt | 13 +++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 ext/iconv/tests/gh7980.phpt diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 90c209cbcbf0..f8e9f95ad569 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -1576,6 +1576,9 @@ static php_iconv_err_t _php_iconv_mime_decode(smart_str *pretval, const char *st } err = _php_iconv_appendl(pretval, ZSTR_VAL(decoded_text), ZSTR_LEN(decoded_text), cd); + if (err == PHP_ICONV_ERR_SUCCESS) { + err = _php_iconv_appendl(pretval, NULL, 0, cd); + } zend_string_release_ex(decoded_text, 0); if (err != PHP_ICONV_ERR_SUCCESS) { @@ -1716,13 +1719,6 @@ static php_iconv_err_t _php_iconv_mime_decode(smart_str *pretval, const char *st *next_pos = p1; } - if (cd != (iconv_t)(-1)) { - _php_iconv_appendl(pretval, NULL, 0, cd); - } - if (cd_pl != (iconv_t)(-1)) { - _php_iconv_appendl(pretval, NULL, 0, cd_pl); - } - smart_str_0(pretval); out: if (cd != (iconv_t)(-1)) { diff --git a/ext/iconv/tests/gh7980.phpt b/ext/iconv/tests/gh7980.phpt new file mode 100644 index 000000000000..a2c875a03d9c --- /dev/null +++ b/ext/iconv/tests/gh7980.phpt @@ -0,0 +1,13 @@ +--TEST-- +Bug GH-7980 (Unexpected result for iconv_mime_decode) +--SKIPIF-- + +--FILE-- +'; +var_dump(iconv_mime_decode($subject, ICONV_MIME_DECODE_STRICT, 'UTF-8')); +?> +--EXPECT-- +string(57) "DSI Chargé de Formation Jean Dupont "