From 43c41ddfb1c0ae9503c66e152afc0bf8361c59df Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 2 May 2014 16:11:25 +0000 Subject: [PATCH] fixed bug in gunzip --- ChangeLog | 1 + src/gzip.cpp | 25 +++++++++--------- test/Jamfile | 1 + test/Makefile.am | 3 ++- test/test_gzip.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++ test/zeroes.gz | Bin 0 -> 538 bytes 6 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 test/test_gzip.cpp create mode 100644 test/zeroes.gz diff --git a/ChangeLog b/ChangeLog index 88c378ac2..b9f29ed0b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -46,6 +46,7 @@ * fix uTP edge case where udp socket buffer fills up * fix nagle implementation in uTP + * fixed bug in gunzip * fix to use proxy settings when adding .torrent file from URL * fix resume file issue related to daylight savings time on windows * improve error checking in lazy_bdecode diff --git a/src/gzip.cpp b/src/gzip.cpp index 83472dc6c..9fc4d6000 100644 --- a/src/gzip.cpp +++ b/src/gzip.cpp @@ -150,6 +150,9 @@ namespace libtorrent // if needed boost::uint32_t destlen = 4096; int ret = 0; + boost::uint32_t srclen = size - header_len; + in += header_len; + do { TORRENT_TRY { @@ -158,15 +161,13 @@ namespace libtorrent error = "out of memory"; return true; } - - boost::uint32_t srclen = size - header_len; - in += header_len; + ret = puff((unsigned char*)&buffer[0], &destlen, (unsigned char*)in, &srclen); - + // if the destination buffer wasn't large enough, double its // size and try again. Unless it's already at its max, in which // case we fail - if (ret == -1) + if (ret == 1) // 1: output space exhausted before completing inflate { if (destlen == maximum_size) { @@ -177,9 +178,14 @@ namespace libtorrent destlen *= 2; if (destlen > maximum_size) destlen = maximum_size; - continue; } - } while (false); + } while (ret == 1); + + if (ret != 0) + { + error = "error while inflating data"; + return true; + } if (destlen > buffer.size()) { @@ -189,11 +195,6 @@ namespace libtorrent buffer.resize(destlen); - if (ret != 0) - { - error = "error while inflating data"; - return true; - } return false; } diff --git a/test/Jamfile b/test/Jamfile index e3f075ce8..bcc153e1b 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -88,6 +88,7 @@ feature launcher : none valgrind : composite ; feature.compose valgrind : "valgrind --tool=memcheck -v --num-callers=20 --read-var-info=yes --track-origins=yes --error-exitcode=222 --suppressions=valgrind_suppressions.txt" on ; test-suite libtorrent : + [ run test_gzip.cpp ] [ run test_bitfield.cpp ] [ run test_torrent_info.cpp ] [ run test_time.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index d7013411d..04b038072 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -99,7 +99,8 @@ EXTRA_DIST = Jamfile \ cb.xml \ mn.xml \ pb.xml \ - upnp.xml + upnp.xml \ + zeroes.gz EXTRA_PROGRAMS = $(test_programs) diff --git a/test/test_gzip.cpp b/test/test_gzip.cpp new file mode 100644 index 000000000..940c552bb --- /dev/null +++ b/test/test_gzip.cpp @@ -0,0 +1,64 @@ +/* + +Copyright (c) 2014, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "libtorrent/file.hpp" +#include "test.hpp" +#include "libtorrent/gzip.hpp" +#include "setup_transfer.hpp" // for load_file +#include "file.hpp" // for combine_path + +using namespace libtorrent; + +int test_main() +{ + std::vector zipped; + error_code ec; + int r = load_file(combine_path("..", "zeroes.gz"), zipped, ec, 1000000); + if (ec) fprintf(stderr, "failed to open file: (%d) %s\n", ec.value() + , ec.message().c_str()); + TEST_CHECK(!ec); + + std::vector inflated; + std::string error; + bool ret = inflate_gzip(&zipped[0], zipped.size(), inflated, 1000000, error); + + if (ret != 0) { + fprintf(stderr, "failed to unzip\n"); + } + TEST_CHECK(ret == 0); + TEST_CHECK(inflated.size() > 0); + for (int i = 0; i < inflated.size(); ++i) + TEST_EQUAL(inflated[i], 0); + + return 0; +} + diff --git a/test/zeroes.gz b/test/zeroes.gz new file mode 100644 index 0000000000000000000000000000000000000000..9c321d2350de0edb4a6a69363b151d72ddf17a8e GIT binary patch literal 538 zcmb2|=HOTykQ~gwT$NgspIXfD_Mjmn0|UdM1z-JhdE^%WnWJC?g}^>G_T?u-7%s3g F008xW4kG{n literal 0 HcmV?d00001