Don't read unlimited data from files

It now guards against reading infinite files such as `/dev/zero`.
And most readings are bound with a (lax) limit.
As a side effect, more checking are done when reading a file and
overall the reading procedure is more robust.

PR #19095.
This commit is contained in:
Chocobo1
2023-06-14 13:38:19 +08:00
committed by GitHub
parent 81bc910d68
commit 79ca2e145f
24 changed files with 370 additions and 199 deletions

View File

@@ -33,6 +33,8 @@
#include <libtorrent/fwd.hpp>
#include <QIODevice>
#include "base/3rdparty/expected.hpp"
#include "base/pathfwd.h"
@@ -81,6 +83,23 @@ namespace Utils::IO
int m_bufferSize = 0;
};
struct ReadError
{
enum Code
{
NotExist,
ExceedSize,
SizeMismatch
};
Code status = {};
QString message;
};
// TODO: define a specific type for `additionalMode`
// providing `size` is explicit and is strongly recommended
nonstd::expected<QByteArray, ReadError> readFile(const Path &path, qint64 size, QIODevice::OpenMode additionalMode = {});
nonstd::expected<void, QString> saveToFile(const Path &path, const QByteArray &data);
nonstd::expected<void, QString> saveToFile(const Path &path, const lt::entry &data);
}