Changed unbounded strcpys to safer function.

This commit is contained in:
John McNamara 2016-07-13 22:48:39 +01:00
parent 53f087d6b3
commit abd5a0df1c
4 changed files with 66 additions and 61 deletions

View File

@ -271,6 +271,10 @@ msvc2010_snprintf(char *str, size_t size, const char *format, ...)
#endif
/* Safer strcpy for fixed width char arrays. */
#define lxw_strcpy(dest, src) \
lxw_snprintf(dest, sizeof(dest), "%s", src)
/* Define the queue.h structs for the formats list. */
STAILQ_HEAD(lxw_formats, lxw_format);

View File

@ -163,8 +163,8 @@ lxw_chart_new(uint8_t type)
chart->cat_axis_position = LXW_CHART_BOTTOM;
chart->val_axis_position = LXW_CHART_LEFT;
strcpy(chart->x_axis->default_num_format, "General");
strcpy(chart->y_axis->default_num_format, "General");
lxw_strcpy(chart->x_axis->default_num_format, "General");
lxw_strcpy(chart->y_axis->default_num_format, "General");
chart->x_axis->default_major_gridlines = LXW_FALSE;
chart->y_axis->default_major_gridlines = LXW_TRUE;
@ -2231,7 +2231,7 @@ _chart_initialize_area_chart(lxw_chart *self, uint8_t type)
if (type == LXW_CHART_AREA_STACKED_PERCENT) {
self->grouping = LXW_GROUPING_PERCENTSTACKED;
strcpy((self->y_axis)->default_num_format, "0%");
lxw_strcpy((self->y_axis)->default_num_format, "0%");
self->subtype = LXW_CHART_SUBTYPE_STACKED;
}
@ -2267,7 +2267,7 @@ _chart_initialize_bar_chart(lxw_chart *self, uint8_t type)
if (type == LXW_CHART_BAR_STACKED_PERCENT) {
self->grouping = LXW_GROUPING_PERCENTSTACKED;
strcpy((self->y_axis)->default_num_format, "0%");
lxw_strcpy((self->y_axis)->default_num_format, "0%");
self->has_overlap = LXW_TRUE;
self->subtype = LXW_CHART_SUBTYPE_STACKED;
}
@ -2297,7 +2297,7 @@ _chart_initialize_column_chart(lxw_chart *self, uint8_t type)
if (type == LXW_CHART_COLUMN_STACKED_PERCENT) {
self->grouping = LXW_GROUPING_PERCENTSTACKED;
strcpy((self->y_axis)->default_num_format, "0%");
lxw_strcpy((self->y_axis)->default_num_format, "0%");
self->has_overlap = LXW_TRUE;
self->subtype = LXW_CHART_SUBTYPE_STACKED;
}

View File

@ -499,7 +499,7 @@ _store_defined_name(lxw_workbook *self, const char *name,
RETURN_ON_MEM_ERROR(defined_name, LXW_ERROR_MEMORY_MALLOC_FAILED);
/* Copy the user input string. */
strcpy(name_copy, name);
lxw_strcpy(name_copy, name);
/* Set the worksheet index or -1 for a global defined name. */
defined_name->index = index;
@ -510,7 +510,7 @@ _store_defined_name(lxw_workbook *self, const char *name,
if (tmp_str == NULL) {
/* The name is global. We just store the defined name string. */
strcpy(defined_name->name, name_copy);
lxw_strcpy(defined_name->name, name_copy);
}
else {
/* The name is worksheet local. We need to extract the sheet name
@ -531,7 +531,8 @@ _store_defined_name(lxw_workbook *self, const char *name,
STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
if (strcmp(worksheet_name, worksheet->name) == 0) {
defined_name->index = worksheet->index;
strcpy(defined_name->normalised_sheetname, worksheet_name);
lxw_strcpy(defined_name->normalised_sheetname,
worksheet_name);
}
}
@ -539,16 +540,16 @@ _store_defined_name(lxw_workbook *self, const char *name,
if (defined_name->index == -1)
goto mem_error;
strcpy(defined_name->name, tmp_str);
lxw_strcpy(defined_name->name, tmp_str);
}
/* Print titles and repeat title pass in the name used for App.xml. */
if (app_name) {
strcpy(defined_name->app_name, app_name);
strcpy(defined_name->normalised_sheetname, app_name);
lxw_strcpy(defined_name->app_name, app_name);
lxw_strcpy(defined_name->normalised_sheetname, app_name);
}
else {
strcpy(defined_name->app_name, name);
lxw_strcpy(defined_name->app_name, name);
}
/* We need to normalize the defined names for sorting. This involves
@ -556,18 +557,18 @@ _store_defined_name(lxw_workbook *self, const char *name,
tmp_str = strstr(name_copy, "_xlnm.");
if (tmp_str)
strcpy(defined_name->normalised_name, defined_name->name + 6);
lxw_strcpy(defined_name->normalised_name, defined_name->name + 6);
else
strcpy(defined_name->normalised_name, defined_name->name);
lxw_strcpy(defined_name->normalised_name, defined_name->name);
lxw_str_tolower(defined_name->normalised_name);
lxw_str_tolower(defined_name->normalised_sheetname);
/* Strip leading "=" from the formula. */
if (formula[0] == '=')
strcpy(defined_name->formula, formula + 1);
lxw_strcpy(defined_name->formula, formula + 1);
else
strcpy(defined_name->formula, formula);
lxw_strcpy(defined_name->formula, formula);
/* We add the defined name to the list in sorted order. */
list_defined_name = TAILQ_FIRST(self->defined_names);

View File

@ -972,58 +972,58 @@ _worksheet_write_freeze_panes(lxw_worksheet *self)
/* Set the active pane. */
if (row && col) {
strcpy(active_pane, "bottomRight");
lxw_strcpy(active_pane, "bottomRight");
lxw_rowcol_to_cell(row_cell, row, 0);
lxw_rowcol_to_cell(col_cell, 0, col);
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "topRight");
strcpy(selection->active_cell, col_cell);
strcpy(selection->sqref, col_cell);
lxw_strcpy(selection->pane, "topRight");
lxw_strcpy(selection->active_cell, col_cell);
lxw_strcpy(selection->sqref, col_cell);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "bottomLeft");
strcpy(selection->active_cell, row_cell);
strcpy(selection->sqref, row_cell);
lxw_strcpy(selection->pane, "bottomLeft");
lxw_strcpy(selection->active_cell, row_cell);
lxw_strcpy(selection->sqref, row_cell);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "bottomRight");
strcpy(selection->active_cell, user_selection->active_cell);
strcpy(selection->sqref, user_selection->sqref);
lxw_strcpy(selection->pane, "bottomRight");
lxw_strcpy(selection->active_cell, user_selection->active_cell);
lxw_strcpy(selection->sqref, user_selection->sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
}
else if (col) {
strcpy(active_pane, "topRight");
lxw_strcpy(active_pane, "topRight");
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "topRight");
strcpy(selection->active_cell, user_selection->active_cell);
strcpy(selection->sqref, user_selection->sqref);
lxw_strcpy(selection->pane, "topRight");
lxw_strcpy(selection->active_cell, user_selection->active_cell);
lxw_strcpy(selection->sqref, user_selection->sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
}
else {
strcpy(active_pane, "bottomLeft");
lxw_strcpy(active_pane, "bottomLeft");
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "bottomLeft");
strcpy(selection->active_cell, user_selection->active_cell);
strcpy(selection->sqref, user_selection->sqref);
lxw_strcpy(selection->pane, "bottomLeft");
lxw_strcpy(selection->active_cell, user_selection->active_cell);
lxw_strcpy(selection->sqref, user_selection->sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
@ -1141,64 +1141,64 @@ _worksheet_write_split_panes(lxw_worksheet *self)
/* If there is no selection set the active cell to the top left cell. */
if (!has_selection) {
strcpy(user_selection->active_cell, top_left_cell);
strcpy(user_selection->sqref, top_left_cell);
lxw_strcpy(user_selection->active_cell, top_left_cell);
lxw_strcpy(user_selection->sqref, top_left_cell);
}
/* Set the active pane. */
if (y_split > 0.0 && x_split > 0.0) {
strcpy(active_pane, "bottomRight");
lxw_strcpy(active_pane, "bottomRight");
lxw_rowcol_to_cell(row_cell, top_row, 0);
lxw_rowcol_to_cell(col_cell, 0, left_col);
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "topRight");
strcpy(selection->active_cell, col_cell);
strcpy(selection->sqref, col_cell);
lxw_strcpy(selection->pane, "topRight");
lxw_strcpy(selection->active_cell, col_cell);
lxw_strcpy(selection->sqref, col_cell);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "bottomLeft");
strcpy(selection->active_cell, row_cell);
strcpy(selection->sqref, row_cell);
lxw_strcpy(selection->pane, "bottomLeft");
lxw_strcpy(selection->active_cell, row_cell);
lxw_strcpy(selection->sqref, row_cell);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "bottomRight");
strcpy(selection->active_cell, user_selection->active_cell);
strcpy(selection->sqref, user_selection->sqref);
lxw_strcpy(selection->pane, "bottomRight");
lxw_strcpy(selection->active_cell, user_selection->active_cell);
lxw_strcpy(selection->sqref, user_selection->sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
}
else if (x_split > 0.0) {
strcpy(active_pane, "topRight");
lxw_strcpy(active_pane, "topRight");
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "topRight");
strcpy(selection->active_cell, user_selection->active_cell);
strcpy(selection->sqref, user_selection->sqref);
lxw_strcpy(selection->pane, "topRight");
lxw_strcpy(selection->active_cell, user_selection->active_cell);
lxw_strcpy(selection->sqref, user_selection->sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
}
else {
strcpy(active_pane, "bottomLeft");
lxw_strcpy(active_pane, "bottomLeft");
selection = calloc(1, sizeof(lxw_selection));
if (selection) {
strcpy(selection->pane, "bottomLeft");
strcpy(selection->active_cell, user_selection->active_cell);
strcpy(selection->sqref, user_selection->sqref);
lxw_strcpy(selection->pane, "bottomLeft");
lxw_strcpy(selection->active_cell, user_selection->active_cell);
lxw_strcpy(selection->sqref, user_selection->sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
@ -3779,8 +3779,8 @@ worksheet_write_url_opt(lxw_worksheet *self,
case ('^'):
case ('{'):
case ('}'):
sprintf(url_external + strlen(url_external), "%%%2x",
url_copy[i]);
lxw_snprintf(url_external + strlen(url_external),
sizeof("%xx"), "%%%2x", url_copy[i]);
break;
default:
url_external[strlen(url_external)] = url_copy[i];
@ -4297,9 +4297,9 @@ worksheet_set_selection(lxw_worksheet *self,
else
lxw_rowcol_to_range(sqref, first_row, first_col, last_row, last_col);
strcpy(selection->pane, "");
strcpy(selection->active_cell, active_cell);
strcpy(selection->sqref, sqref);
lxw_strcpy(selection->pane, "");
lxw_strcpy(selection->active_cell, active_cell);
lxw_strcpy(selection->sqref, sqref);
STAILQ_INSERT_TAIL(self->selections, selection, list_pointers);
}
@ -4451,7 +4451,7 @@ worksheet_set_header_opt(lxw_worksheet *self, const char *string,
if (strlen(string) >= LXW_HEADER_FOOTER_MAX)
return LXW_ERROR_255_STRING_LENGTH_EXCEEDED;
strcpy(self->header, string);
lxw_strcpy(self->header, string);
self->header_footer_changed = 1;
return LXW_NO_ERROR;
@ -4475,7 +4475,7 @@ worksheet_set_footer_opt(lxw_worksheet *self, const char *string,
if (strlen(string) >= LXW_HEADER_FOOTER_MAX)
return LXW_ERROR_255_STRING_LENGTH_EXCEEDED;
strcpy(self->footer, string);
lxw_strcpy(self->footer, string);
self->header_footer_changed = 1;
return LXW_NO_ERROR;