Return error on incorrect text entities.

GitOrigin-RevId: 46862113571eb16a80ef3bcdf3a6352593dee0dc
This commit is contained in:
levlam 2020-02-20 02:26:36 +03:00
parent d9e2e02897
commit 48b9f38d4a
2 changed files with 17 additions and 20 deletions

View File

@ -1135,17 +1135,12 @@ vector<std::pair<Slice, bool>> find_urls(Slice str) {
return result;
}
// keeps nested, but removes mutually intersecting and empty entities
// keeps nested, but removes mutually intersecting entities
// entities must be pre-sorted
static void remove_unallowed_entities(vector<MessageEntity> &entities) {
vector<const MessageEntity *> nested_entities_stack;
size_t left_entities = 0;
for (size_t i = 0; i < entities.size(); i++) {
if (entities[i].offset < 0 || entities[i].length <= 0 || entities[i].offset > 1000000 ||
entities[i].length > 1000000) {
continue;
}
while (!nested_entities_stack.empty() &&
entities[i].offset >= nested_entities_stack.back()->offset + nested_entities_stack.back()->length) {
// remove non-intersecting entities from the stack
@ -1198,17 +1193,6 @@ static void remove_intersecting_entities(vector<MessageEntity> &entities) {
entities.erase(entities.begin() + left_entities, entities.end());
}
static void fix_entities(vector<MessageEntity> &entities) {
if (entities.empty()) {
// fast path
return;
}
std::sort(entities.begin(), entities.end());
remove_unallowed_entities(entities);
}
vector<MessageEntity> find_entities(Slice text, bool skip_bot_commands, bool only_urls) {
vector<MessageEntity> entities;
@ -2702,7 +2686,20 @@ Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool al
return Status::Error(400, "Strings must be encoded in UTF-8");
}
fix_entities(entities);
for (auto &entity : entities) {
if (entity.offset < 0 || entity.offset > 1000000) {
return Status::Error(400, PSLICE() << "Receive an entity with incorrect offset " << entity.offset);
}
if (entity.length < 0 || entity.length > 1000000) {
return Status::Error(400, PSLICE() << "Receive an entity with incorrect length " << entity.length);
}
}
td::remove_if(entities, [](const MessageEntity &entity) { return entity.length == 0; });
if (!entities.empty()) {
std::sort(entities.begin(), entities.end());
remove_unallowed_entities(entities);
}
TRY_RESULT(result, clean_input_string_with_entities(text, entities));

View File

@ -717,13 +717,13 @@ TEST(MessageEntities, fix_formatted_text) {
td::vector<td::MessageEntity> entities;
entities.emplace_back(td::MessageEntity::Type::Bold, offset, length);
td::vector<td::MessageEntity> fixed_entities;
if (length > 0 && offset >= 0 && static_cast<size_t>(length + offset) > str.size()) {
if (length < 0 || offset < 0 || (length > 0 && static_cast<size_t>(length + offset) > str.size())) {
check_fix_formatted_text(str, entities, true, false, false, false);
check_fix_formatted_text(str, entities, false, false, false, true);
continue;
}
if (length > 0 && offset >= 0 && (length >= 2 || offset != 3)) {
if (length > 0 && (length >= 2 || offset != 3)) {
fixed_entities.emplace_back(td::MessageEntity::Type::Bold, offset, length);
}
check_fix_formatted_text(str, entities, str, fixed_entities, true, false, false, false);