Skip to content

Commit a2712d2

Browse files
committed
Lint code. Remove bad allocation hidden errors
1 parent ea491c2 commit a2712d2

5 files changed

Lines changed: 19 additions & 107 deletions

File tree

lzs/LZS.cpp

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -32,84 +32,14 @@ qint32 LZS::dad[4097];
3232
unsigned char LZS::text_buf[4113];
3333
QByteArray LZS::result;
3434

35-
const QByteArray &LZS::decompress(const QByteArray &data, int max, LZSObserver *observer)
36-
{
37-
return decompress(data.constData(), data.size(), max, observer);
38-
}
39-
40-
const QByteArray &LZS::decompress(const char *data, int fileSize, int max, LZSObserver *observer)
41-
{
42-
int curResult = 0, sizeAlloc = max + 10;
43-
quint16 curBuff = 4078, offset, flagByte = 0, i, length;
44-
const quint8 *fileData = (const quint8 *)data,
45-
*endFileData = fileData + fileSize;
46-
47-
if (observer) {
48-
observer->setMaximum(fileSize);
49-
}
50-
51-
// Impossible case
52-
if(quint64(sizeAlloc) > 2000 * quint64(fileSize)) {
53-
qWarning() << "LZS::decompress impossible ratio case" << sizeAlloc << 2000 * quint64(fileSize);
54-
result.clear();
55-
return result;
56-
}
57-
58-
// Reduce the amount of realloc
59-
if (result.size() < sizeAlloc) {
60-
try {
61-
result.resize(sizeAlloc);
62-
} catch(std::bad_alloc) {
63-
result.clear();
64-
return result;
65-
}
66-
}
67-
68-
memset(text_buf, 0, 4078); // The buffer of 4096 bytes is set to 0
69-
70-
forever {
71-
if (((flagByte >>= 1) & 256) == 0) {
72-
flagByte = *fileData++ | 0xff00; // First byte
73-
}
74-
75-
if (observer) { // Output
76-
observer->setValue(fileData - (const quint8 *)data);
77-
}
78-
79-
if (fileData >= endFileData || curResult >= max) {
80-
result.truncate(curResult);
81-
return result; // Ending
82-
}
83-
84-
if (flagByte & 1) {
85-
// Uncompressed byte
86-
result[curResult] = text_buf[curBuff] = *fileData++;
87-
curBuff = (curBuff + 1) & 4095;
88-
++curResult;
89-
} else {
90-
// Infos to retrieve uncompressed data
91-
offset = *fileData++;
92-
length = *fileData++;
93-
offset |= (length & 0xF0) << 4;
94-
length = (length & 0xF) + 2 + offset;
95-
96-
for (i = offset; i <= length; ++i) {
97-
result[curResult] = text_buf[curBuff] = text_buf[i & 4095];
98-
curBuff = (curBuff + 1) & 4095;
99-
++curResult;
100-
}
101-
}
102-
}
103-
}
104-
10535
const QByteArray &LZS::decompressAll(const QByteArray &data, LZSObserver *observer)
10636
{
10737
return decompressAll(data.constData(), data.size(), observer);
10838
}
10939

11040
const QByteArray &LZS::decompressAll(const char *data, int fileSize, LZSObserver *observer)
11141
{
112-
int curResult = 0, sizeAlloc = fileSize * 5;
42+
int curResult = 0, sizeAlloc = int(qMin(quint64(fileSize * 5), quint64(INT_MAX)));
11343
quint16 curBuff = 4078, offset, flagByte = 0, i, length;
11444
const quint8 *fileData = (const quint8 *)data,
11545
*endFileData = fileData + fileSize;
@@ -118,19 +48,12 @@ const QByteArray &LZS::decompressAll(const char *data, int fileSize, LZSObserver
11848
observer->setMaximum(fileSize);
11949
}
12050

121-
// Impossible case
122-
if(sizeAlloc > 2000 * fileSize) {
123-
result.clear();
124-
return result;
125-
}
126-
12751
// Reduce the amount of realloc
12852
if (result.size() < sizeAlloc) {
12953
try {
13054
result.resize(sizeAlloc);
13155
} catch(std::bad_alloc) {
132-
result.clear();
133-
return result;
56+
// Ignore, try anyway
13457
}
13558
}
13659

lzs/LZS.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@
3131
class LZS
3232
{
3333
public:
34-
static const QByteArray &decompress(const QByteArray &fileData, int max, LZSObserver *observer = NULL);
35-
static const QByteArray &decompress(const char *data, int fileSize, int max, LZSObserver *observer = NULL);
36-
static const QByteArray &decompressAll(const QByteArray &fileData, LZSObserver *observer = NULL);
37-
static const QByteArray &decompressAll(const char *data, int fileSize, LZSObserver *observer = NULL);
38-
static const QByteArray &compress(const QByteArray &fileData, LZSObserver *observer = NULL);
39-
static const QByteArray &compress(const char *data, int sizeData, LZSObserver *observer = NULL);
34+
static const QByteArray &decompressAll(const QByteArray &fileData, LZSObserver *observer = nullptr);
35+
static const QByteArray &decompressAll(const char *data, int fileSize, LZSObserver *observer = nullptr);
36+
static const QByteArray &compress(const QByteArray &fileData, LZSObserver *observer = nullptr);
37+
static const QByteArray &compress(const char *data, int sizeData, LZSObserver *observer = nullptr);
4038

4139
private:
4240
static void InsertNode(qint32 r);

lzs/LZSObserver.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,15 @@
1717
#include "LZSObserver.h"
1818
#include <stdio.h>
1919

20-
LZSObserver::LZSObserver()
21-
{
22-
}
23-
24-
LZSObserverPercent::LZSObserverPercent()
25-
{
26-
}
27-
2820
void LZSObserverPercent::setValue(int value)
2921
{
30-
int percent = value * 100.0 / (double)maximum();
22+
int percent = int(qint64(value * 100) / maximum());
3123
if (percent != _lastPercent) {
3224
_lastPercent = percent;
3325
setPercent(percent);
3426
}
3527
}
3628

37-
LZSObserverStdOut::LZSObserverStdOut()
38-
{
39-
}
40-
4129
void LZSObserverStdOut::setPercent(int percent)
4230
{
4331
printf("[%d%%] %s\r", percent, qPrintable(_filename));

lzs/LZSObserver.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
class LZSObserver
2323
{
2424
public:
25-
LZSObserver();
25+
inline LZSObserver() {}
26+
inline virtual ~LZSObserver() {}
2627

2728
inline void setMaximum(int maximum) {
2829
_maximum = maximum;
@@ -32,29 +33,31 @@ class LZSObserver
3233
return _maximum;
3334
}
3435

35-
virtual void setValue(int value)=0;
36+
virtual void setValue(int value) = 0;
3637
private:
3738
int _maximum;
3839
};
3940

4041
class LZSObserverPercent : public LZSObserver
4142
{
4243
public:
43-
LZSObserverPercent();
44-
void setValue(int value);
45-
virtual void setPercent(int percent)=0;
44+
inline LZSObserverPercent() {}
45+
inline virtual ~LZSObserverPercent() override {}
46+
virtual void setValue(int value) override;
47+
virtual void setPercent(int percent) = 0;
4648
private:
4749
int _lastPercent;
4850
};
4951

5052
class LZSObserverStdOut : public LZSObserverPercent
5153
{
5254
public:
53-
LZSObserverStdOut();
55+
inline LZSObserverStdOut() {}
56+
inline virtual ~LZSObserverStdOut() override {}
5457
inline void setFilename(const QString &filename) {
5558
_filename = filename;
5659
}
57-
virtual void setPercent(int percent);
60+
virtual void setPercent(int percent) override;
5861
private:
5962
QString _filename;
6063
};

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ int main(int argc, char *argv[])
2626
#endif
2727

2828
Arguments args;
29-
quint32 lzsSize;
29+
qint32 lzsSize = 0;
3030
LZSObserver *observer;
3131
LZSObserverStdOut stdObserver;
3232

3333
if (args.quiet()) {
34-
observer = NULL;
34+
observer = nullptr;
3535
} else {
3636
observer = &stdObserver;
3737
}

0 commit comments

Comments
 (0)