diff --git a/src/base/bittorrent/bencoderesumedatastorage.cpp b/src/base/bittorrent/bencoderesumedatastorage.cpp index 5640bee68..b82a3d8f6 100644 --- a/src/base/bittorrent/bencoderesumedatastorage.cpp +++ b/src/base/bittorrent/bencoderesumedatastorage.cpp @@ -49,6 +49,7 @@ #include "base/utils/fs.h" #include "base/utils/io.h" #include "base/utils/string.h" +#include "common.h" #include "infohash.h" #include "loadtorrentparams.h" @@ -201,7 +202,8 @@ void BitTorrent::BencodeResumeDataStorage::loadQueue(const Path &queueFilename) BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorrentResumeData(const QByteArray &data, const QByteArray &metadata) const { lt::error_code ec; - const lt::bdecode_node resumeDataRoot = lt::bdecode(data, ec); + const lt::bdecode_node resumeDataRoot = lt::bdecode(data, ec + , nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT); if (ec) return nonstd::make_unexpected(tr("Cannot parse resume data: %1").arg(QString::fromStdString(ec.message()))); @@ -268,7 +270,8 @@ BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorre if (!metadata.isEmpty()) { - const lt::bdecode_node torentInfoRoot = lt::bdecode(metadata, ec); + const lt::bdecode_node torentInfoRoot = lt::bdecode(metadata, ec + , nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT); if (ec) return nonstd::make_unexpected(tr("Cannot parse torrent info: %1").arg(QString::fromStdString(ec.message()))); diff --git a/src/base/bittorrent/common.h b/src/base/bittorrent/common.h index 82bcd1fef..2c8fe75c7 100644 --- a/src/base/bittorrent/common.h +++ b/src/base/bittorrent/common.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2020 Vladimir Golovnev + * Copyright (C) 2020-2023 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -33,3 +33,7 @@ #include "base/global.h" inline const QString QB_EXT = u".!qB"_s; + +inline const int MAX_TORRENT_SIZE = 100 * 1024 * 1024; // 100 MiB +inline const int BENCODE_DEPTH_LIMIT = 100; +inline const int BENCODE_TOKEN_LIMIT = 10'000'000; diff --git a/src/base/bittorrent/dbresumedatastorage.cpp b/src/base/bittorrent/dbresumedatastorage.cpp index b24ae5111..86ffac9fd 100644 --- a/src/base/bittorrent/dbresumedatastorage.cpp +++ b/src/base/bittorrent/dbresumedatastorage.cpp @@ -58,6 +58,7 @@ #include "base/profile.h" #include "base/utils/fs.h" #include "base/utils/string.h" +#include "common.h" #include "infohash.h" #include "loadtorrentparams.h" @@ -247,7 +248,8 @@ namespace const QByteArray bencodedResumeData = query.value(DB_COLUMN_RESUMEDATA.name).toByteArray(); lt::error_code ec; - const lt::bdecode_node resumeDataRoot = lt::bdecode(bencodedResumeData, ec); + const lt::bdecode_node resumeDataRoot = lt::bdecode(bencodedResumeData, ec + , nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT); lt::add_torrent_params &p = resumeData.ltAddTorrentParams; @@ -256,7 +258,8 @@ namespace if (const QByteArray bencodedMetadata = query.value(DB_COLUMN_METADATA.name).toByteArray() ; !bencodedMetadata.isEmpty()) { - const lt::bdecode_node torentInfoRoot = lt::bdecode(bencodedMetadata, ec); + const lt::bdecode_node torentInfoRoot = lt::bdecode(bencodedMetadata, ec + , nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT); p.ti = std::make_shared(torentInfoRoot, ec); } diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index 42d34e688..cefc7d770 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2015-2023 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -45,6 +45,7 @@ #include "base/utils/fs.h" #include "base/utils/io.h" #include "base/utils/misc.h" +#include "common.h" #include "infohash.h" #include "trackerentry.h" @@ -85,12 +86,9 @@ nonstd::expected TorrentInfo::load(const QByteArray &data) { // 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; - lt::error_code ec; const lt::bdecode_node node = lt::bdecode(data, ec - , nullptr, depthLimit, tokenLimit); + , nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT); if (ec) return nonstd::make_unexpected(QString::fromStdString(ec.message())); diff --git a/src/base/global.h b/src/base/global.h index d1fe7120e..fde7984be 100644 --- a/src/base/global.h +++ b/src/base/global.h @@ -35,8 +35,6 @@ #define QBT_APP_64BIT #endif -inline const int MAX_TORRENT_SIZE = 100 * 1024 * 1024; // 100 MiB - template constexpr typename std::add_const_t &asConst(T &t) noexcept { return t; } diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 66c82b055..e91c84177 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -43,6 +43,7 @@ #include #include +#include "base/bittorrent/common.h" #include "base/bittorrent/downloadpriority.h" #include "base/bittorrent/infohash.h" #include "base/bittorrent/magneturi.h"