From 5390cea9222ce8ba5c8b06fecc66f91ec9938f9f Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 4 Nov 2004 23:24:16 +0000 Subject: [PATCH] fix txtin/txtout memleaks iconv and CString are both 8-bit clean; maintain that error checks --- stepmania/src/RageUtil_CharConversions.cpp | 29 +++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/stepmania/src/RageUtil_CharConversions.cpp b/stepmania/src/RageUtil_CharConversions.cpp index cf04e0f644..a64042e481 100644 --- a/stepmania/src/RageUtil_CharConversions.cpp +++ b/stepmania/src/RageUtil_CharConversions.cpp @@ -52,21 +52,17 @@ static bool ConvertFromCharset( CString &txt, const char *charset ) } /* Copy the string into a char* for iconv */ - char *txtin = new char[ txt.size() + 1 ]; - strcpy( txtin, txt ); - size_t inleft = strlen( txtin ); + char *txtin = const_cast( txt.data() ); + size_t inleft = txt.size(); /* Create a new string with enough room for the new conversion */ - char *txtout = new char[ (txt.size() + 1) * 5 ]; - txtout = (char *)memset( txtout, 0, (txt.size() + 1) * 5 ); - size_t outleft = inleft * 5; - size_t outsize = outleft; + CString buf; + buf.resize( txt.size() * 5 ); + char *txtout = const_cast( buf.data() ); + size_t outleft = buf.size(); size_t size = iconv( converter, &txtin, &inleft, &txtout, &outleft ); - /* Redirect the output pointer back to the beginning of the string */ - outsize -= outleft; - txtout -= outsize; iconv_close( converter ); if( size == (size_t)(-1) ) @@ -74,10 +70,19 @@ static bool ConvertFromCharset( CString &txt, const char *charset ) LOG->Trace( "%s\n", strerror( errno ) ); return false; /* Returned an error */ } - if( !strlen(txtout) ) + + if( inleft != 0 ) + { + LOG->Warn( "iconv(UTF-8,%s) for \"%s\": whole buffer not converted (%i left)", charset, txt.c_str(), inleft ); + return false; + } + + if( buf.size() == outleft ) return false; /* Conversion failed */ - txt = txtout; + buf.resize( buf.size()-outleft ); + + txt = buf; return true; }