gfx-rs/wgpu · error
slice offset {slice_offset} size {slice_size} is out of rang
Error message
slice offset {slice_offset} size {slice_size} is out of range for buffer of size {whole_size} What it means
Buffer::slice also validates offset + size, panicking when the addition overflows or the end of the requested range exceeds the buffer's total size. This guarantees every buffer slice refers to memory fully inside the buffer.
Source
Thrown at wgpu/src/api/buffer.rs:1065
}
#[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,
whole_size: BufferAddress,
) -> (BufferAddress, BufferAddress) {
let offset = match bounds.start_bound() {
Bound::Included(&bound) => bound,
Bound::Excluded(&bound) => bound + 1,
Bound::Unbounded => 0,
};
let size = match bounds.end_bound() {
Bound::Included(&bound) => bound + 1 - offset,View on GitHub (pinned to 3e11ff59bf)
Solutions
- Ensure offset + size <= buffer.size() before slicing; recompute the end from buffer.size().
- Fix integer overflow in offset arithmetic (use saturating/checked math and correct types).
- Resize the buffer at creation time to cover all regions you plan to slice.
Example fix
// before let view = buffer.slice(off..off + size); // off + size > buffer.size() // after let end = off + size; assert!(end <= buffer.size()); let view = buffer.slice(off..end);
Defensive patterns
Strategy: validation
Validate before calling
let end = slice_offset.checked_add(slice_size);
match end {
Some(e) if e <= buffer.size() => buffer.slice(slice_offset..e),
_ => return Err("slice range out of bounds or overflowing".into()),
} Try / catch
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| buffer.slice(off..off + size)));
Prevention
- Use checked_add when combining offsets and sizes.
- Size buffers up front to cover all planned regions, including alignment padding.
- Assert end <= buffer.size() in debug builds around dynamic bind-group offset code.
When it happens
Trigger: Calling buffer.slice(start..end) where end > buffer.size(), or where start + end overflows u64 (the checked_add path), e.g. a wraparound from a bad computation.
Common situations: Computing dynamic bind-group offsets with wrapping arithmetic, stacking multiple regions into one buffer and running past the end, or unit mistakes (bytes vs. element counts) inflating the end offset.
Related errors
- slice offset {slice_offset} is out of range for buffer of si
- Buffers are required to build acceleration structures
- iterator given to write_iter() produced more than {self_len}
- iterator given to write_iter() produced {iter_len} elements
- source slice length ({src_len}) does not match destination s
AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03).
Data as JSON: /api/errors/0bacca5b9172b1be.
Report an issue: GitHub.