php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53891 iconv_mime_encode() fails to Q-encode UTF-8 string
Submitted: 2011-01-31 14:25 UTC Modified: 2018-08-25 12:32 UTC
Votes:16
Avg. Score:4.3 ± 0.8
Reproduced:14 of 14 (100.0%)
Same Version:5 (35.7%)
Same OS:8 (57.1%)
From: can at strg dot at Assigned: cmb (profile)
Status: Closed Package: ICONV related
PHP Version: 5.3.5 OS: GNU/linux (gentoo/kernel 2.6.35)
Private report: No CVE-ID: None
 [2011-01-31 14:25 UTC] can at strg dot at
Description:
------------
iconv_mime_encode() fails on certain UTF-8 strings containing at least one multibyte character. The error is reproducable for these strings.

I have further noticed that it is more likely that this error occurs if a multibyte character is found at the following character positions within the string:
24±1, 52±3, 82±4, 112±5, 143±5

The code that led me to the previous conclusion can be found here: http://pastebin.com/GXQZ129M

Test script:
---------------
$preferences = array(
    'scheme' => 'Q',
    'input-charset'  => 'utf-8',
    'output-charset' => 'utf-8',
    'line-length' => 74,
    'line-break-chars' => "\r\n",
);
// Note the ellipsis character at the end of the following string:
iconv_mime_encode('subject', "d obeybiubrsfqllpdtpge…", $preferences);

Actual result:
--------------
PHP Notice:  iconv_mime_encode(): Unknown error (7) in /home/soulmerge/test.php on line 11

Notice: iconv_mime_encode(): Unknown error (7) in /home/soulmerge/test.php on line 11



Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-01 05:34 UTC] aharvey@php.net
-Package: Mail related +Package: ICONV related
 [2015-09-09 18:09 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2015-09-09 18:09 UTC] cmb@php.net
Confirmed: <https://3v4l.org/JLman>

Error 7 is likely E2BIG, what occurs if there's not enough room at
*outbuf when calling iconv().
 [2015-09-09 22:18 UTC] cmb@php.net
-Status: Verified +Status: Analyzed -Assigned To: +Assigned To: cmb
 [2015-09-09 22:18 UTC] cmb@php.net
The culprit is the check whether there is sufficient place in the
line to add another encoded word[1]. out_charset_len + 12 is not
sufficient for quoted-printable encoding of UTF-8. In the worst
case it is out_charset_len + BOILERPLATE + CHARS_PER_BYTE *
MAX_BYTES (where BOILERPLATE == 7, CHARS_PER_BYTE == 3 and
MAX_BYTES == 6), i.e. out_charset_len + 25.

AFAIK, this would be sufficient for all other character encodings
and for Base64 encoding as well. Of course, changing this
threshold might change the result of iconv_mime_encode with regard
to folding, but that supposedly would only break some tests, if
they're relying on the exact output. It would more severly affect
any code that uses small line-length values, but presumably such
code doesn't exist – actually, I can't see a reason to set
line-length < 76.

[1] <https://github.com/php/php-src/blob/php-7.0.0RC2/ext/iconv/iconv.c#L1238>
 [2016-08-13 11:12 UTC] cmb@php.net
-Status: Analyzed +Status: Verified -Assigned To: cmb +Assigned To:
 [2016-08-13 11:12 UTC] cmb@php.net
Only changing the threshold does not work, unfortunately.
 [2017-12-06 08:27 UTC] af at ins dot de
We have encountered the same bug when encoding UTF-8 strings with PHP 5.6.32 and PHP 7.1.7. A fix after this long time would be really nice.

As a sidenote: This error impacts also the ZendFramework (5.6.32) because Zend/Mail;Zend/Header is checking with iconv_mime_encode() if a subject line is valid and throws a "Subject value must be composed of printable US-ASCII or UTF-8 characters" instead of sending the mail when the bug occurs.
 [2017-12-06 08:30 UTC] af at ins dot de
Sorry. ZendFramework-version is obviouisly not 5.6.32, it is: 2.4.13
 [2018-08-12 13:25 UTC] cmb@php.net
-Summary: iconv_mime_encode() fails converting UTF-8 string to quoted-printable +Summary: iconv_mime_encode() fails to Q-encode UTF-8 string
 [2018-08-12 13:25 UTC] cmb@php.net
<https://github.com/php/php-src/pull/3436> is supposed to resolve
this issue.
 [2018-08-25 12:31 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=eb03290754105dbefeb5a3d75c20e504cd82715b
Log: Fix #53891: iconv_mime_encode() fails to Q-encode UTF-8 string
 [2018-08-25 12:31 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2018-08-25 12:32 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 15:01:28 2024 UTC