Skip to content

Commit 7d1ac06

Browse files
committed
Fix stack overflow in Utils::Gzip::decompress
Anyway, use std::vector to allocate memory on the heap (in compress() too)
1 parent 67f44e0 commit 7d1ac06

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/base/utils/gzip.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "gzip.h"
3131

32+
#include <vector>
33+
3234
#include <QByteArray>
3335

3436
#ifndef ZLIB_CONST
@@ -44,15 +46,15 @@ QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *
4446
return {};
4547

4648
const int BUFSIZE = 128 * 1024;
47-
char tmpBuf[BUFSIZE] = {0};
49+
std::vector<char> tmpBuf(BUFSIZE);
4850

4951
z_stream strm;
5052
strm.zalloc = Z_NULL;
5153
strm.zfree = Z_NULL;
5254
strm.opaque = Z_NULL;
5355
strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
5456
strm.avail_in = uInt(data.size());
55-
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
57+
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
5658
strm.avail_out = BUFSIZE;
5759

5860
// windowBits = 15 + 16 to enable gzip
@@ -74,17 +76,17 @@ QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *
7476
return {};
7577
}
7678

77-
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
78-
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
79+
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
80+
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
7981
strm.avail_out = BUFSIZE;
8082
}
8183

8284
// flush the rest from deflate
8385
while (result != Z_STREAM_END) {
8486
result = deflate(&strm, Z_FINISH);
8587

86-
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
87-
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
88+
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
89+
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
8890
strm.avail_out = BUFSIZE;
8991
}
9092

@@ -102,15 +104,15 @@ QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok)
102104
return {};
103105

104106
const int BUFSIZE = 1024 * 1024;
105-
char tmpBuf[BUFSIZE] = {0};
107+
std::vector<char> tmpBuf(BUFSIZE);
106108

107109
z_stream strm;
108110
strm.zalloc = Z_NULL;
109111
strm.zfree = Z_NULL;
110112
strm.opaque = Z_NULL;
111113
strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
112114
strm.avail_in = uInt(data.size());
113-
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
115+
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
114116
strm.avail_out = BUFSIZE;
115117

116118
// windowBits must be greater than or equal to the windowBits value provided to deflateInit2() while compressing
@@ -128,7 +130,7 @@ QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok)
128130
result = inflate(&strm, Z_NO_FLUSH);
129131

130132
if (result == Z_STREAM_END) {
131-
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
133+
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
132134
break;
133135
}
134136

@@ -137,8 +139,8 @@ QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok)
137139
return {};
138140
}
139141

140-
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
141-
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
142+
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
143+
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
142144
strm.avail_out = BUFSIZE;
143145
}
144146

0 commit comments

Comments
 (0)