Hmbown/CodeWhale · info

event recovery newline index fits u64

Error message

event recovery newline index fits u64

What it means

This panic comes from a `u64::try_from(index).expect(...)` inside the runtime event-log tail repair loop in `crates/tui/src/runtime_threads.rs`. After reading a trailing chunk of a possibly torn event log, the code finds the last newline byte with `rposition` and converts that byte index to u64 before computing the truncate offset. The expect documents the invariant that the index (bounded by an 8 KiB buffer) always fits in u64; on any 64-bit (and realistically 32-bit) platform it cannot fail, so a panic here signals a broken build/platform assumption or a corrupted internal invariant rather than bad input.

Solutions

  1. Treat the panic as a platform-portability finding: replace the expect with saturating/checked conversion or `as u64` only after a compile-time assert that `size_of::<usize>() <= size_of::<u64>()`.
  2. If hit at runtime, inspect the panic backtrace to confirm which try_from failed and verify the target's pointer width; do not ship a build for that target until the conversion is made total.
  3. If it fires on a normal 64-bit build, suspect memory corruption or a modified local copy of runtime_threads.rs and diff against the committed source.

Example fix

// before
truncate_at = chunk_start
    + u64::try_from(index).expect("event recovery newline index fits u64")
    + 1;
// after
const _: () = assert!(size_of::<usize>() <= size_of::<u64>());
truncate_at = chunk_start + u64::try_from(index).unwrap_or(u64::MAX) + 1;
Defensive patterns

Strategy: validation

Validate before calling

const _: () = assert!(size_of::<usize>() <= size_of::<u64>(), "usize must fit in u64 for event-log offsets");

Type guard

fn fits_u64(v: usize) -> Option<u64> { u64::try_from(v).ok() }

Prevention

When it happens

Trigger: Only reachable when the event-log file lacks a final newline, a chunk containing a newline is read, and `usize::try_into::<u64>()` fails for the byte index — i.e. running on a platform where usize is wider than u64 or under a broken compiler/memory model where the conversion is rejected.

Common situations: Developers essentially never hit this in production; it appears when porting the code to exotic targets (e.g. some CHERI / 128-bit-pointer platforms where usize is 128 bits) or when an assertion-based fuzzing/memory-check build flags the try_from as potentially failing.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/4921f752cb557c37. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/runtime_threads.rs:14030

    file.read_exact(&mut last)?;
    if last[0] == b'\n' {
        return Ok(());
    }

    let mut search_end = len;
    let mut truncate_at = 0_u64;
    let mut buffer = [0_u8; 8 * 1024];
    let buffer_len = u64::try_from(buffer.len()).expect("event recovery buffer fits u64");
    while search_end > 0 {
        let chunk_len = usize::try_from(search_end.min(buffer_len))
            .expect("event recovery chunk length fits usize");
        let chunk_len_u64 = u64::try_from(chunk_len).expect("event recovery chunk length fits u64");
        let chunk_start = search_end - chunk_len_u64;
        file.seek(SeekFrom::Start(chunk_start))?;
        file.read_exact(&mut buffer[..chunk_len])?;
        if let Some(index) = buffer[..chunk_len].iter().rposition(|byte| *byte == b'\n') {
            truncate_at = chunk_start
                + u64::try_from(index).expect("event recovery newline index fits u64")
                + 1;
            break;
        }
        search_end = chunk_start;
    }

    file.set_len(truncate_at)
        .with_context(|| format!("Failed to truncate torn tail in {}", path.display()))?;
    file.sync_all()
        .with_context(|| format!("Failed to sync repaired {}", path.display()))?;
    tracing::warn!(
        path = %path.display(),
        removed_bytes = len.saturating_sub(truncate_at),
        "Recovered an unterminated Runtime event-log tail"
    );
    Ok(())
}

View on GitHub (pinned to 73e0f67d83)