diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index dd85d32df..d1642b223 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -1135,17 +1135,12 @@ vector> 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 &entities) { vector 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 &entities) { entities.erase(entities.begin() + left_entities, entities.end()); } -static void fix_entities(vector &entities) { - if (entities.empty()) { - // fast path - return; - } - - std::sort(entities.begin(), entities.end()); - - remove_unallowed_entities(entities); -} - vector find_entities(Slice text, bool skip_bot_commands, bool only_urls) { vector entities; @@ -2702,7 +2686,20 @@ Status fix_formatted_text(string &text, vector &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)); diff --git a/test/message_entities.cpp b/test/message_entities.cpp index 1f91b3f6f..bba2bcecf 100644 --- a/test/message_entities.cpp +++ b/test/message_entities.cpp @@ -717,13 +717,13 @@ TEST(MessageEntities, fix_formatted_text) { td::vector entities; entities.emplace_back(td::MessageEntity::Type::Bold, offset, length); td::vector fixed_entities; - if (length > 0 && offset >= 0 && static_cast(length + offset) > str.size()) { + if (length < 0 || offset < 0 || (length > 0 && static_cast(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);