Repository navigation
autolink: avoid unsigned wrap in the rewind scans - #823
Open
1820893135-pixel wants to merge 1 commit into
Open
1820893135-pixel wants to merge 1 commit into
1820893135-pixel wants to merge 1 commit into
Conversation
sd_autolink__email() and sd_autolink__url() walk backwards with an index
expression computed in size_t:
data[-rewind - 1]
With rewind == 0 this is data + (size_t)-1, which is undefined pointer
arithmetic even though the resulting address is the previous byte. UBSan
reports "addition of unsigned offset ... overflowed" for both functions,
and with -fno-sanitize-recover=all rendering a 2-byte or a 9-byte
document aborts.
Walk with a pointer that is decremented once per iteration instead, so no
index is ever formed below zero. The bytes read are the same.
This was referenced Oct 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #820 and #821.
Root cause
Both rewind scans form a negative index in
size_t:-rewind - 1is unsigned, so on the first iteration it isSIZE_MAX, anddata + SIZE_MAXis undefined pointer arithmetic even though the resultingaddress (the byte before
data) is inside the buffer. UBSan reports it forboth functions:
and with
-fno-sanitize-recover=all, rendering the 9-byte documenthttps://aor the 2-byte document
a@aborts the process.Fix
Walk with a pointer that is decremented once per iteration:
No index below zero is ever formed; the loop bound and the bytes read are
unchanged.
Verification
Build (no Ruby needed):
with the
driver.cfrom the reports.https://aautolink.c:272a@autolink.c:216No behaviour change: rendered 71 documents (every
test/MarkdownTest_1.0*/Tests/*.textplus 30 autolink/table/blockquote cases)with the pristine and the patched build (plain
-O2); HTML output, stderr andexit status are byte-identical.
Note
For the callers in
markdown.c,max_rewindis the distance to the lastconsumed character, so the byte reached is inside the document; the defect is
the undefined index arithmetic itself.