From 6663f527da072439b9f8ed82311709bf3a182626 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 19 Jan 2011 10:07:51 +0000 Subject: [PATCH] fixed error handling in torrent_info constructor --- ChangeLog | 1 + examples/client_test.cpp | 6 +++--- examples/dump_torrent.cpp | 6 +++--- examples/rss_reader.cpp | 4 ++-- include/libtorrent/torrent_info.hpp | 2 +- src/disk_io_thread.cpp | 1 + src/session.cpp | 1 + src/torrent_info.cpp | 26 +++++++++++++++----------- test/setup_transfer.cpp | 7 ++++--- test/test_web_seed.cpp | 3 ++- 10 files changed, 33 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 698b22718..6733673e4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -72,6 +72,7 @@ incoming connection * added more detailed instrumentation of the disk I/O thread + * fixed error handling in torrent_info constructor * fixed bug in torrent_info::remap_files * fix python binding for wait_for_alert * only apply privileged port filter to DHT-only peers diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 525339e68..3e6499934 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -611,7 +611,7 @@ void add_torrent(libtorrent::session& ses std::string filename = combine_path(save_path, t->name() + ".resume"); std::vector buf; - if (load_file(filename.c_str(), buf) == 0) + if (load_file(filename.c_str(), buf, ec) == 0) p.resume_data = &buf; p.ti = t; @@ -889,10 +889,10 @@ int main(int argc, char* argv[]) + alert::stats_notification)); std::vector in; - if (load_file(".ses_state", in) == 0) + error_code ec; + if (load_file(".ses_state", in, ec) == 0) { lazy_entry e; - error_code ec; if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0) ses.load_state(e); } diff --git a/examples/dump_torrent.cpp b/examples/dump_torrent.cpp index 8f7c0b8ce..6f4ac0909 100644 --- a/examples/dump_torrent.cpp +++ b/examples/dump_torrent.cpp @@ -59,14 +59,14 @@ int main(int argc, char* argv[]) return 1; } std::vector buf(size); - int ret = load_file(argv[1], buf, 40 * 1000000); + error_code ec; + int ret = load_file(argv[1], buf, ec, 40 * 1000000); if (ret != 0) { - fprintf(stderr, "failed to load file: %d\n", ret); + fprintf(stderr, "failed to load file: %s\n", ec.message().c_str()); return 1; } lazy_entry e; - error_code ec; int pos; printf("decoding. recursion limit: %d total item count limit: %d\n" , depth_limit, item_limit); diff --git a/examples/rss_reader.cpp b/examples/rss_reader.cpp index ac1bd1157..30ed70946 100644 --- a/examples/rss_reader.cpp +++ b/examples/rss_reader.cpp @@ -78,10 +78,10 @@ int main(int argc, char* argv[]) ses.set_settings(sett); std::vector in; - if (load_file(".ses_state", in) == 0) + error_code ec; + if (load_file(".ses_state", in, ec) == 0) { lazy_entry e; - error_code ec; if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0) ses.load_state(e); } diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index de0ccd31a..7930fccb9 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -222,7 +222,7 @@ namespace libtorrent #endif int TORRENT_EXPORT load_file(std::string const& filename - , std::vector& v, int limit = 8000000); + , std::vector& v, error_code& ec, int limit = 8000000); class TORRENT_EXPORT torrent_info : public intrusive_ptr_base { diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index a2ed35dff..934274b64 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -2305,6 +2305,7 @@ namespace libtorrent lazy_entry const* rd = (lazy_entry const*)j.buffer; TORRENT_ASSERT(rd != 0); ret = j.storage->check_fastresume(*rd, j.error); + test_error(j); break; } case disk_io_job::check_files: diff --git a/src/session.cpp b/src/session.cpp index 851187e61..414a15251 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -517,6 +517,7 @@ namespace libtorrent torrent_handle session::add_torrent(add_torrent_params const& params, error_code& ec) { + ec.clear(); if (string_begins_no_case("magnet:", params.url.c_str())) { add_torrent_params p(params); diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index de7c27db7..3148fd7aa 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -401,14 +401,18 @@ namespace libtorrent return 1 << i; } - int load_file(std::string const& filename, std::vector& v, int limit) + int load_file(std::string const& filename, std::vector& v, error_code& ec, int limit) { + ec.clear(); file f; - error_code ec; if (!f.open(filename, file::read_only, ec)) return -1; size_type s = f.get_size(ec); if (ec) return -1; - if (s > limit) return -2; + if (s > limit) + { + ec = error_code(errors::metadata_too_large, get_libtorrent_category()); + return -2; + } v.resize(s); if (s == 0) return 0; file::iovec_t b = {&v[0], s}; @@ -576,11 +580,11 @@ namespace libtorrent , m_i2p(false) { std::vector buf; - int ret = load_file(filename, buf); - if (ret < 0) return; + error_code ec; + int ret = load_file(filename, buf, ec); + if (ret < 0) throw invalid_torrent_file(ec); lazy_entry e; - error_code ec; if (buf.size() == 0 || lazy_bdecode(&buf[0], &buf[0] + buf.size(), e, ec) != 0) throw invalid_torrent_file(ec); @@ -601,11 +605,11 @@ namespace libtorrent std::vector buf; std::string utf8; wchar_utf8(filename, utf8); - int ret = load_file(utf8, buf); - if (ret < 0) return; + error_code ec; + int ret = load_file(utf8, buf, ec); + if (ret < 0) throw invalid_torrent_file(ec); lazy_entry e; - error_code ec; if (buf.size() == 0 || lazy_bdecode(&buf[0], &buf[0] + buf.size(), e, ec) != 0) throw invalid_torrent_file(ec); @@ -650,7 +654,7 @@ namespace libtorrent , m_i2p(false) { std::vector buf; - int ret = load_file(filename, buf); + int ret = load_file(filename, buf, ec); if (ret < 0) return; lazy_entry e; @@ -671,7 +675,7 @@ namespace libtorrent std::vector buf; std::string utf8; wchar_utf8(filename, utf8); - int ret = load_file(utf8, buf); + int ret = load_file(utf8, buf, ec); if (ret < 0) return; lazy_entry e; diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 622450731..712d242cc 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -907,9 +907,9 @@ void web_server_thread(int* port, bool ssl, bool chunked) int size = range_end - range_start + 1; boost::uint64_t off = idx * 64 * 1024 + range_start; std::vector file_buf; - int res = load_file("./tmp1_web_seed/seed", file_buf); - error_code ec; + int res = load_file("./tmp1_web_seed/seed", file_buf, ec); + if (res == -1 || file_buf.empty()) { send_response(s, ec, 404, "Not Found", extra_header, 0); @@ -931,7 +931,8 @@ void web_server_thread(int* port, bool ssl, bool chunked) std::vector file_buf; // remove the / from the path path = path.substr(1); - int res = load_file(path, file_buf); + error_code ec; + int res = load_file(path, file_buf, ec); if (res == -1) { send_response(s, ec, 404, "Not Found", extra_header, 0); diff --git a/test/test_web_seed.cpp b/test/test_web_seed.cpp index 6e776720e..b33df2543 100644 --- a/test/test_web_seed.cpp +++ b/test/test_web_seed.cpp @@ -180,7 +180,8 @@ void save_file(char const* filename, char const* data, int size) sha1_hash file_hash(std::string const& name) { std::vector buf; - load_file(name, buf); + error_code ec; + load_file(name, buf, ec); if (buf.empty()) return sha1_hash(0); hasher h(&buf[0], buf.size()); return h.final();