Skip to content

Commit efb86ae

Browse files
committed
Fix #68180: iconv_mime_decode can return extra characters in a header
Basically, the algorithm to append a converted string to an existing `smart_str` works by increasing the `smart_str` buffer, to let `iconv` convert characters until there is no more space, to set the new length of the `smart_str` and to repeat until there is no more input. Formerly, the new length calculation has been wrong, though, since we would have to take the old `out_len` into account (`buf_growth - old_out_len - out_len`). However, since there is no need to take the old `out_len` into account when increasing the `smart_str` buffer, we can simplify the fix, avoiding an additional variable.
1 parent e29c946 commit efb86ae

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PHP NEWS
1414
. Fixed bug #76517 (incorrect restoring of LDFLAGS). (sji)
1515

1616
- iconv:
17+
. Fixed bug #68180 (iconv_mime_decode can return extra characters in a
18+
header). (cmb)
1719
. Fixed bug #63839 (iconv_mime_decode_headers function is skipping headers).
1820
(cmb)
1921
. Fixed bug #60494 (iconv_mime_decode does ignore special characters). (cmb)

ext/iconv/iconv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l,
466466

467467
if (in_p != NULL) {
468468
while (in_left > 0) {
469-
out_left = buf_growth - out_left;
469+
out_left = buf_growth;
470470
smart_str_alloc(d, out_left, 0);
471471

472472
out_p = ZSTR_VAL((d)->s) + ZSTR_LEN((d)->s);
@@ -500,7 +500,7 @@ static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l,
500500
}
501501
} else {
502502
for (;;) {
503-
out_left = buf_growth - out_left;
503+
out_left = buf_growth;
504504
smart_str_alloc(d, out_left, 0);
505505

506506
out_p = ZSTR_VAL((d)->s) + ZSTR_LEN((d)->s);

ext/iconv/tests/bug68180.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #68180 (iconv_mime_decode can return extra characters in a header)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('iconv')) die('skip iconv extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$original = "=?UTF-8?Q?=E3=80=8E=E3=80=90=E5=A4=96=E8=B3=87=E7=B3=BB=E6=88=A6=E7=95=A5=E3=82=B3=E3=83=B3=E3=82=B5=E3=83=AB=E3=81=8C=E9=9B=86=E7=B5=90=E3=80=91=E3=83=88=E3=83=83=E3=83=97=E3=82=B3=E3=83=B3=E3=82=B5=E3=83=AB=E3=82=BF=E3=83=B3=E3=83=88=E3=81=A8=E8=A9=B1=E3=81=9B=E3=82=8B=E3=82=B3=E3=83=B3=E3=82=B5=E3=83=AB=E6=A5=AD=E7=95=8C=E7=A0=94=E7=A9=B6=E3=82=BB=E3=83=9F=E3=83=8A=E3=83=BC=E3=80=8F=E3=81=B8=E3=81=AE=E3=82=A8=E3=83=B3=E3=83=88=E3=83=AA=E3=83=BC=E3=81=82=E3=82=8A=E3=81=8C=E3=81=A8=E3=81=86=E3=81=94=E3=81=96=E3=81=84=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?=";
10+
$decoded = iconv_mime_decode($original, ICONV_MIME_DECODE_STRICT, 'utf-8');
11+
var_dump($decoded);
12+
?>
13+
===DONE===
14+
--EXPECT--
15+
string(183) "『【外資系戦略コンサルが集結】トップコンサルタントと話せるコンサル業界研究セミナー』へのエントリーありがとうございました。"
16+
===DONE===

0 commit comments

Comments
 (0)