Skip to content

Commit 43c41dd

Browse files
committed
fixed bug in gunzip
1 parent af39c78 commit 43c41dd

File tree

6 files changed

+81
-13
lines changed

6 files changed

+81
-13
lines changed

ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* fix uTP edge case where udp socket buffer fills up
4747
* fix nagle implementation in uTP
4848

49+
* fixed bug in gunzip
4950
* fix to use proxy settings when adding .torrent file from URL
5051
* fix resume file issue related to daylight savings time on windows
5152
* improve error checking in lazy_bdecode

src/gzip.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ namespace libtorrent
150150
// if needed
151151
boost::uint32_t destlen = 4096;
152152
int ret = 0;
153+
boost::uint32_t srclen = size - header_len;
154+
in += header_len;
155+
153156
do
154157
{
155158
TORRENT_TRY {
@@ -158,15 +161,13 @@ namespace libtorrent
158161
error = "out of memory";
159162
return true;
160163
}
161-
162-
boost::uint32_t srclen = size - header_len;
163-
in += header_len;
164+
164165
ret = puff((unsigned char*)&buffer[0], &destlen, (unsigned char*)in, &srclen);
165-
166+
166167
// if the destination buffer wasn't large enough, double its
167168
// size and try again. Unless it's already at its max, in which
168169
// case we fail
169-
if (ret == -1)
170+
if (ret == 1) // 1: output space exhausted before completing inflate
170171
{
171172
if (destlen == maximum_size)
172173
{
@@ -177,9 +178,14 @@ namespace libtorrent
177178
destlen *= 2;
178179
if (destlen > maximum_size)
179180
destlen = maximum_size;
180-
continue;
181181
}
182-
} while (false);
182+
} while (ret == 1);
183+
184+
if (ret != 0)
185+
{
186+
error = "error while inflating data";
187+
return true;
188+
}
183189

184190
if (destlen > buffer.size())
185191
{
@@ -189,11 +195,6 @@ namespace libtorrent
189195

190196
buffer.resize(destlen);
191197

192-
if (ret != 0)
193-
{
194-
error = "error while inflating data";
195-
return true;
196-
}
197198
return false;
198199
}
199200

test/Jamfile

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ feature launcher : none valgrind : composite ;
8888
feature.compose <launcher>valgrind : <testing.launcher>"valgrind --tool=memcheck -v --num-callers=20 --read-var-info=yes --track-origins=yes --error-exitcode=222 --suppressions=valgrind_suppressions.txt" <valgrind>on ;
8989

9090
test-suite libtorrent :
91+
[ run test_gzip.cpp ]
9192
[ run test_bitfield.cpp ]
9293
[ run test_torrent_info.cpp ]
9394
[ run test_time.cpp ]

test/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ EXTRA_DIST = Jamfile \
9999
cb.xml \
100100
mn.xml \
101101
pb.xml \
102-
upnp.xml
102+
upnp.xml \
103+
zeroes.gz
103104

104105
EXTRA_PROGRAMS = $(test_programs)
105106

test/test_gzip.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
3+
Copyright (c) 2014, Arvid Norberg
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions
8+
are met:
9+
10+
* Redistributions of source code must retain the above copyright
11+
notice, this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in
14+
the documentation and/or other materials provided with the distribution.
15+
* Neither the name of the author nor the names of its
16+
contributors may be used to endorse or promote products derived
17+
from this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
POSSIBILITY OF SUCH DAMAGE.
30+
31+
*/
32+
33+
#include "libtorrent/file.hpp"
34+
#include "test.hpp"
35+
#include "libtorrent/gzip.hpp"
36+
#include "setup_transfer.hpp" // for load_file
37+
#include "file.hpp" // for combine_path
38+
39+
using namespace libtorrent;
40+
41+
int test_main()
42+
{
43+
std::vector<char> zipped;
44+
error_code ec;
45+
int r = load_file(combine_path("..", "zeroes.gz"), zipped, ec, 1000000);
46+
if (ec) fprintf(stderr, "failed to open file: (%d) %s\n", ec.value()
47+
, ec.message().c_str());
48+
TEST_CHECK(!ec);
49+
50+
std::vector<char> inflated;
51+
std::string error;
52+
bool ret = inflate_gzip(&zipped[0], zipped.size(), inflated, 1000000, error);
53+
54+
if (ret != 0) {
55+
fprintf(stderr, "failed to unzip\n");
56+
}
57+
TEST_CHECK(ret == 0);
58+
TEST_CHECK(inflated.size() > 0);
59+
for (int i = 0; i < inflated.size(); ++i)
60+
TEST_EQUAL(inflated[i], 0);
61+
62+
return 0;
63+
}
64+

test/zeroes.gz

538 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)