summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Ratelle <nratelle@qnx.com>2012-01-05 17:13:46 -0500
committerQt by Nokia <qt-info@nokia.com>2012-03-14 15:06:29 +0100
commitea18c7d6d260804465f1d6a5cc464f7cc9c860a4 (patch)
tree4f5658c204918ef0a1c73dbefb30582059a7a778
parentd15f4b53d0d52a94b3c44b927816967a151f7ccf (diff)
Fixes potential memory leak in qtextcodec.cpp
cherry-picked from qt5/qtbase 4560ea230f40 Reviewed-by: Olivier Goffart <ogoffart@woboq.com> Change-Id: I06fc265fcc34bcc671c44b7041c0c461e588d193 Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
-rw-r--r--src/corelib/codecs/qtextcodec.cpp26
1 files changed, 9 insertions, 17 deletions
diff --git a/src/corelib/codecs/qtextcodec.cpp b/src/corelib/codecs/qtextcodec.cpp
index 4114495479..ea8a5fd75d 100644
--- a/src/corelib/codecs/qtextcodec.cpp
+++ b/src/corelib/codecs/qtextcodec.cpp
@@ -47,6 +47,7 @@
#include "qlist.h"
#include "qfile.h"
+#include "qvarlengtharray.h"
#ifndef QT_NO_LIBRARY
# include "qcoreapplication.h"
# include "qtextcodecplugin.h"
@@ -274,10 +275,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;
@@ -297,7 +295,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.size());
if (len) {
prepend = true;
sp.append(QChar(wc[0]));
@@ -308,18 +306,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.size()))) {
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]))
@@ -337,8 +329,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;
@@ -347,9 +341,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;
}