From e0c8dd2d520f4d926d5dbc45516d8f761acf40a7 Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Sun, 21 Jul 2013 01:31:34 +0800 Subject: [PATCH] fix core dump bug! in str_functs.cpp --- src/cppcommon/str_functs.cpp | 104 ++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/src/cppcommon/str_functs.cpp b/src/cppcommon/str_functs.cpp index 1c5514f..9299e7d 100644 --- a/src/cppcommon/str_functs.cpp +++ b/src/cppcommon/str_functs.cpp @@ -216,21 +216,24 @@ namespace CPPCOMMON string unicodeToUtf8(const string& uniStr) { size_t len = uniStr.size(); - if(len%2) + if(uniStr.empty() || len%2) { return ""; } - uint16_t * uniArr = new uint16_t[len>>1]; - char * utfStr = new char[len<<1]; + uint16_t * uniArr = new uint16_t[(len>>1) + 1]; + if(NULL == uniArr) + { + return ""; + } + char * utfStr = new char[(len<<1) + 1]; + if(NULL == utfStr) + { + delete [] uniArr; + return ""; + } for(int i = 0; i < len; i+=2) { - //uint16_t tmp1 = uniStr[i]; - //tmp1 <<= 8; - //tmp1 &= 0xff00; - //uint16_t tmp2 = uniStr[i+1]; - //tmp2 &= 0x00ff; - //uniArr[i>>1] = tmp1 | tmp2; uniArr[i>>1] = twocharToUint16(uniStr[i], uniStr[i+1]); } @@ -250,6 +253,7 @@ namespace CPPCOMMON } /*from: http://www.cppblog.com/lf426/archive/2008/03/31/45796.html */ + /*if the inutf8 is not utf8 , this function maybe cause core dump!!!*/ int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode) { int length; @@ -280,27 +284,31 @@ namespace CPPCOMMON length++; } - *unicode = 0; + + //*unicode = 0; !! this may cause out range of array; return length; } string utf8ToUnicode(const string& utfStr) { - uint16_t* pUni = new uint16_t[utfStr.size()]; - size_t uniLen = utf8ToUnicode(utfStr.c_str(), utfStr.size(), pUni); - string res; - if(uniLen ==0 ) + cout<<__FILE__<<__LINE__< char2= uint16ToChar2(pUni[i]); - res += char2.first; - res += char2.second; - } + return ""; + } + size_t uniLen = utf8ToUnicode(utfStr.c_str(), utfStr.size(), pUni); + string res(""); + for(uint i = 0; i < uniLen; i++) + { + + pair char2= uint16ToChar2(pUni[i]); + res += char2.first; + res += char2.second; } delete [] pUni; return res; @@ -317,7 +325,6 @@ namespace CPPCOMMON cd = iconv_open(to_charset,from_charset); if (cd==NULL) { - //cout<<__FILE__<<__LINE__< utf8 string gbkToUtf8(const string& gbk) { - //cout<<__FILE__<<__LINE__< tmp; - //tmp.push_back("1"); - ////tmp.push_back("2"); - ////tmp.clear(); - //cout< tmp; + //tmp.push_back("1"); + ////tmp.push_back("2"); + ////tmp.clear(); + //cout<