Skip to content

Commit

Permalink
[Torrent] Fix bug when import ill-formed torrent
Browse files Browse the repository at this point in the history
  • Loading branch information
setvisible committed Dec 31, 2023
1 parent 3404667 commit 990c1cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
6 changes: 5 additions & 1 deletion src/core/torrentcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ void TorrentContext::setEnabled(bool enabled)
******************************************************************************/
void TorrentContext::prepareTorrent(Torrent *torrent)
{
d->prepareTorrent(torrent);
try {
d->prepareTorrent(torrent);
} catch (std::exception const& e) {
qWarning() << "Caugth exception in " << Q_FUNC_INFO << ": " << QString::fromUtf8(e.what());
}
}

void TorrentContext::stopPrepare(Torrent *torrent)
Expand Down
45 changes: 11 additions & 34 deletions src/core/torrentcontext_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,13 @@ void TorrentContextPrivate::archiveExistingFile(const QString &filename)
{
if (QFileInfo::exists(filename)) {
auto archive = QString("%0.0.old").arg(filename);
auto i = 0;
int i = 0;
while (QFileInfo::exists(archive)) {
i++;
archive = QString("%0.%1.old").arg(filename, QString::number(i));
}
archive.append(QString::number(i));
if (!QFile::rename(filename, archive)) {
qDebug_1 << "Cannot archive file" << filename << "to" << archive;
qWarning() << "Couldn't rename file '" << filename << "' into '" << archive << "'.'";
}
}
}
Expand All @@ -607,9 +606,9 @@ void TorrentContextPrivate::writeTorrentFileFromMagnet(const QString &filename,

// Bittorrent Encoding
lt::create_torrent ct(*ti);
auto te = ct.generate();
auto entry = ct.generate();
std::vector<char> buffer;
lt::bencode(std::back_inserter(buffer), te);
lt::bencode(std::back_inserter(buffer), entry);

// Write
QByteArray data(&buffer[0], static_cast<int>(buffer.size()));
Expand Down Expand Up @@ -650,7 +649,7 @@ void TorrentContextPrivate::readTorrentFile(const QString &filename, Torrent *to
// Do a fake 'detail' update, to initialize the torrent.
auto detail = torrent->detail();
detail.files.clear();
for (int index = 0; index < metaInfo.initialMetaInfo.files.count(); ++index) {
for (auto index = 0; index < metaInfo.initialMetaInfo.files.count(); ++index) {
TorrentFileInfo fi;
fi.priority = TorrentFileInfo::Normal;
detail.files.append(fi);
Expand Down Expand Up @@ -1126,39 +1125,17 @@ void WorkerThread::setSettings(lt::settings_pack &pack)

/******************************************************************************
******************************************************************************/
static std::vector<char> load_file(std::string const& filename)
{
std::fstream in;
in.exceptions(std::ifstream::failbit);
in.open(filename.c_str(), std::ios_base::in | std::ios_base::binary);
in.seekg(0, std::ios_base::end);
auto size = static_cast<qsizetype>(in.tellg());
in.seekg(0, std::ios_base::beg);
std::vector<char> ret(size);
in.read(ret.data(), static_cast<std::streamsize>(size));
return ret;
}

TorrentInitialMetaInfo WorkerThread::dump(const QString &filename) const
{
auto buf = load_file(filename.toStdString());
lt::error_code ec;
int pos = -1;
lt::load_torrent_limits cfg;
//lt::bdecode_node const e = lt::bdecode(
auto e = lt::bdecode(
buf, ec, &pos,
cfg.max_decode_depth,
cfg.max_decode_tokens);
if (ec) {
qDebug_2 << "failed to decode: '"
<< QString::fromStdString(ec.message())
<< "' at character: " << pos;
lt::error_code error_code;
const lt::torrent_info torrent_info(filename.toStdString(), error_code);
if (error_code) {
qWarning() << "failed to decode: '" << QString::fromStdString(error_code.message());
return {};
}
lt::torrent_info const t(std::move(e), cfg);
std::shared_ptr<lt::torrent_info> ti = std::make_shared<lt::torrent_info>(t);
return toTorrentInitialMetaInfo(ti);
auto ptr_torrent_info= std::make_shared<lt::torrent_info>(torrent_info);
return toTorrentInitialMetaInfo(ptr_torrent_info);
}

/******************************************************************************
Expand Down

0 comments on commit 990c1cd

Please sign in to comment.