FIX: Prevent negative column width, row height (#167)

* Prevent negative column width / row height in rust

* prevent in front-end
This commit is contained in:
Sinan Yumurtacı
2024-12-10 17:07:06 -06:00
committed by GitHub
parent 56915ce0b1
commit eee40c1b9a
5 changed files with 44 additions and 2 deletions

View File

@@ -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)

View File

@@ -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));
}

View File

@@ -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]

View File

@@ -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,

View File

@@ -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();
},