Skip to content

fix off-by-one null terminator in toUTF8_UTF16 - #233

Open
zayeem06 wants to merge 1 commit into
antlr:masterfrom
zayeem06:utf8-null-terminate-offbyone
Open

fix off-by-one null terminator in toUTF8_UTF16#233
zayeem06 wants to merge 1 commit into
antlr:masterfrom
zayeem06:utf8-null-terminate-offbyone

Conversation

@zayeem06

@zayeem06 zayeem06 commented Jul 3, 2026

Copy link
Copy Markdown

Off-by-one terminator in toUTF8_UTF16
The UTF-16 to UTF-8 converter in runtime/C/src/antlr3string.c writes its terminator at *(outputEnd+1) after ConvertUTF16toUTF8 leaves outputEnd one past the last byte, so it lands a byte too far. On an empty UTF-16 string the buffer is a single byte (size = len*3 = 0, malloc(1)) and the write goes to index 1, one byte past the allocation; on non-empty input it leaves a stray uninitialized byte in front of the terminator. Terminating at *outputEnd writes at the byte the converter actually stopped on, which is always inside the size+1 allocation.

Reviewers may want to confirm the empty-string path: newStr8("")->toUTF8 reaches this with len == 0. ASan flags a 1-byte heap-buffer-overflow write there before the change and is clean after.

@zayeem06

Copy link
Copy Markdown
Author

any update on this?

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.

1 participant