Skip to content

[BUG] [v0.0.7] truncate_text() in summarizer.rs panics on multi-byte UTF-8 due to byte-index slicing #186

Open

Description

Bug Description

Location: src/cortex-compact/src/summarizer.rs:129

Type: Potential panic on multi-byte UTF-8 content

Version: v0.0.7

Issue Details

The truncate_text function uses byte-index slicing (&text[..max_chars]) which can panic if max_chars falls in the middle of a multi-byte UTF-8 character.

fn truncate_text(text: &str, max_tokens: usize) -> String {
    let max_chars = max_tokens * 4;
    if text.len() <= max_chars {
        text.to_string()
    } else {
        format!("{}...[truncated]", &text[..max_chars])  // BUG: byte-index slicing
    }
}

Why This Is a Bug

When text contains multi-byte UTF-8 characters (like emojis, Chinese characters, etc.), using byte-index slicing &text[..max_chars] can panic at runtime if max_chars lands in the middle of a multi-byte character.

Expected Behavior

The function should use character-based truncation (similar to what's done in cortex-common/src/truncate.rs) instead of byte-index slicing.

Suggested Fix

Use the safe truncation functions from cortex_common::truncate module:

use cortex_common::truncate::truncate_with_ellipsis;

fn truncate_text(text: &str, max_tokens: usize) -> String {
    let max_chars = max_tokens * 4;
    truncate_with_ellipsis(text, max_chars).into_owned()
}

Or implement character-based truncation inline using .chars().take().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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