From 9ab35b70b5fb372b147e046fe1e5a437bd68183b Mon Sep 17 00:00:00 2001 From: Alex Yusiuk Date: Sun, 21 Sep 2025 17:06:16 +0300 Subject: [PATCH 1/3] refactor(ironrdp-web): fix `indexing_slicing` clippy lint warnings --- Cargo.toml | 1 + crates/ironrdp-web/src/canvas.rs | 6 ++--- crates/ironrdp-web/src/image.rs | 43 +++++++++++++++++++++++++------ crates/ironrdp-web/src/session.rs | 2 +- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ba8229bd8..68a62e407 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,7 @@ fn_to_numeric_cast_any = "warn" ptr_cast_constness = "warn" # == Correctness == # +#indexing_slicing = "warn" TODO: enable this lint project wide. cast_lossless = "warn" cast_possible_truncation = "warn" cast_possible_wrap = "warn" diff --git a/crates/ironrdp-web/src/canvas.rs b/crates/ironrdp-web/src/canvas.rs index 54cfbb2a3..ba040fb01 100644 --- a/crates/ironrdp-web/src/canvas.rs +++ b/crates/ironrdp-web/src/canvas.rs @@ -44,9 +44,9 @@ impl Canvas { let region_height = region.height(); let mut src = buffer.chunks_exact(4).map(|pixel| { - let r = pixel[0]; - let g = pixel[1]; - let b = pixel[2]; + let r = *pixel.first().expect("index cannot be out of bounds"); + let g = *pixel.get(1).expect("index cannot be out of bounds"); + let b = *pixel.get(2).expect("index cannot be out of bounds"); u32::from_be_bytes([0, r, g, b]) }); diff --git a/crates/ironrdp-web/src/image.rs b/crates/ironrdp-web/src/image.rs index 13ac3fedb..571022c1c 100644 --- a/crates/ironrdp-web/src/image.rs +++ b/crates/ironrdp-web/src/image.rs @@ -1,9 +1,13 @@ #![allow(clippy::arithmetic_side_effects)] +use anyhow::Context; use ironrdp::pdu::geometry::{InclusiveRectangle, Rectangle as _}; use ironrdp::session::image::DecodedImage; -pub(crate) fn extract_partial_image(image: &DecodedImage, region: InclusiveRectangle) -> (InclusiveRectangle, Vec) { +pub(crate) fn extract_partial_image( + image: &DecodedImage, + region: InclusiveRectangle, +) -> anyhow::Result<(InclusiveRectangle, Vec)> { // PERF: needs actual benchmark to find a better heuristic if region.height() > 64 || region.width() > 512 { extract_whole_rows(image, region) @@ -13,7 +17,10 @@ pub(crate) fn extract_partial_image(image: &DecodedImage, region: InclusiveRecta } // Faster for low-height and smaller images -fn extract_smallest_rectangle(image: &DecodedImage, region: InclusiveRectangle) -> (InclusiveRectangle, Vec) { +fn extract_smallest_rectangle( + image: &DecodedImage, + region: InclusiveRectangle, +) -> anyhow::Result<(InclusiveRectangle, Vec)> { let pixel_size = usize::from(image.pixel_format().bytes_per_pixel()); let image_width = usize::from(image.width()); @@ -33,20 +40,31 @@ fn extract_smallest_rectangle(image: &DecodedImage, region: InclusiveRectangle) for row in 0..region_height { let src_begin = image_stride * (region_top + row) + region_left * pixel_size; let src_end = src_begin + region_stride; - let src_slice = &src[src_begin..src_end]; + let src_slice = src.get(src_begin..src_end).with_context(|| { + format!( + "invalid region {region:?} for image with dimensions {}x{}", + image.width(), + image.height() + ) + })?; let target_begin = region_stride * row; let target_end = target_begin + region_stride; - let target_slice = &mut dst[target_begin..target_end]; + let target_slice = dst + .get_mut(target_begin..target_end) + .expect("slice index cannot be out of bounds"); target_slice.copy_from_slice(src_slice); } - (region, dst) + Ok((region, dst)) } // Faster for high-height and bigger images -fn extract_whole_rows(image: &DecodedImage, region: InclusiveRectangle) -> (InclusiveRectangle, Vec) { +fn extract_whole_rows( + image: &DecodedImage, + region: InclusiveRectangle, +) -> anyhow::Result<(InclusiveRectangle, Vec)> { let pixel_size = usize::from(image.pixel_format().bytes_per_pixel()); let image_width = usize::from(image.width()); @@ -60,7 +78,16 @@ fn extract_whole_rows(image: &DecodedImage, region: InclusiveRectangle) -> (Incl let src_begin = region_top * image_stride; let src_end = (region_bottom + 1) * image_stride; - let dst = src[src_begin..src_end].to_vec(); + let dst = src + .get(src_begin..src_end) + .with_context(|| { + format!( + "invalid region {region:?} for image with dimensions {}x{}", + image.width(), + image.height() + ) + })? + .to_vec(); let wider_region = InclusiveRectangle { left: 0, @@ -69,5 +96,5 @@ fn extract_whole_rows(image: &DecodedImage, region: InclusiveRectangle) -> (Incl bottom: region.bottom, }; - (wider_region, dst) + Ok((wider_region, dst)) } diff --git a/crates/ironrdp-web/src/session.rs b/crates/ironrdp-web/src/session.rs index e5933bad3..e7ca2e039 100644 --- a/crates/ironrdp-web/src/session.rs +++ b/crates/ironrdp-web/src/session.rs @@ -587,7 +587,7 @@ impl iron_remote_desktop::Session for Session { } ActiveStageOutput::GraphicsUpdate(region) => { // PERF: some copies and conversion could be optimized - let (region, buffer) = extract_partial_image(&image, region); + let (region, buffer) = extract_partial_image(&image, region)?; gui.draw(&buffer, region).context("draw updated region")?; } ActiveStageOutput::PointerDefault => { From f06bc51ec489da1107d5ccd58865f951241bd943 Mon Sep 17 00:00:00 2001 From: Alexandr Yusuk Date: Wed, 24 Sep 2025 14:02:08 +0300 Subject: [PATCH 2/3] refactoring --- crates/ironrdp-web/src/canvas.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/ironrdp-web/src/canvas.rs b/crates/ironrdp-web/src/canvas.rs index ba040fb01..9ded3eac9 100644 --- a/crates/ironrdp-web/src/canvas.rs +++ b/crates/ironrdp-web/src/canvas.rs @@ -44,10 +44,8 @@ impl Canvas { let region_height = region.height(); let mut src = buffer.chunks_exact(4).map(|pixel| { - let r = *pixel.first().expect("index cannot be out of bounds"); - let g = *pixel.get(1).expect("index cannot be out of bounds"); - let b = *pixel.get(2).expect("index cannot be out of bounds"); - u32::from_be_bytes([0, r, g, b]) + let [r, g, b] = pixel.first_chunk::<3>().expect("cannot be out of bounds"); + u32::from_be_bytes([0, *r, *g, *b]) }); let mut dst = self.surface.buffer_mut().expect("surface buffer"); From 5da508dcd640ae11bc58222177528568022317b1 Mon Sep 17 00:00:00 2001 From: Alexandr Yusuk Date: Wed, 24 Sep 2025 14:17:27 +0300 Subject: [PATCH 3/3] fix clippy --- crates/ironrdp-web/src/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ironrdp-web/src/image.rs b/crates/ironrdp-web/src/image.rs index 571022c1c..c13f4a0ed 100644 --- a/crates/ironrdp-web/src/image.rs +++ b/crates/ironrdp-web/src/image.rs @@ -1,6 +1,6 @@ #![allow(clippy::arithmetic_side_effects)] -use anyhow::Context; +use anyhow::Context as _; use ironrdp::pdu::geometry::{InclusiveRectangle, Rectangle as _}; use ironrdp::session::image::DecodedImage;