gfx-rs/wgpu · error

slice offset {slice_offset} is out of range for buffer of si

Error message

slice offset {slice_offset} is out of range for buffer of size {whole_size}

What it means

Buffer::slice validates that the requested slice offset does not exceed the buffer's total size via check_buffer_bounds, panicking if offset > whole_size. This is a debug-time safety check so that out-of-range buffer slicing fails immediately with a clear message instead of producing invalid bindings.

Source

Thrown at wgpu/src/api/buffer.rs:1056

    /// Copies all elements from src into `self`.
    ///
    /// The length of `src` must be the same as `self`.
    ///
    /// This method is equivalent to
    /// [`self.slice(..).copy_from_slice(src)`][WriteOnly::copy_from_slice].
    pub fn copy_from_slice(&mut self, src: &[u8]) {
        self.slice(..).copy_from_slice(src)
    }
}

#[track_caller]
fn check_buffer_bounds(
    whole_size: BufferAddress,
    slice_offset: BufferAddress,
    slice_size: BufferAddress,
) {
    if slice_offset > whole_size {
        panic!(
            "slice offset {} is out of range for buffer of size {}",
            slice_offset, whole_size
        );
    }

    // Detect integer overflow.
    let end = slice_offset.checked_add(slice_size);
    if end.is_none_or(|end| end > whole_size) {
        panic!(
            "slice offset {} size {} is out of range for buffer of size {}",
            slice_offset, slice_size, whole_size
        );
    }
}

#[track_caller]
pub(crate) fn range_to_offset_size<S: RangeBounds<BufferAddress>>(
    bounds: S,

View on GitHub (pinned to 3e11ff59bf)

Solutions

  1. Verify the slice offset against buffer.size() before slicing; clamp or return early if offset > size.
  2. Fix the constant/stride that produced the offset — often an alignment (256-byte uniform) or struct-size mismatch.
  3. Check that you are slicing the intended buffer, not one allocated with a smaller size.

Example fix

// before
let view = buffer.slice(1024..1024 + size); // buffer.size() == 512
// after
assert!(1024 + size <= buffer.size(), "slice out of bounds");
let view = buffer.slice(0..size);
Defensive patterns

Strategy: validation

Validate before calling

if slice_offset > buffer.size() {
    return Err(format!("offset {slice_offset} exceeds buffer size {}", buffer.size()));
}
let view = buffer.slice(slice_offset..end);

Try / catch

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| buffer.slice(off..end)));

Prevention

When it happens

Trigger: Calling buffer.slice(offset..end) (or related APIs) with an offset larger than the buffer's size — e.g. a hardcoded offset beyond the allocation, or a size computed from a different (smaller) buffer.

Common situations: Binding uniform buffers at computed offsets where the offset table no longer matches the buffer sizes (stride changes, alignment fixes, buffer shrunk), or typos like buffer.slice(size..size + x) on a buffer of length size.

Related errors


AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03). Data as JSON: /api/errors/e3545df693291a1a. Report an issue: GitHub.