Skip to content

[BUG] [v0.0.7] cortex import panics with byte index not a char boundary when JSON error preview slices multi-byte UTF-8 (import_cmd.rs:76) #53419

Description

@nightmare0329

Bug Report

Version: v0.0.7
File: src/cortex-cli/src/import_cmd.rs

Description

When cortex import fails to parse the JSON content (e.g., invalid JSON, HTML response from URL, etc.), it tries to show a preview of the first 200 bytes of the content to help the user debug. The preview is computed via a byte-index slice &json_content[..preview_len] where preview_len is a raw byte count. If the content contains multi-byte UTF-8 characters (accented letters, CJK text, emoji, etc.) and the 200-byte boundary falls inside a multi-byte sequence, Rust panics with "byte index N is not a char boundary" instead of showing the helpful error message.

Root Cause

// import_cmd.rs lines 75-76
let preview_len = json_content.len().min(200);  // bytes, not chars
let content_preview = &json_content[..preview_len];  // PANICS if mid-char

Steps to Reproduce

Create an invalid JSON file with a multi-byte UTF-8 character that straddles byte 200:

# Create a file where byte 200 is inside a 2-byte char (e.g. é = 0xC3 0xA9)
python3 -c "print(\"x\" * 199 + chr(0xe9) + \" more text not json\")" > /tmp/bad_import.json
cortex import /tmp/bad_import.json
# PANIC: byte index 200 is not a char boundary; it is inside é (bytes 199..201)

Expected Behavior

The error message should be displayed gracefully without panicking. Expected output:

Failed to parse JSON from file: ...

Received content (first 200 bytes):
xxx...x\xe9 more text not json...

Fix

Use char_indices() to find the safe boundary, or use the chars() iterator:

// Safe byte boundary that respects UTF-8 char boundaries
let preview_len = json_content
    .char_indices()
    .take(200)
    .last()
    .map(|(i, c)| i + c.len_utf8())
    .unwrap_or(0);
let content_preview = &json_content[..preview_len];

Hotkey: 5CzBkL6CJWFa7QSFoWxqiodobsGmxD6oLxLQa24tNxvsT9dn
UID: 137

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions