Skip to content

[Bug] [v0.0.7] Align::pad in output.rs uses byte indexing text[..width] causing panic on multi-byte UTF-8 input #18547

Description

@echoforge2200

Summary

Align::pad in src/cortex-engine/src/output.rs uses byte-based slicing text[..width] to truncate text when it exceeds the column width. This panics at runtime when the truncation point falls in the middle of a multi-byte UTF-8 character.

Location

File: src/cortex-engine/src/output.rs, lines 214-216

pub fn pad(&self, text: &str, width: usize) -> String {
    if text.len() >= width {
        return text[..width].to_string();  // BUG: byte index, not char boundary
    }

Root Cause

text.len() returns the byte length, not the character count. For multi-byte UTF-8 strings (e.g., containing Chinese, Japanese, Arabic, emoji, or accented characters), text[..width] will attempt to slice at a byte offset that may not be a valid UTF-8 character boundary, causing a panic (byte index N is not a char boundary).

Reproduction

let text = "héllo"; // 'é' is 2 bytes (0xC3 0xA9)
// text.len() == 6 (bytes), but char count is 5
// If width == 5, text[..5] slices mid-character -> PANIC
let result = Align::Left.pad("héllo", 5);

User-Facing Impact

The Align::pad function is used by TableBuilder::format_row (line 327) which is called by TableBuilder::build (line 304). Any user who renders a table containing non-ASCII text in a cell that exceeds the column width will experience a runtime panic, crashing the application.

Fix

Use text.chars().count() for length comparison and text.char_indices().nth(width) to find the correct byte boundary for truncation.

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