Skip to content

diffguard-lsp/text.rs:107,123: lossy usize→u32 casts on 64-bit targets #582

@EffortlessSteven

Description

@EffortlessSteven

Location: crates/diffguard-lsp/src/text.rs lines 107 and 123

Problem: Two lossy usize to u32 casts on 64-bit targets:

Line 107: current_character_utf16.saturating_add(ch.len_utf16() as u32)
Line 123: text.chars().map(|ch| ch.len_utf16() as u32).sum()

ch.len_utf16() returns usize which can exceed u32::MAX on 64-bit targets with very long UTF-16 sequences. Silent truncation corrupts LSP character offset calculations.

Fix: Use u32::try_from() with fallible handling instead of as cast.

Clippy: cast_possible_truncation lint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions