From: Dmitry Stogov Date: Wed, 5 Sep 2007 10:18:38 +0000 (+0000) Subject: Fixed bug #42488 (SoapServer reports an encoding error and the error itself breaks). X-Git-Tag: RELEASE_2_0_0a1~1878 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5f28a1da4e16361b6348a024c8b8b6259a5f3e3c;p=php Fixed bug #42488 (SoapServer reports an encoding error and the error itself breaks). --- diff --git a/ext/soap/soap.c b/ext/soap/soap.c index f8c6189da1..ec7a230438 100644 --- a/ext/soap/soap.c +++ b/ext/soap/soap.c @@ -343,13 +343,49 @@ char* soap_encode_string_ex(zend_uchar type, zstr data, int len TSRMLS_DC) if (n >= 0) { efree(str); str = estrdup((char*)xmlBufferContent(out)); - } else if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) { - soap_error1(E_ERROR, "Encoding: string '%s' is not a valid utf-8 string", str); } xmlBufferFree(out); xmlBufferFree(in); - } else if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) { - soap_error1(E_ERROR, "Encoding: string '%s' is not a valid utf-8 string", str); + } + if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) { + char *err = emalloc(new_len + 8); + char c; + int i; + + memcpy(err, str, new_len+1); + for (i = 0; (c = err[i++]);) { + if ((c & 0x80) == 0) { + } else if ((c & 0xe0) == 0xc0) { + if ((err[i] & 0xc0) != 0x80) { + break; + } + i++; + } else if ((c & 0xf0) == 0xe0) { + if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80) { + break; + } + i += 2; + } else if ((c & 0xf8) == 0xf0) { + if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80 || (err[i+2] & 0xc0) != 0x80) { + break; + } + i += 3; + } else { + break; + } + } + if (c) { + err[i-1] = '\\'; + err[i++] = 'x'; + err[i++] = ((unsigned char)c >> 4) + ((((unsigned char)c >> 4) > 9) ? ('a' - 10) : '0'); + err[i++] = (c & 15) + (((c & 15) > 9) ? ('a' - 10) : '0'); + err[i++] = '.'; + err[i++] = '.'; + err[i++] = '.'; + err[i++] = 0; + } + + soap_error1(E_ERROR, "Encoding: string '%s' is not a valid utf-8 string", err); } } return str; @@ -386,13 +422,49 @@ char* soap_encode_string(zval *data, int* len TSRMLS_DC) efree(str); str = estrdup((char*)xmlBufferContent(out)); new_len = n; - } else if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) { - soap_error1(E_ERROR, "Encoding: string '%s' is not a valid utf-8 string", str); } xmlBufferFree(out); xmlBufferFree(in); - } else if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) { - soap_error1(E_ERROR, "Encoding: string '%s' is not a valid utf-8 string", str); + } + if (!php_libxml_xmlCheckUTF8(BAD_CAST(str))) { + char *err = emalloc(new_len + 8); + char c; + int i; + + memcpy(err, str, new_len+1); + for (i = 0; (c = err[i++]);) { + if ((c & 0x80) == 0) { + } else if ((c & 0xe0) == 0xc0) { + if ((err[i] & 0xc0) != 0x80) { + break; + } + i++; + } else if ((c & 0xf0) == 0xe0) { + if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80) { + break; + } + i += 2; + } else if ((c & 0xf8) == 0xf0) { + if ((err[i] & 0xc0) != 0x80 || (err[i+1] & 0xc0) != 0x80 || (err[i+2] & 0xc0) != 0x80) { + break; + } + i += 3; + } else { + break; + } + } + if (c) { + err[i-1] = '\\'; + err[i++] = 'x'; + err[i++] = ((unsigned char)c >> 4) + ((((unsigned char)c >> 4) > 9) ? ('a' - 10) : '0'); + err[i++] = (c & 15) + (((c & 15) > 9) ? ('a' - 10) : '0'); + err[i++] = '.'; + err[i++] = '.'; + err[i++] = '.'; + err[i++] = 0; + } + + soap_error1(E_ERROR, "Encoding: string '%s' is not a valid utf-8 string", err); } } if (len) { diff --git a/ext/soap/tests/bugs/bug42488.phpt b/ext/soap/tests/bugs/bug42488.phpt new file mode 100755 index 0000000000..1b62f6f56d --- /dev/null +++ b/ext/soap/tests/bugs/bug42488.phpt @@ -0,0 +1,23 @@ +--TEST-- +Bug #42488 (SoapServer reports an encoding error and the error itself breaks) +--SKIPIF-- + + +--INI-- +soap.wsdl_cache_enabled=0 +--FILE-- + + +EOF; +$soap = new SoapServer(NULL, array('uri'=>'test://')); +function getBadUTF(){ + return "stuff\x93thing"; +} +$soap->addFunction('getBadUTF'); +$soap->handle($request); +?> +--EXPECT-- + +SOAP-ENV:ServerSOAP-ERROR: Encoding: string 'stuff\x93...' is not a valid utf-8 string \ No newline at end of file