Fix memory leaks: do not allocate results on heap

This commit is contained in:
Vladimir Kolesnikov 2013-02-10 15:51:58 +02:00
parent 8967ae4aad
commit 8156bc0a95

View File

@ -20,7 +20,7 @@
QString QuotedPrintable::encode(const QByteArray &input) QString QuotedPrintable::encode(const QByteArray &input)
{ {
QString *output = new QString(); QString output;
char byte; char byte;
const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
@ -30,16 +30,14 @@ QString QuotedPrintable::encode(const QByteArray &input)
byte = input[i]; byte = input[i];
if ((byte == 0x20) || (byte >= 33) && (byte <= 126) && (byte != 61)) { if ((byte == 0x20) || (byte >= 33) && (byte <= 126) && (byte != 61)) {
output->append(byte); output.append(byte);
} }
else { else {
output->append('='); output.append('=').append(hex[((byte >> 4) & 0x0F)]).append(hex[(byte & 0x0F)]);
output->append(hex[((byte >> 4) & 0x0F)]);
output->append(hex[(byte & 0x0F)]);
} }
} }
return *output; return output;
} }
@ -48,19 +46,19 @@ QByteArray QuotedPrintable::decode(const QString &input)
// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F // 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F
const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15}; const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15};
QByteArray *output = new QByteArray(); QByteArray output;
for (int i = 0; i < input.length(); ++i) for (int i = 0; i < input.length(); ++i)
{ {
if (input.at(i).toAscii() == '=') if (input.at(i).toLatin1() == '=')
{ {
output->append((hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0']); output.append((hexVal[input.at(++i).toLatin1() - '0'] << 4) + hexVal[input.at(++i).toLatin1() - '0']);
} }
else else
{ {
output->append(input.at(i).toAscii()); output.append(input.at(i).toLatin1());
} }
} }
return *output; return output;
} }