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.
Summary
Align::padinsrc/cortex-engine/src/output.rsuses byte-based slicingtext[..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-216Root 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
User-Facing Impact
The
Align::padfunction is used byTableBuilder::format_row(line 327) which is called byTableBuilder::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 andtext.char_indices().nth(width)to find the correct byte boundary for truncation.