php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43314 iconv_mime_encode(), broken Q scheme
Submitted: 2007-11-16 16:23 UTC Modified: 2010-03-02 10:24 UTC
Votes:16
Avg. Score:4.7 ± 0.6
Reproduced:16 of 16 (100.0%)
Same Version:3 (18.8%)
Same OS:5 (31.2%)
From: wiela at centras dot lt Assigned: rasmus (profile)
Status: Closed Package: ICONV related
PHP Version: 5.2.5 OS: Windows XP HE
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wiela at centras dot lt
New email:
PHP Version: OS:

 

 [2007-11-16 16:23 UTC] wiela at centras dot lt
Description:
------------
iconv_mime_encode(),'Q' encoding scheme isn't reliable and 
sometimes (for particular character and/or string length combination?) returns: "Notice: iconv_mime_encode(): Unknown error (7) in ..."
*without any result*. 

This also applies to earlier php versions (tested with php 5.2.1).



Reproduce code:
---------------
$preferences = array(
    "input-charset" => "UTF-8",
    "output-charset" => "UTF-8",
    "line-length" => 76,
    "line-break-chars" => "\n",
    "scheme" => "Q"
);

// $str1 results error, it's utf-8 string, its base64_encode() is: 
//'xIXEjcSZxJfEr8WhxbPFviDEr8SZxI3FocWzxJnEr8SFIMSNxJnFs8SFxaHFs8Wr'
$str1 = "ąčęėį?ų? įęč?ųęįą čęųą?ųū"; 

// $str2 doesn't result error, although it's only one character
// shorter. It's utf-8 string, its base64_encode() is: 
//'xIXEjcSZxJfEr8WhxbPFviDEr8SZxI3FocWzxJnEr8SFIMSNxJnFs8SFxaHFsw=='
$str2 = "ąčęėį?ų? įęč?ųęįą čęųą?ų";

echo iconv_mime_encode("Subject", $str1, $preferences);
echo iconv_mime_encode("Subject", $str2, $preferences);


Expected result:
----------------
Well, at least any (*some*) result is expected, without any 
errors and warnings. 

For $str1 is expected:
Subject: =?UTF-8?Q?=C4=85=C4=8D=C4=99=C4=97=C4=AF=C5=A1=C5=B3?=
 =?UTF-8?Q?=C5=BE=20=C4=AF=C4=99=C4=8D=C5=A1=C5=B3=C4=99=C4=AF?=
 =?UTF-8?Q?=C4=85=20=C4=8D=C4=99=C5=B3=C4=85=C5=A1=C5=B3=C5=AB?=

For $str2 is expected:
Subject: =?UTF-8?Q?=C4=85=C4=8D=C4=99=C4=97=C4=AF=C5=A1=C5=B3?=
 =?UTF-8?Q?=C5=BE=20=C4=AF=C4=99=C4=8D=C5=A1=C5=B3=C4=99=C4=AF?=
 =?UTF-8?Q?=C4=85=20=C4=8D=C4=99=C5=B3=C4=85=C5=A1=C5=B3?=

Actual result:
--------------
For $str1: 
FALSE with "Notice: iconv_mime_encode(): Unknown error (7) in ..."


For $str2:
Subject: =?UTF-8?Q?=C4=85=C4=8D=C4=99=C4=97=C4=AF=C5=A1=C5=B3?=
 =?UTF-8?Q?=C5=BE=20=C4=AF=C4=99=C4=8D=C5=A1=C5=B3=C4=99=C4=AF?=
 =?UTF-8?Q?=C4=85=20=C4=8D=C4=99=C5=B3=C4=85=C5=A1=C5=B3?=

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-01 14:10 UTC] d_kelsey at uk dot ibm dot com
I encountered a similar problem with another utf-8 string, and although this may not be the best way to fix it, this change provides a workaround.

in iconv.c (line 1281 in php5.2.5) the line
out_size -= ((nbytes_required - (char_cnt - 2)) + 1) / (3 - 1);

should be changed to
out_size -= ((nbytes_required - (char_cnt - 2)) + 1) / 3;

It looks like the code attempts to determine how many characters would fit into output buffer when converted (given that it has gone over the limit), but it assumes that on average each character uses 2 bytes (ie an even mixture of encoded and printable characters). A lot of strings will be greater than this and out_size will be set to a very large positive number (as it subtracts a larger number from out_size and being unsigned will result in a large positive number).
The workaround is to take the worst case scenario and assume all characters generated 3 bytes (ie all encoded).
 [2009-01-09 14:38 UTC] om at viazenetti dot de
Hm, is this bugged fixed in newer versions? Currently we are using version 5.2.6 and the error still occures.
 [2009-11-28 22:18 UTC] dennispopel at gmail dot com
Same on Vista/PHP5.3.0
 [2010-03-02 01:34 UTC] rasmus@php.net
-Status: No Feedback +Status: Closed
 [2010-03-02 01:34 UTC] rasmus@php.net
Fixed in SVN
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC