From 3f8490efa76b30590abe5984f2d24e9d12621d59 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Mon, 25 Jul 2022 15:20:23 +0200 Subject: [PATCH 1/2] Validate the number of color attachments in create_render_pipeline Returns an error instead of panicking when populating an ArrayVec later in the same function. --- wgpu-core/src/device/mod.rs | 10 ++++++++++ wgpu-core/src/pipeline.rs | 2 ++ 2 files changed, 12 insertions(+) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index bc3553f0bb..fb32d8580a 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -2469,6 +2469,16 @@ impl Device { ArrayVec::::new(); let mut shader_binding_sizes = FastHashMap::default(); + let num_attachments = desc.fragment.as_ref().map(|f| f.targets.len()).unwrap_or(0); + if num_attachments > hal::MAX_COLOR_ATTACHMENTS { + return Err( + pipeline::CreateRenderPipelineError::TooManyColorAttachments { + given: num_attachments as u32, + limit: hal::MAX_COLOR_ATTACHMENTS as u32, + }, + ); + } + let color_targets = desc .fragment .as_ref() diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index ca30e56a2e..e235ca4706 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -323,6 +323,8 @@ pub enum CreateRenderPipelineError { DepthStencilState(#[from] DepthStencilStateError), #[error("invalid sample count {0}")] InvalidSampleCount(u32), + #[error("the number of color attachments {given} exceeds the limit {limit}")] + TooManyColorAttachments { given: u32, limit: u32 }, #[error("the number of vertex buffers {given} exceeds the limit {limit}")] TooManyVertexBuffers { given: u32, limit: u32 }, #[error("the total number of vertex attributes {given} exceeds the limit {limit}")] From d3a76724e26925ca5f412c440803fca2ce584eba Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Mon, 25 Jul 2022 15:29:05 +0200 Subject: [PATCH 2/2] Add an entry in the changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c9f02ed0e..c1f263ce94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ the same every time it is rendered, we now warn if it is missing. - Fix panics that occur when using `as_hal` functions when the hal generic type does not match the hub being looked up in by @i509VCB [#2871](https://github.com/gfx-rs/wgpu/pull/2871) - Add some validation in map_async by @nical in [#2876](https://github.com/gfx-rs/wgpu/pull/2876) - Fix bugs when mapping/unmapping zero-sized buffers and ranges by @nical in [#2877](https://github.com/gfx-rs/wgpu/pull/2877) +- Validate the number of color attachments in `create_render_pipeline` by @nical in [#2913](https://github.com/gfx-rs/wgpu/pull/2913) #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851)