Bound tokenizer merges count to INT32_MAX like the tokens table#548
Open
gigioneggiando wants to merge 1 commit into
Open
Bound tokenizer merges count to INT32_MAX like the tokens table#548gigioneggiando wants to merge 1 commit into
gigioneggiando wants to merge 1 commit into
Conversation
vocab_load() validates the token table with tokens.len > INT32_MAX but omits the same bound on the merges table. The merge loop then stores each rank with table_put(&vocab->merge_rank, merge, (int)i), narrowing the uint64 index to int. A GGUF declaring more than INT32_MAX merges makes (int)i wrap negative and stores corrupt ranks. Mirror the tokens sibling's bound on merges.len.
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.
Summary
vocab_load()validates the token table withtokens.len > INT32_MAX, but the merges table validation omits the same bound. The merge loop then stores each rank with:A GGUF declaring more than
INT32_MAXmerges makes(int)iwrap negative and stores corrupt merge ranks (affecting tokenization correctness).Fix
Mirror the
tokenssibling's bound: rejectmerges.len > INT32_MAXin the merges validation.Verification
Source-confirmed and compile-checked (the patched translation unit builds cleanly under the ASan/UBSan flags). I did not build a runtime PoC — it would require a crafted GGUF declaring >2³¹ merge entries, which isn't practical to materialize as a file. The
(int)inarrowing and the missing bound (vs the present one ontokens.len) are visible directly invocab_load.Found during a coordinated security review of ds4.