From eee40c1b9a5b6449c0c263e7bc8c121fb3562ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20Yumurtac=C4=B1?= Date: Tue, 10 Dec 2024 17:07:06 -0600 Subject: [PATCH] FIX: Prevent negative column width, row height (#167) * Prevent negative column width / row height in rust * prevent in front-end --- CHANGELOG.md | 1 + base/src/test/test_column_width.rs | 18 ++++++++++++++++++ base/src/test/test_general.rs | 11 +++++++++++ base/src/worksheet.rs | 10 ++++++++-- webapp/src/components/worksheet.tsx | 6 ++++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55c8828..6f83f4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Fixed several issues with pasting content - Fixed several issues with borders +- Fixed bug where columns and rows could be resized to negative width and height, respectively ## [0.2.0] - 2024-11-06 (The HN release) diff --git a/base/src/test/test_column_width.rs b/base/src/test/test_column_width.rs index c2cd136..b5001fc 100644 --- a/base/src/test/test_column_width.rs +++ b/base/src/test/test_column_width.rs @@ -82,3 +82,21 @@ fn test_column_width_higher_edge() { assert!((worksheet.get_column_width(17).unwrap() - DEFAULT_COLUMN_WIDTH).abs() < f64::EPSILON); assert_eq!(model.get_cell_style_index(0, 23, 16), Ok(1)); } + +#[test] +fn test_column_width_negative() { + let mut model = new_empty_model(); + let result = model + .workbook + .worksheet_mut(0) + .unwrap() + .set_column_width(16, -1.0); + assert_eq!(result, Err("Can not set a negative width: -1".to_string())); + assert_eq!(model.workbook.worksheets[0].cols.len(), 0); + let worksheet = model.workbook.worksheet(0).unwrap(); + assert_eq!( + (worksheet.get_column_width(16).unwrap()), + DEFAULT_COLUMN_WIDTH + ); + assert_eq!(model.get_cell_style_index(0, 23, 16), Ok(0)); +} diff --git a/base/src/test/test_general.rs b/base/src/test/test_general.rs index 440f7d4..9c5e97b 100644 --- a/base/src/test/test_general.rs +++ b/base/src/test/test_general.rs @@ -1,5 +1,7 @@ #![allow(clippy::unwrap_used)] +use crate::constants::DEFAULT_ROW_HEIGHT; + use crate::cell::CellValue; use crate::number_format::to_excel_precision_str; @@ -113,6 +115,15 @@ fn test_set_row_height() { worksheet.set_row_height(5, 5.0).unwrap(); let worksheet = model.workbook.worksheet(0).unwrap(); assert!((5.0 - worksheet.row_height(5).unwrap()).abs() < f64::EPSILON); + + let worksheet = model.workbook.worksheet_mut(0).unwrap(); + let result = worksheet.set_row_height(6, -1.0); + assert_eq!(result, Err("Can not set a negative height: -1".to_string())); + + assert_eq!(worksheet.row_height(6).unwrap(), DEFAULT_ROW_HEIGHT); + + worksheet.set_row_height(6, 0.0).unwrap(); + assert_eq!(worksheet.row_height(6).unwrap(), 0.0); } #[test] diff --git a/base/src/worksheet.rs b/base/src/worksheet.rs index 027eafe..f93665c 100644 --- a/base/src/worksheet.rs +++ b/base/src/worksheet.rs @@ -255,11 +255,14 @@ impl Worksheet { /// * If the row does not a have a style we add it. /// * If it has we modify the height and make sure it is applied. /// - /// Fails if column index is outside allowed range. + /// Fails if row index is outside allowed range or height is negative. pub fn set_row_height(&mut self, row: i32, height: f64) -> Result<(), String> { if !is_valid_row(row) { return Err(format!("Row number '{row}' is not valid.")); } + if height < 0.0 { + return Err(format!("Can not set a negative height: {height}")); + } let rows = &mut self.rows; for r in rows.iter_mut() { @@ -284,7 +287,7 @@ impl Worksheet { /// * If the column does not a have a width we simply add it /// * If it has, it might be part of a range and we ned to split the range. /// - /// Fails if column index is outside allowed range. + /// Fails if column index is outside allowed range or width is negative. pub fn set_column_width(&mut self, column: i32, width: f64) -> Result<(), String> { self.set_column_width_and_style(column, width, None) } @@ -298,6 +301,9 @@ impl Worksheet { if !is_valid_column_number(column) { return Err(format!("Column number '{column}' is not valid.")); } + if width < 0.0 { + return Err(format!("Can not set a negative width: {width}")); + } let cols = &mut self.cols; let mut col = Col { min: column, diff --git a/webapp/src/components/worksheet.tsx b/webapp/src/components/worksheet.tsx index f0c106e..397aabc 100644 --- a/webapp/src/components/worksheet.tsx +++ b/webapp/src/components/worksheet.tsx @@ -104,10 +104,16 @@ function Worksheet(props: { editor: editor, }, onColumnWidthChanges(sheet, column, width) { + if (width < 0) { + return; + } model.setColumnWidth(sheet, column, width); worksheetCanvas.current?.renderSheet(); }, onRowHeightChanges(sheet, row, height) { + if (height < 0) { + return; + } model.setRowHeight(sheet, row, height); worksheetCanvas.current?.renderSheet(); },