sinelaw/fresh · error

Buffer range out of bounds: requested

Error message

Buffer range out of bounds: requested {}..{}, buffer size {}

What it means

A buffered range read verified with anyhow::ensure! that buffer_end <= data.len() failed: the requested byte range extends past the end of the buffer's data. buffer/mod.rs:1411 guards after waiting for the buffer to load and fetching its data, so this indicates a stale/oversized range versus actual buffer content.

Solutions

  1. Re-fetch the current buffer length and clamp buffer_end to data.len() before requesting the range
  2. Invalidate cached offsets/ranges after any reload or external file change
  3. Refresh the buffer from disk (reload) before reading ranges if the file may have changed externally

Example fix

// before
let data = read_buffer_range(buffer_id, 0, cached_len)?;
// after
let len = current_buffer_len(buffer_id)?;
let data = read_buffer_range(buffer_id, 0, cached_len.min(len))?;
Defensive patterns

Strategy: validation

Validate before calling

let len = current_buffer_len(buffer_id)?;
let end = end.min(len);
if start > end { return Ok(String::new()); }

Try / catch

match read_buffer_range(buffer_id, s, e) {
    Err(e) if e.to_string().contains("out of bounds") => reload_and_recompute_ranges(buffer_id),
    other => other,
}

Prevention

When it happens

Trigger: Requesting buffer content for a range (buffer_start..buffer_end) computed against an older buffer length, then the file shrank (external modification, truncated reload) — or a caller passing an end offset beyond the buffer size.

Common situations: File changed on disk and shrank while the editor held old offsets; rendering code caching buffer lengths across reloads; plugins using stale spans after a revert/reload.

Related errors


AI-assisted analysis of sinelaw/fresh@67894ca546 (2026-09-13). Data as JSON: /api/errors/14d0a0115346a9b3. Report an issue: GitHub.

Appendix: source

Thrown at crates/fresh-editor-core/src/model/buffer/mod.rs:1411

                // Clip to the requested range
                let read_start = current_offset.max(piece_start_in_doc);
                let read_end = end_offset.min(piece_end_in_doc);

                if read_end > read_start {
                    let offset_in_piece = read_start - piece_start_in_doc;
                    let bytes_to_read = read_end - read_start;

                    let buffer_start = piece_view.buffer_offset + offset_in_piece;
                    let buffer_end = buffer_start + bytes_to_read;

                    // Buffer should be loaded now
                    let buffer = self.buffers.get(buffer_id).context("Buffer not found")?;
                    let data = buffer
                        .get_data()
                        .context("Buffer data unavailable after load")?;

                    anyhow::ensure!(
                        buffer_end <= data.len(),
                        "Buffer range out of bounds: requested {}..{}, buffer size {}",
                        buffer_start,
                        buffer_end,
                        data.len()
                    );

                    result.extend_from_slice(&data[buffer_start..buffer_end]);
                    current_offset = read_end;
                    made_progress = true;
                }
            }

            // If we didn't make progress and didn't restart iteration, this is an error
            if !made_progress && !restarted_iteration {
                tracing::error!(
                    "get_text_range_mut: No progress at offset {} (requested range: {}..{}, buffer len: {})",
                    current_offset,

View on GitHub (pinned to 67894ca546)