diff options
author | Nick Ratelle <nratelle@qnx.com> | 2012-01-05 17:13:46 -0500 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-08 07:28:21 +0100 |
commit | 4560ea230f4055d1be480f862a275639d661b339 (patch) | |
tree | 05cecdd3109e3aeefe9b31ed9663612105ba50f8 /src | |
parent | 8854b74cf4511c758b998abfc269cc1f3a11d4a1 (diff) |
Fixes potential memory leak in qtextcodec.cpp
Change-Id: I56c0a1a6cc261bd15653bc4cbb94daed1f8aa811
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/codecs/qtextcodec.cpp | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/src/corelib/codecs/qtextcodec.cpp b/src/corelib/codecs/qtextcodec.cpp index 13f0ec8ce5..c031a43985 100644 --- a/src/corelib/codecs/qtextcodec.cpp +++ b/src/corelib/codecs/qtextcodec.cpp @@ -48,6 +48,7 @@ #include "qlist.h" #include "qfile.h" #include "qstringlist.h" +#include "qvarlengtharray.h" #ifdef Q_OS_UNIX # include "qiconvcodec_p.h" @@ -220,10 +221,7 @@ QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, Conv if (!mb || !mblen) return QString(); - const int wclen_auto = 4096; - wchar_t wc_auto[wclen_auto]; - int wclen = wclen_auto; - wchar_t *wc = wc_auto; + QVarLengthArray<wchar_t, 4096> wc(4096); int len; QString sp; bool prepend = false; @@ -243,7 +241,7 @@ QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, Conv prev[1] = mb[0]; remainingChars = 0; len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, - prev, 2, wc, wclen); + prev, 2, wc.data(), wc.length()); if (len) { prepend = true; sp.append(QChar(wc[0])); @@ -254,18 +252,12 @@ QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, Conv } while (!(len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, - mb, mblen, wc, wclen))) { + mb, mblen, wc.data(), wc.length()))) { int r = GetLastError(); if (r == ERROR_INSUFFICIENT_BUFFER) { - if (wc != wc_auto) { - qWarning("MultiByteToWideChar: Size changed"); - break; - } else { - wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, + const int wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mb, mblen, 0, 0); - wc = new wchar_t[wclen]; - // and try again... - } + wc.resize(wclen); } else if (r == ERROR_NO_UNICODE_TRANSLATION) { //find the last non NULL character while (mblen > 1 && !(mb[mblen-1])) @@ -283,8 +275,10 @@ QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, Conv break; } } + if (len <= 0) return QString(); + if (wc[len-1] == 0) // len - 1: we don't want terminator --len; @@ -293,9 +287,7 @@ QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, Conv state->state_data[0] = (char)state_data; state->remainingChars = remainingChars; } - QString s((QChar*)wc, len); - if (wc != wc_auto) - delete [] wc; + QString s((QChar*)wc.data(), len); if (prepend) { return sp+s; } |