Skip to content

Commit

Permalink
worksheet: add range check to worksheet_set_selection()
Browse files Browse the repository at this point in the history
Issue #447
  • Loading branch information
jmcnamara committed Jun 5, 2024
1 parent c89c551 commit f483e65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions include/xlsxwriter/worksheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -4579,9 +4579,9 @@ void worksheet_split_panes_opt(lxw_worksheet *worksheet,
* @endcode
*
*/
void worksheet_set_selection(lxw_worksheet *worksheet,
lxw_row_t first_row, lxw_col_t first_col,
lxw_row_t last_row, lxw_col_t last_col);
lxw_error worksheet_set_selection(lxw_worksheet *worksheet,
lxw_row_t first_row, lxw_col_t first_col,
lxw_row_t last_row, lxw_col_t last_col);

/**
* @brief Set the first visible cell at the top left of a worksheet.
Expand Down
20 changes: 16 additions & 4 deletions src/worksheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -9545,27 +9545,37 @@ worksheet_hide(lxw_worksheet *self)
/*
* Set which cell or cells are selected in a worksheet.
*/
void
lxw_error
worksheet_set_selection(lxw_worksheet *self,
lxw_row_t first_row, lxw_col_t first_col,
lxw_row_t last_row, lxw_col_t last_col)
{
lxw_selection *selection;
lxw_row_t tmp_row;
lxw_col_t tmp_col;
lxw_error err;
char active_cell[LXW_MAX_CELL_RANGE_LENGTH];
char sqref[LXW_MAX_CELL_RANGE_LENGTH];

/* Only allow selection to be set once to avoid freeing/re-creating it. */
if (!STAILQ_EMPTY(self->selections))
return;
return LXW_ERROR_PARAMETER_VALIDATION;

/* Excel doesn't set a selection for cell A1 since it is the default. */
if (first_row == 0 && first_col == 0 && last_row == 0 && last_col == 0)
return;
return LXW_NO_ERROR;

selection = calloc(1, sizeof(lxw_selection));
RETURN_VOID_ON_MEM_ERROR(selection);
RETURN_ON_MEM_ERROR(selection, LXW_ERROR_MEMORY_MALLOC_FAILED);

/* Check that row and col are valid without storing. */
err = _check_dimensions(self, first_row, first_col, LXW_TRUE, LXW_TRUE);
if (err)
return err;

err = _check_dimensions(self, last_row, last_col, LXW_TRUE, LXW_TRUE);
if (err)
return err;

/* Set the cell range selection. Do this before swapping max/min to */
/* allow the selection direction to be reversed. */
Expand Down Expand Up @@ -9595,6 +9605,8 @@ worksheet_set_selection(lxw_worksheet *self,
lxw_strcpy(selection->sqref, sqref);

STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);

return LXW_NO_ERROR;
}

/*
Expand Down

0 comments on commit f483e65

Please sign in to comment.