fix(engine): use saturating casts for token counts in streaming#40
Closed
fix(engine): use saturating casts for token counts in streaming#40
Conversation
Greptile OverviewGreptile SummaryThis PR fixes integer overflow and truncation issues across three critical modules by implementing safe type conversions with saturation semantics. Key Changes:
Impact: Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| src/cortex-engine/src/streaming.rs | Added saturating_i64_to_u32 function to safely convert token counts from i64 to u32, preventing silent truncation on large values |
| src/cortex-agents/src/mention.rs | Added UTF-8 boundary-safe slicing helpers (safe_slice_up_to, safe_slice_from) to prevent panics when slicing multi-byte characters |
| src/cortex-tui/src/interactive/renderer.rs | Used try_from with saturation to prevent usize to u16 overflow when calculating widget height |
Sequence Diagram
sequenceDiagram
participant LLM as LLM Provider
participant Client as Client (TokenUsage)
participant Stream as Streaming Module
participant TUI as TUI Renderer
participant Agent as Agent Mention Parser
Note over LLM,Client: Token Count Conversion Flow
LLM->>Client: Returns token counts (i64)
Client->>Stream: TokenUsage {input_tokens: i64, output_tokens: i64}
Stream->>Stream: saturating_i64_to_u32(value)
Note right of Stream: Clamps negative to 0<br/>Clamps > u32::MAX to u32::MAX
Stream->>Stream: StreamTokenUsage {u32 fields}
Note over TUI: Height Calculation Flow
TUI->>TUI: Calculate items_count (usize)
TUI->>TUI: u16::try_from(items_count)
alt items_count > u16::MAX
TUI->>TUI: Returns u16::MAX
else items_count <= u16::MAX
TUI->>TUI: Returns items_count as u16
end
TUI->>TUI: saturating_add for total height
Note over Agent: UTF-8 Safe Slicing Flow
Agent->>Agent: Detect mention position (byte index)
Agent->>Agent: safe_slice_up_to(text, pos)
alt pos is not char boundary
Agent->>Agent: Search backwards for valid boundary
end
Agent->>Agent: safe_slice_from(text, pos)
alt pos is not char boundary
Agent->>Agent: Search forwards for valid boundary
end
Agent->>Agent: Return sliced strings safely
echobt
added a commit
that referenced
this pull request
Feb 4, 2026
…ng operations This PR consolidates the following numeric safety fixes: - #39: Use saturating casts in git_info to prevent overflow - #40: Use saturating casts for token counts in streaming - #41: Use saturating subtraction to prevent underflow in compaction All changes use saturating arithmetic operations: - Replaced direct casts with saturating_sub and try_into - Prevents panic on numeric overflow/underflow conditions
Contributor
Author
|
Consolidated into #71 - fix: consolidated numeric overflow/underflow prevention with saturating operations |
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
Fixes #5188 - streaming.rs uses unchecked narrowing casts.
Problem
Token count conversions can silently truncate on very large values.
Solution
Used saturating conversion to cap at u32::MAX instead of truncating.