php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12757 utf8_encode doesn't terminate strings with '\0'
Submitted: 2001-08-15 05:25 UTC Modified: 2001-08-18 19:42 UTC
From: rnyberg at it dot su dot se Assigned:
Status: Closed Package: Strings related
PHP Version: 4.0.6 OS: any
Private report: No CVE-ID: None
 [2001-08-15 05:25 UTC] rnyberg at it dot su dot se
The enclosed patch fixes the problem.

--- php-4.0.6/ext/xml/xml.c     Thu May 24 14:42:12 2001
+++ php-rnyberg/ext/xml/xml.c   Wed Aug 15 10:45:03 2001
@@ -494,14 +494,14 @@
        if (encoder == NULL) {
                /* If no encoder function was specified, return the data as-is.
                 */
-               newbuf = emalloc(len);
-               memcpy(newbuf, s, len);
+               newbuf = emalloc(len + 1);
+               memcpy(newbuf, s, len + 1);
                *newlen = len;
                return newbuf;
        }
        /* This is the theoretical max (will never get beyond len * 2 as long
         * as we are converting from single-byte characters, though) */
-       newbuf = emalloc(len * 4);
+       newbuf = emalloc(len * 4 + 1);
        while (pos > 0) {
                c = encoder ? encoder((unsigned char)(*s)) : (unsigned short)(*s);
                if (c < 0x80) {
@@ -522,9 +522,10 @@
                pos--;
                s++;
     }
-       if (*newlen < len * 4) {
-               newbuf = erealloc(newbuf, *newlen);
+       if (*newlen < len * 4 + 1) {
+               newbuf = erealloc(newbuf, *newlen + 1);
        }
+       newbuf[*newlen] = '\0';
        return newbuf;
 }
 /* }}} */

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-15 05:44 UTC] sniper@php.net
Seems like at least some of those have already been
fixed in CVS. Could you check the latest CVS snapshot:

http://snaps.php.net/

--Jani

 [2001-08-15 08:01 UTC] rnyberg at it dot su dot se
It's fixed in one place, but it still doesn't set the trailing '\0' 
if encoder == NULL, if I understand it correctly. 

	-Richard
 [2001-08-15 19:10 UTC] sniper@php.net
Could you add a short script that demonstrates what the bug is? 

--Jani


 [2001-08-18 16:01 UTC] rnyberg at it dot su dot se
I noticed the bug when we used utf8-encoded dn in ldap_search. However, the bug that bite me is fixed in the
latest snapshot.

Though it seems to me that there may be another bug in
utf8_encode, if encoder == NULL, since it doesn't look
as if the code in that block takes the trailing '\0' in
consideration.
I don't know how to test this in a PHP-script however, so
it's just my suspicion.

        -Richard

 [2001-08-18 19:42 UTC] sniper@php.net
I made the xml_utf8_encode to null terminate the strings
if the encoding is null. 

--Jani

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC