Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading very large torrents. Closes #8449 #8455

Merged
merged 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions src/base/bittorrent/torrentinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,51 @@ TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept

TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
{
if (error)
error->clear();

QFile file {path};
if (!file.open(QIODevice::ReadOnly)) {
if (error)
*error = file.errorString();
return TorrentInfo();
}

const qint64 fileSizeLimit = 100 * 1024 * 1024; // 100 MB
if (file.size() > fileSizeLimit) {
if (error)
*error = tr("File size exceeds max limit %1").arg(fileSizeLimit);
return TorrentInfo();
}

const QByteArray data = file.read(fileSizeLimit);
if (data.size() != file.size()) {
if (error)
*error = tr("Torrent file read error");
return TorrentInfo();
}

file.close();

// 2-step construction to overcome default limits of `depth_limit` & `token_limit` which are
// used in `torrent_info()` constructor
const int depthLimit = 100;
const int tokenLimit = 10000000;
libt::bdecode_node node;
libt::error_code ec;
TorrentInfo info(NativePtr(new libt::torrent_info(Utils::Fs::toNativePath(path).toStdString(), ec)));
if (error) {
if (ec)
bdecode(data.constData(), (data.constData() + data.size()), node, ec
, nullptr, depthLimit, tokenLimit);
if (ec) {
if (error)
*error = QString::fromStdString(ec.message());
else
error->clear();
return TorrentInfo();
}

TorrentInfo info {NativePtr(new libt::torrent_info(node, ec))};
if (ec) {
if (error)
*error = QString::fromStdString(ec.message());
return TorrentInfo();
}

return info;
Expand Down
3 changes: 3 additions & 0 deletions src/base/bittorrent/torrentinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#ifndef BITTORRENT_TORRENTINFO_H
#define BITTORRENT_TORRENTINFO_H

#include <QCoreApplication>
#include <QtGlobal>

#include <libtorrent/torrent_info.hpp>
Expand All @@ -51,6 +52,8 @@ namespace BitTorrent

class TorrentInfo
{
Q_DECLARE_TR_FUNCTIONS("TorrentInfo")

public:
#if LIBTORRENT_VERSION_NUM < 10100
typedef boost::intrusive_ptr<const libtorrent::torrent_info> NativeConstPtr;
Expand Down