Always check input file_id.

GitOrigin-RevId: aa33312b3961d6cf724f43418084c5a9133d7ad7
This commit is contained in:
levlam 2018-01-05 17:24:41 +03:00
parent bd145f18ac
commit 149c3fdc46
2 changed files with 39 additions and 30 deletions

View File

@ -1781,8 +1781,12 @@ tl_object_ptr<td_api::file> FileManager::get_file_object(FileId file_id, bool wi
file_view.has_remote_location(), remote_size)); file_view.has_remote_location(), remote_size));
} }
Result<FileId> FileManager::check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted) { Result<FileId> FileManager::check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted,
bool allow_zero) {
TRY_RESULT(file_id, std::move(result)); TRY_RESULT(file_id, std::move(result));
if (allow_zero && !file_id.is_valid()) {
return FileId();
}
int32 file_node_id; int32 file_node_id;
auto *file_node = get_file_node(file_id, &file_node_id); auto *file_node = get_file_node(file_id, &file_node_id);
@ -1852,38 +1856,42 @@ Result<FileId> FileManager::get_input_file_id(FileType type, const tl_object_ptr
return Status::Error(6, "InputFile not specified"); return Status::Error(6, "InputFile not specified");
} }
switch (file->get_id()) { auto r_file_id = [&]() -> Result<FileId> {
case td_api::inputFileLocal::ID: { switch (file->get_id()) {
const string &path = static_cast<const td_api::inputFileLocal *>(file.get())->path_; case td_api::inputFileLocal::ID: {
if (allow_zero && path.empty()) { const string &path = static_cast<const td_api::inputFileLocal *>(file.get())->path_;
return FileId(); if (allow_zero && path.empty()) {
return FileId();
}
return register_local(FullLocalFileLocation(is_encrypted ? FileType::Encrypted : type, path, 0),
owner_dialog_id, 0, get_by_hash);
} }
return register_local(FullLocalFileLocation(is_encrypted ? FileType::Encrypted : type, path, 0), owner_dialog_id, case td_api::inputFileId::ID: {
0, get_by_hash); FileId file_id(static_cast<const td_api::inputFileId *>(file.get())->id_);
} if (!file_id.is_valid()) {
case td_api::inputFileId::ID: { return FileId();
FileId file_id(static_cast<const td_api::inputFileId *>(file.get())->id_); }
if (allow_zero && !file_id.is_valid()) { return file_id;
return FileId();
} }
return check_input_file_id(type, file_id, is_encrypted); case td_api::inputFileRemote::ID: {
} const string &file_persistent_id = static_cast<const td_api::inputFileRemote *>(file.get())->id_;
case td_api::inputFileRemote::ID: { if (allow_zero && file_persistent_id.empty()) {
const string &file_persistent_id = static_cast<const td_api::inputFileRemote *>(file.get())->id_; return FileId();
if (allow_zero && file_persistent_id.empty()) { }
return FileId(); return from_persistent_id(file_persistent_id, type);
} }
return check_input_file_id(type, from_persistent_id(file_persistent_id, type), is_encrypted); case td_api::inputFileGenerated::ID: {
auto *generated_file = static_cast<const td_api::inputFileGenerated *>(file.get());
return register_generate(is_encrypted ? FileType::Encrypted : type, generated_file->original_path_,
generated_file->conversion_, owner_dialog_id, generated_file->expected_size_);
}
default:
UNREACHABLE();
return Status::Error(500, "Unreachable");
} }
case td_api::inputFileGenerated::ID: { }();
auto *generated_file = static_cast<const td_api::inputFileGenerated *>(file.get());
return register_generate(is_encrypted ? FileType::Encrypted : type, generated_file->original_path_, return check_input_file_id(type, std::move(r_file_id), is_encrypted, allow_zero);
generated_file->conversion_, owner_dialog_id, generated_file->expected_size_);
}
default:
UNREACHABLE();
return Status::Error(500, "Unreachable");
}
} }
vector<tl_object_ptr<telegram_api::InputDocument>> FileManager::get_input_documents(const vector<FileId> &file_ids) { vector<tl_object_ptr<telegram_api::InputDocument>> FileManager::get_input_documents(const vector<FileId> &file_ids) {

View File

@ -284,7 +284,8 @@ class FileManager : public FileLoadManager::Callback {
FileId parse_file(T &parser); FileId parse_file(T &parser);
private: private:
Result<FileId> check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted) TD_WARN_UNUSED_RESULT; Result<FileId> check_input_file_id(FileType type, Result<FileId> result, bool is_encrypted,
bool allow_zero) TD_WARN_UNUSED_RESULT;
FileId register_url(string url, FileType file_type, DialogId owner_dialog_id); FileId register_url(string url, FileType file_type, DialogId owner_dialog_id);