-
Notifications
You must be signed in to change notification settings - Fork 0
Use long for edit byte totals to avoid overflow #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use long for edit byte totals to avoid overflow #10
Conversation
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Summary
This PR addresses a potential integer overflow issue in the ApplyTextEdits
method within the Unity MCP Bridge's script management system. The change modifies how UTF-8 byte counts are tracked during text editing operations by switching from a 32-bit int
to a 64-bit long
for the totalBytes
variable and adding overflow protection with a checked
block.
The modification is part of the ManageScript.cs
file, which handles script editing operations in Unity's MCP Bridge. This bridge enables communication between Unity applications and Large Language Models via the Model Context Protocol. The ApplyTextEdits
method processes text modifications and validates their size, making it critical for the system's text editing functionality.
The change integrates seamlessly with the existing codebase architecture. The method continues to function identically from an external perspective, but now handles edge cases more robustly. The long
type provides a practically unlimited range for byte counting (9.2 × 10^18 vs 2.1 × 10^9 for int
), while the checked
block ensures any theoretical overflow would throw an OverflowException
rather than silently wrapping to negative values.
Important Files Changed
File Changes
Filename | Score | Overview |
---|---|---|
UnityMcpBridge/Editor/Tools/ManageScript.cs |
5/5 | Changed totalBytes from int to long and added overflow protection with checked block |
Confidence score: 5/5
- This PR is safe to merge with minimal risk
- Score reflects simple, defensive programming improvements with no behavioral changes
- No files require special attention
Sequence Diagram
sequenceDiagram
participant User
participant ManageScript
participant FileSystem
participant SHA256
participant ValidationSystem
participant RefreshDebounce
participant AssetDatabase
User->>ManageScript: "HandleCommand(apply_text_edits)"
ManageScript->>FileSystem: "File.ReadAllText(fullPath)"
FileSystem-->>ManageScript: "original content"
ManageScript->>SHA256: "ComputeSha256(original)"
SHA256-->>ManageScript: "currentSha"
alt precondition check fails
ManageScript-->>User: "Error: stale_file"
end
ManageScript->>ManageScript: "Convert edits to spans"
ManageScript->>ManageScript: "Check totalBytes <= MaxEditPayloadBytes"
alt payload too large
ManageScript-->>User: "Error: too_large"
end
ManageScript->>ManageScript: "Sort spans descending by start"
ManageScript->>ManageScript: "Check for overlapping edits"
alt edits overlap
ManageScript-->>User: "Error: Edits overlap"
end
ManageScript->>ManageScript: "Apply edits to working text"
ManageScript->>ValidationSystem: "CheckBalancedDelimiters(working)"
ValidationSystem-->>ManageScript: "validation result"
alt unbalanced delimiters
ManageScript-->>User: "Error: unbalanced_braces"
end
ManageScript->>SHA256: "ComputeSha256(working)"
SHA256-->>ManageScript: "newSha"
ManageScript->>FileSystem: "File.WriteAllText(tmp, working)"
ManageScript->>FileSystem: "File.Replace(tmp, fullPath, backup)"
FileSystem-->>ManageScript: "write complete"
ManageScript->>RefreshDebounce: "ScheduleScriptRefresh(relativePath)"
RefreshDebounce->>AssetDatabase: "ImportAsset(relPath)"
ManageScript-->>User: "Success: Applied edits"
1 file reviewed, no comments
Summary
long
checked
to guard against overflowTesting
pytest -q
https://chatgpt.com/codex/tasks/task_e_68a1e82028f883279970e50b1fbf06e7