Skip to content

Commit eab4b65

Browse files
committed
memory: fix invalid string array indexes
See #482
1 parent 21c11a2 commit eab4b65

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/workbook.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,10 @@ _store_defined_name(lxw_workbook *self, const char *name,
737737
/* Remove any worksheet quoting. */
738738
if (worksheet_name[0] == '\'')
739739
worksheet_name++;
740-
if (worksheet_name[strlen(worksheet_name) - 1] == '\'')
740+
if (strlen(worksheet_name) > 0
741+
&& worksheet_name[strlen(worksheet_name) - 1] == '\'') {
741742
worksheet_name[strlen(worksheet_name) - 1] = '\0';
743+
}
742744

743745
/* Search for worksheet name to get the equivalent worksheet index. */
744746
STAILQ_FOREACH(sheet, self->sheets, list_pointers) {
@@ -976,8 +978,9 @@ _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range)
976978
/* Remove any worksheet quoting. */
977979
if (sheetname[0] == '\'')
978980
sheetname++;
979-
if (sheetname[strlen(sheetname) - 1] == '\'')
981+
if (strlen(sheetname) > 0 && sheetname[strlen(sheetname) - 1] == '\'') {
980982
sheetname[strlen(sheetname) - 1] = '\0';
983+
}
981984

982985
/* Check that the sheetname exists. */
983986
if (!workbook_get_worksheet_by_name(self, sheetname)) {

src/worksheet.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8127,16 +8127,25 @@ _store_array_formula(lxw_worksheet *self,
81278127

81288128
/* Copy and trip leading "{=" from formula. */
81298129
if (formula[0] == '{')
8130-
if (formula[1] == '=')
8130+
if (strlen(formula) >= 2 && formula[1] == '=')
81318131
formula_copy = lxw_strdup(formula + 2);
81328132
else
81338133
formula_copy = lxw_strdup(formula + 1);
81348134
else
81358135
formula_copy = lxw_strdup_formula(formula);
81368136

81378137
/* Strip trailing "}" from formula. */
8138-
if (formula_copy[strlen(formula_copy) - 1] == '}')
8138+
if (strlen(formula_copy) > 0
8139+
&& formula_copy[strlen(formula_copy) - 1] == '}') {
81398140
formula_copy[strlen(formula_copy) - 1] = '\0';
8141+
}
8142+
8143+
/* Check for empty formula that started as {=}. */
8144+
if (lxw_str_is_empty(formula_copy)) {
8145+
free(formula_copy);
8146+
free(range);
8147+
return LXW_ERROR_PARAMETER_IS_EMPTY;
8148+
}
81408149

81418150
/* Create a new array formula cell object. */
81428151
cell = _new_array_formula_cell(first_row, first_col,

test/unit/worksheet/test_worksheet_range_returns.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ CTEST(worksheet, bound_checks01) {
105105
err = worksheet_set_column(worksheet, MAX_COL, 6, 17, NULL);
106106
ASSERT_EQUAL(LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE, err);
107107

108+
/* Tests array formula strings. */
109+
err = worksheet_write_array_formula(worksheet, 0, 0, 0, 0, "{", NULL);
110+
ASSERT_EQUAL(LXW_ERROR_PARAMETER_IS_EMPTY, err);
111+
112+
err = worksheet_write_array_formula(worksheet, 0, 0, 0, 0, "}", NULL);
113+
ASSERT_EQUAL(LXW_ERROR_PARAMETER_IS_EMPTY, err);
114+
115+
err = worksheet_write_array_formula(worksheet, 0, 0, 0, 0, "{}", NULL);
116+
ASSERT_EQUAL(LXW_ERROR_PARAMETER_IS_EMPTY, err);
117+
118+
err = worksheet_write_array_formula(worksheet, 0, 0, 0, 0, "{=}", NULL);
119+
ASSERT_EQUAL(LXW_ERROR_PARAMETER_IS_EMPTY, err);
120+
108121
lxw_worksheet_assemble_xml_file(worksheet);
109122

110123
lxw_worksheet_free(worksheet);

0 commit comments

Comments
 (0)