Skip to content

Panic: byte index is not a char boundary when input contains special characters or emoji #2989

@p-Voit

Description

@p-Voit

Bug Description

When user input contains special characters or emoji, Forge Code panics with:

ERROR: byte index 3 is not a char boundary; it is inside '✅' (bytes 1..4) of `'✅`

This is a Rust string slicing bug where &str[..n] is used with a byte index that falls in the middle of a multi-byte UTF-8 character.

Steps to Reproduce

  1. Send a message to Forge Code that contains emoji or special characters, for example:
    • ✅ deploy the app
  2. Forge Code panics with a byte index N is not a char boundary error.

Expected Behavior

Forge Code should correctly handle any valid UTF-8 input, including emoji (4 bytes each), special symbols, and other multi-byte characters without panicking.

Root Cause

Somewhere in the Rust codebase, a string is being sliced using byte indices (e.g., &text[..n] or &text[start..end]) without verifying that the indices fall on character boundaries. Emoji are 4 bytes each in UTF-8, many special symbols are 2-3 bytes — so any naive byte-level truncation/slicing will panic for non-ASCII input.

Suggested Fix

Replace raw byte-index slicing with one of the following approaches:

  1. Use str::is_char_boundary() to validate indices before slicing:

    if text.is_char_boundary(n) {
        &text[..n]
    } else {
        &text[..text.floor_char_boundary(n)] // nightly, or manual fallback
    }
  2. Use char_indices() to find the correct byte offset for a given character position:

    let byte_idx = text.char_indices().nth(char_limit).map(|(i, _)| i).unwrap_or(text.len());
    &text[..byte_idx]
  3. Use .chars().take(n).collect::<String>() if a character-count limit is intended.

Environment

  • macOS
  • Forge Code CLI
  • Trigger: emoji and special characters in user input

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions