From cdcfe5786803548a6436b2fb9fae3c03d10732d7 Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Sat, 8 May 2021 17:26:21 -0700 Subject: [PATCH] Validate width and height inputs (#162) * Validate width and height inputs - Fixes #157 * Add window size check to `imgui-winit` demo - There is really nothing better that can be done in this case. - The surface, buffer, and world sizes must all be non-zero. - A zero-length surface would (previously) panic in `wgpu`. - A zero-length buffer would panic with the new assertions. - A zero-length world would cause a divide-by-zero panic when drawing. --- examples/imgui-winit/src/main.rs | 14 ++++++++------ src/lib.rs | 13 ++++++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/imgui-winit/src/main.rs b/examples/imgui-winit/src/main.rs index 068110a..52cacd5 100644 --- a/examples/imgui-winit/src/main.rs +++ b/examples/imgui-winit/src/main.rs @@ -100,13 +100,15 @@ fn main() -> Result<(), Error> { // Resize the window if let Some(size) = input.window_resized() { - // Resize the surface texture - pixels.resize_surface(size.width, size.height); + if size.width > 0 && size.height > 0 { + // Resize the surface texture + pixels.resize_surface(size.width, size.height); - // Resize the world - let LogicalSize { width, height } = size.to_logical(scale_factor); - world.resize(width, height); - pixels.resize_buffer(width, height); + // Resize the world + let LogicalSize { width, height } = size.to_logical(scale_factor); + world.resize(width, height); + pixels.resize_buffer(width, height); + } } // Update internal state and request a redraw diff --git a/src/lib.rs b/src/lib.rs index 72064ec..671f93a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,7 +203,14 @@ impl Pixels { /// /// Call this method to change the virtual screen resolution. E.g. when you want your pixel /// buffer to be resized from `640x480` to `800x600`. + /// + /// # Panics + /// + /// Panics when `width` or `height` are 0. pub fn resize_buffer(&mut self, width: u32, height: u32) { + assert!(width > 0); + assert!(height > 0); + // Recreate the backing texture let (scaling_matrix_inverse, texture_extent, texture, scaling_renderer, pixels_buffer_size) = builder::create_backing_texture( @@ -237,8 +244,12 @@ impl Pixels { /// texture for non-integer scaling ratios. /// /// Call this method in response to a resize event from your window manager. The size expected - /// is in physical pixel units. + /// is in physical pixel units. Does nothing when `width` or `height` are 0. pub fn resize_surface(&mut self, width: u32, height: u32) { + if width == 0 || height == 0 { + return; + } + // Update SurfaceTexture dimensions self.surface_size.width = width; self.surface_size.height = height;