Skip to content

Fix Markdown formatting offsets for non-ASCII URLs#3690

Open
michaelsam94 wants to merge 1 commit into
tdlib:masterfrom
michaelsam94:codex/fix-markdown-v3-invalid-utf8
Open

Fix Markdown formatting offsets for non-ASCII URLs#3690
michaelsam94 wants to merge 1 commit into
tdlib:masterfrom
michaelsam94:codex/fix-markdown-v3-invalid-utf8

Conversation

@michaelsam94

@michaelsam94 michaelsam94 commented Jul 6, 2026

Copy link
Copy Markdown

Summary

  • count TextUrl URL arguments in UTF-16 code units when adjusting generated Markdown entity offsets
  • add a regression for a non-ASCII URL followed by a preserved custom emoji entity

Fixes #3686.

Tests

  • git diff --check upstream/master...HEAD
  • Not run: CMake test target, because cmake is not installed in this environment.

@levlam

levlam commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

The fix is wrong and changes nothing. get_markdown_v3 is never called with invalid UTF-8. I have already identified actual cause of the mentioned issue and fixed it in the private TDLib version, which is supposed to be publicly available soon.

@michaelsam94 michaelsam94 force-pushed the codex/fix-markdown-v3-invalid-utf8 branch from f1d36ec to 0da6dfc Compare July 6, 2026 12:16
@michaelsam94

Copy link
Copy Markdown
Author

You are right; the invalid UTF-8 guard was the wrong fix. I force-pushed a corrected patch.

The issue I found is in get_markdown_v3: when closing a TextUrl, it added entity->argument.size() to utf16_added, but that argument can contain non-ASCII UTF-8. This can shift preserved entities after the link to invalid UTF-16 offsets in the generated Markdown before it is reparsed. The patch now uses text_length(entity->argument) and adds a regression with a Cyrillic URL followed by a custom emoji entity.

@michaelsam94 michaelsam94 changed the title Handle invalid UTF-8 in Markdown formatting Fix Markdown formatting offsets for non-ASCII URLs Jul 6, 2026
@levlam

levlam commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

The patch is correct now, but the same fix has already been done in the private TDLib version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_markdown_v3_without_pre parser function causes a core dump due to corrupted UTF-8 bytes

2 participants