Skip to content

cortex import panics with 'byte index is not a char boundary' when file contains UTF-8 chars near 200-byte mark #53399

@nightmare0329

Description

@nightmare0329

Bug Report

Version: cortex v0.0.7
Command: cortex import <file>

Description

cortex import panics (process abort) instead of showing a clean error message when the import file contains invalid JSON that includes multi-byte UTF-8 characters near the 200-byte truncation boundary used for error previews.

Steps to Reproduce

# Create a file with a multi-byte character (e-acute, U+00E9) at bytes 199-200:
$ python3 -c "print('x'*199 + chr(0xe9) + 'rest')" > /tmp/bad.txt
# File is 242 bytes total; e-acute spans bytes 199-200

# Import it (JSON parse will fail, triggering the error preview code):
$ cortex import /tmp/bad.txt

Expected Behavior

A clean error message like:

Error: Failed to parse JSON from file: expected value at line 1 column 1

Actual Behavior

thread 'main' panicked at src/cortex-cli/src/import_cmd.rs:76:48
byte index 200 is not a char boundary; it is inside 'e-acute' (bytes 199..201) of `xxx...`
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
Aborted (core dumped)

Screenshot (real binary output)

import UTF-8 panic

Root Cause

In src/cortex-cli/src/import_cmd.rs, the error path computes a preview length using byte count, then slices the string at that byte position — which panics if the byte offset falls inside a multi-byte UTF-8 character:

let preview_len = json_content.len().min(200);  // byte length
let content_preview = &json_content[..preview_len];  // PANICS if mid-char boundary

The fix is to truncate by character count instead of byte count:

let content_preview: String = json_content.chars().take(200).collect();
let truncated = if json_content.chars().count() > 200 { "..." } else { "" };

File: src/cortex-cli/src/import_cmd.rs, lines 75-78

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideIssues related to IDEvalidValid issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions