Skip to content

Conversation

dsarno
Copy link
Owner

@dsarno dsarno commented Aug 17, 2025

Summary

  • switch ApplyTextEdits to track UTF-8 byte counts with long
  • wrap byte count accumulation in checked to guard against overflow

Testing

  • pytest -q

https://chatgpt.com/codex/tasks/task_e_68a1e82028f883279970e50b1fbf06e7

Copy link

coderabbitai bot commented Aug 17, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/fix-potential-integer-overflow-in-byte-count

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dsarno dsarno merged commit fedf444 into protocol-framing Aug 17, 2025
2 checks passed
Copy link

@greptile-apps greptile-apps bot left a 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"
Loading

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

@dsarno dsarno deleted the codex/fix-potential-integer-overflow-in-byte-count branch August 19, 2025 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant