diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index bf07dffbe..47a9dfeaf 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -866,6 +866,20 @@ pub enum ImmediateUploadError { size: u32, immediate_size: u32, }, + #[error("Start index {start_index} overruns the value data range with {data_size} element(s)")] + ValueStartIndexOverrun { start_index: u32, data_size: usize }, + #[error( + "Start index {} + count of {} overruns the value data range \ + with {} element(s)", + start_index, + count, + data_size + )] + ValueEndIndexOverrun { + start_index: u32, + count: u32, + data_size: usize, + }, } impl WebGpuError for ImmediateUploadError { diff --git a/wgpu-core/src/command/pass.rs b/wgpu-core/src/command/pass.rs index 15d2e241d..878b5ef64 100644 --- a/wgpu-core/src/command/pass.rs +++ b/wgpu-core/src/command/pass.rs @@ -240,8 +240,30 @@ where pipeline_layout.validate_immediates_ranges(offset, size_bytes)?; - let values_end_offset = (values_offset + size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize; - let data_slice = &immediates_data[(values_offset as usize)..values_end_offset]; + let values_offset_usize = values_offset as usize; + if values_offset_usize > immediates_data.len() { + return Err(ImmediateUploadError::ValueStartIndexOverrun { + start_index: values_offset, + data_size: immediates_data.len(), + } + .into()); + } + + // NOTE: The `validate_immediates_ranges` call above validates `size_bytes` is aligned. + let size_immediate_elements = size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT; + let size_immediate_elements_usize = size_immediate_elements as usize; + if size_immediate_elements_usize > immediates_data.len() - values_offset_usize { + return Err(ImmediateUploadError::ValueEndIndexOverrun { + start_index: values_offset, + count: size_immediate_elements, + data_size: immediates_data.len(), + } + .into()); + } + + // NOTE: These additions are will not overflow, because we've validated the range above. + let values_end_offset = values_offset_usize + size_immediate_elements_usize; + let data_slice = &immediates_data[(values_offset_usize)..values_end_offset]; f(data_slice);