Fixes for Coverity issues.

This commit is contained in:
John McNamara 2017-01-07 00:54:13 +00:00
parent 62f83785e0
commit 3ad3bf8a82
6 changed files with 199 additions and 84 deletions

View File

@ -99,15 +99,30 @@ strip:
# Run a coverity static analysis.
coverity:
$(Q)$(MAKE) -C third_party/minizip
ifndef USE_STANDARD_TMPFILE
$(Q)$(MAKE) -C third_party/tmpfileplus
endif
$(Q)$(MAKE) -C src clean
$(Q)rm -f lib/*
$(Q)rm -rf cov-int
$(Q)rm -f libxlsxwriter-coverity.tgz
$(Q)../cov-analysis-macosx-7.7.0.4/bin/cov-build --dir cov-int make -C src libxlsxwriter.a
$(Q)../../cov-analysis-linux64-8.7.0/bin/cov-build --dir cov-int make -C src libxlsxwriter.a
$(Q)tar -czf libxlsxwriter-coverity.tgz cov-int
$(Q)$(MAKE) -C src clean
$(Q)rm -f lib/*
# Run a scan-build static analysis.
scan_build:
$(Q)$(MAKE) -C third_party/minizip
ifndef USE_STANDARD_TMPFILE
$(Q)$(MAKE) -C third_party/tmpfileplus
endif
$(Q)$(MAKE) -C src clean
$(Q)rm -f lib/*
$(Q)scan-build make -C src libxlsxwriter.a
$(Q)$(MAKE) -C src clean
$(Q)rm -f lib/*
spellcheck:
$(Q)for f in docs/src/*.dox; do aspell --lang=en_US --check $$f; done
$(Q)for f in include/xlsxwriter/*.h; do aspell --lang=en_US --check $$f; done

View File

@ -1392,7 +1392,6 @@ void chart_axis_set_max(lxw_chart_axis *axis, double max);
*/
void chart_axis_set_log_base(lxw_chart_axis *axis, uint16_t log_base);
/**
* @brief Turn on/off the major gridlines for an axis.
*

View File

@ -1506,7 +1506,7 @@ _chart_write_a_ln(lxw_chart *self, lxw_chart_line *line)
{
struct xml_attribute_list attributes;
struct xml_attribute *attribute;
float width_flt = line->width;
float width_flt;
uint32_t width_int;
LXW_INIT_ATTRIBUTES();

View File

@ -37,7 +37,7 @@ STATIC uint8_t _add_file_to_zip(lxw_packager *self, FILE * file,
#undef SLIST_ENTRY
#include <windows.h>
#include "../third_party/iowin32.h"
#include "../third_party/minizip/iowin32.h"
zipFile
_open_zipfile_win32(const char *filename)
@ -329,16 +329,24 @@ _write_app_file(lxw_packager *self)
lxw_workbook *workbook = self->workbook;
lxw_worksheet *worksheet;
lxw_defined_name *defined_name;
lxw_app *app = lxw_app_new();
lxw_app *app;
uint16_t named_range_count = 0;
char *autofilter;
char *has_range;
char number[LXW_ATTR_32] = { 0 };
int err;
int err = LXW_NO_ERROR;
app = lxw_app_new();
if (!app) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
app->file = lxw_tmpfile(self->tmpdir);
if (!app->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!app->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
lxw_snprintf(number, LXW_ATTR_32, "%d", self->workbook->num_sheets);
@ -373,13 +381,13 @@ _write_app_file(lxw_packager *self)
lxw_app_assemble_xml_file(app);
err = _add_file_to_zip(self, app->file, "docProps/app.xml");
RETURN_ON_ERROR(err);
fclose(app->file);
mem_error:
lxw_app_free(app);
return 0;
return err;
}
/*
@ -388,25 +396,32 @@ _write_app_file(lxw_packager *self)
STATIC uint8_t
_write_core_file(lxw_packager *self)
{
int err = LXW_NO_ERROR;
lxw_core *core = lxw_core_new();
int err;
if (!core) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
core->file = lxw_tmpfile(self->tmpdir);
if (!core->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!core->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
core->properties = self->workbook->properties;
lxw_core_assemble_xml_file(core);
err = _add_file_to_zip(self, core->file, "docProps/core.xml");
RETURN_ON_ERROR(err);
fclose(core->file);
mem_error:
lxw_core_free(core);
return 0;
return err;
}
/*
@ -416,29 +431,34 @@ STATIC uint8_t
_write_custom_file(lxw_packager *self)
{
lxw_custom *custom;
int err;
int err = LXW_NO_ERROR;
if (STAILQ_EMPTY(self->workbook->custom_properties))
return 0;
custom = lxw_custom_new();
if (!custom) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
custom->file = lxw_tmpfile(self->tmpdir);
if (!custom->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!custom->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
custom->custom_properties = self->workbook->custom_properties;
lxw_custom_assemble_xml_file(custom);
err = _add_file_to_zip(self, custom->file, "docProps/custom.xml");
RETURN_ON_ERROR(err);
fclose(custom->file);
mem_error:
lxw_custom_free(custom);
return 0;
return err;
}
/*
@ -447,23 +467,30 @@ _write_custom_file(lxw_packager *self)
STATIC uint8_t
_write_theme_file(lxw_packager *self)
{
int err = LXW_NO_ERROR;
lxw_theme *theme = lxw_theme_new();
int err;
if (!theme) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
theme->file = lxw_tmpfile(self->tmpdir);
if (!theme->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!theme->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
lxw_theme_assemble_xml_file(theme);
err = _add_file_to_zip(self, theme->file, "xl/theme/theme1.xml");
RETURN_ON_ERROR(err);
fclose(theme->file);
mem_error:
lxw_theme_free(theme);
return 0;
return err;
}
/*
@ -474,13 +501,24 @@ _write_styles_file(lxw_packager *self)
{
lxw_styles *styles = lxw_styles_new();
lxw_hash_element *hash_element;
int err;
int err = LXW_NO_ERROR;
if (!styles) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
/* Copy the unique and in-use formats from the workbook to the styles
* xf_format list. */
LXW_FOREACH_ORDERED(hash_element, self->workbook->used_xf_formats) {
lxw_format *workbook_format = (lxw_format *) hash_element->value;
lxw_format *style_format = lxw_format_new();
if (!style_format) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
memcpy(style_format, workbook_format, sizeof(lxw_format));
STAILQ_INSERT_TAIL(styles->xf_formats, style_format, list_pointers);
}
@ -492,19 +530,21 @@ _write_styles_file(lxw_packager *self)
styles->xf_count = self->workbook->used_xf_formats->unique_count;
styles->file = lxw_tmpfile(self->tmpdir);
if (!styles->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!styles->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
lxw_styles_assemble_xml_file(styles);
err = _add_file_to_zip(self, styles->file, "xl/styles.xml");
RETURN_ON_ERROR(err);
fclose(styles->file);
mem_error:
lxw_styles_free(styles);
return 0;
return err;
}
/*
@ -518,11 +558,18 @@ _write_content_types_file(lxw_packager *self)
lxw_worksheet *worksheet;
char filename[LXW_MAX_ATTRIBUTE_LENGTH] = { 0 };
uint16_t index = 1;
int err;
int err = LXW_NO_ERROR;
if (!content_types) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
content_types->file = lxw_tmpfile(self->tmpdir);
if (!content_types->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!content_types->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
if (workbook->has_png)
lxw_ct_add_default(content_types, "png", "image/png");
@ -560,13 +607,13 @@ _write_content_types_file(lxw_packager *self)
lxw_content_types_assemble_xml_file(content_types);
err = _add_file_to_zip(self, content_types->file, "[Content_Types].xml");
RETURN_ON_ERROR(err);
fclose(content_types->file);
mem_error:
lxw_content_types_free(content_types);
return 0;
return err;
}
/*
@ -580,11 +627,18 @@ _write_workbook_rels_file(lxw_packager *self)
lxw_worksheet *worksheet;
char sheetname[LXW_FILENAME_LENGTH] = { 0 };
uint16_t index = 1;
int err;
int err = LXW_NO_ERROR;
if (!rels) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
rels->file = lxw_tmpfile(self->tmpdir);
if (!rels->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!rels->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
STAILQ_FOREACH(worksheet, workbook->worksheets, list_pointers) {
lxw_snprintf(sheetname, LXW_FILENAME_LENGTH, "worksheets/sheet%d.xml",
@ -602,12 +656,13 @@ _write_workbook_rels_file(lxw_packager *self)
lxw_relationships_assemble_xml_file(rels);
err = _add_file_to_zip(self, rels->file, "xl/_rels/workbook.xml.rels");
RETURN_ON_ERROR(err);
fclose(rels->file);
mem_error:
lxw_free_relationships(rels);
return 0;
return err;
}
/*
@ -634,9 +689,12 @@ _write_worksheet_rels_file(lxw_packager *self)
continue;
rels = lxw_relationships_new();
rels->file = lxw_tmpfile(self->tmpdir);
if (!rels->file)
if (!rels->file) {
lxw_free_relationships(rels);
return LXW_ERROR_CREATING_TMPFILE;
}
STAILQ_FOREACH(rel, worksheet->external_hyperlinks, list_pointers) {
lxw_add_worksheet_relationship(rels, rel->type, rel->target,
@ -654,10 +712,11 @@ _write_worksheet_rels_file(lxw_packager *self)
lxw_relationships_assemble_xml_file(rels);
err = _add_file_to_zip(self, rels->file, sheetname);
RETURN_ON_ERROR(err);
fclose(rels->file);
lxw_free_relationships(rels);
RETURN_ON_ERROR(err);
}
return 0;
@ -684,9 +743,12 @@ _write_drawing_rels_file(lxw_packager *self)
continue;
rels = lxw_relationships_new();
rels->file = lxw_tmpfile(self->tmpdir);
if (!rels->file)
if (!rels->file) {
lxw_free_relationships(rels);
return LXW_ERROR_CREATING_TMPFILE;
}
STAILQ_FOREACH(rel, worksheet->drawing_links, list_pointers) {
lxw_add_worksheet_relationship(rels, rel->type, rel->target,
@ -700,10 +762,11 @@ _write_drawing_rels_file(lxw_packager *self)
lxw_relationships_assemble_xml_file(rels);
err = _add_file_to_zip(self, rels->file, sheetname);
RETURN_ON_ERROR(err);
fclose(rels->file);
lxw_free_relationships(rels);
RETURN_ON_ERROR(err);
}
return 0;
@ -716,11 +779,18 @@ STATIC uint8_t
_write_root_rels_file(lxw_packager *self)
{
lxw_relationships *rels = lxw_relationships_new();
int err;
int err = LXW_NO_ERROR;
if (!rels) {
err = LXW_ERROR_MEMORY_MALLOC_FAILED;
goto mem_error;
}
rels->file = lxw_tmpfile(self->tmpdir);
if (!rels->file)
return LXW_ERROR_CREATING_TMPFILE;
if (!rels->file) {
err = LXW_ERROR_CREATING_TMPFILE;
goto mem_error;
}
lxw_add_document_relationship(rels, "/officeDocument", "xl/workbook.xml");
@ -739,13 +809,13 @@ _write_root_rels_file(lxw_packager *self)
lxw_relationships_assemble_xml_file(rels);
err = _add_file_to_zip(self, rels->file, "_rels/.rels");
RETURN_ON_ERROR(err);
fclose(rels->file);
mem_error:
lxw_free_relationships(rels);
return 0;
return err;
}
/*****************************************************************************

View File

@ -243,7 +243,10 @@ lxw_name_to_row(const char *row_str)
if (p)
row_num = atoi(p);
return row_num - 1;
if (row_num)
return row_num - 1;
else
return 0;
}
/*

View File

@ -137,11 +137,7 @@ lxw_worksheet_new(lxw_worksheet_init_data *init_data)
if (init_data && init_data->optimize) {
FILE *tmpfile;
if (init_data)
tmpfile = lxw_tmpfile(init_data->tmpdir);
else
tmpfile = lxw_tmpfile(NULL);
tmpfile = lxw_tmpfile(init_data->tmpdir);
if (!tmpfile) {
LXW_ERROR("Error creating tmpfile() for worksheet in "
"'constant_memory' mode.");
@ -2067,11 +2063,14 @@ _process_png(lxw_image_options *image_options)
uint32_t height = 0;
double x_dpi = 96;
double y_dpi = 96;
int fseek_err;
FILE *stream = image_options->stream;
/* Skip another 4 bytes to the end of the PNG header. */
fseek(stream, 4, SEEK_CUR);
fseek_err = fseek(stream, 4, SEEK_CUR);
if (fseek_err)
goto file_error;
while (!feof(stream)) {
@ -2131,17 +2130,16 @@ _process_png(lxw_image_options *image_options)
if (memcmp(type, "IEND", 4) == 0)
break;
if (!feof(stream))
fseek(stream, offset, SEEK_CUR);
if (!feof(stream)) {
fseek_err = fseek(stream, offset, SEEK_CUR);
if (fseek_err)
goto file_error;
}
}
/* Ensure that we read some valid data from the file. */
if (width == 0) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"no size data found in file: %s.",
image_options->filename);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
if (width == 0)
goto file_error;
/* Set the image metadata. */
image_options->image_type = LXW_IMAGE_PNG;
@ -2152,6 +2150,13 @@ _process_png(lxw_image_options *image_options)
image_options->extension = lxw_strdup("png");
return LXW_NO_ERROR;
file_error:
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"no size data found in file: %s.",
image_options->filename);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
/*
@ -2167,11 +2172,14 @@ _process_jpeg(lxw_image_options *image_options)
uint16_t height = 0;
double x_dpi = 96;
double y_dpi = 96;
int fseek_err;
FILE *stream = image_options->stream;
/* Read back 2 bytes to the end of the initial 0xFFD8 marker. */
fseek(stream, -2, SEEK_CUR);
fseek_err = fseek(stream, -2, SEEK_CUR);
if (fseek_err)
goto file_error;
/* Search through the image data to read the height and width in the */
/* 0xFFC0/C2 element. Also read the DPI in the 0xFFE0 element. */
@ -2193,7 +2201,9 @@ _process_jpeg(lxw_image_options *image_options)
if (marker == 0xFFC0 || marker == 0xFFC2) {
/* Skip 1 byte to height and width. */
fseek(stream, 1, SEEK_CUR);
fseek_err = fseek(stream, 1, SEEK_CUR);
if (fseek_err)
goto file_error;
if (fread(&height, sizeof(height), 1, stream) < 1)
break;
@ -2212,7 +2222,9 @@ _process_jpeg(lxw_image_options *image_options)
uint16_t y_density = 0;
uint8_t units = 1;
fseek(stream, 7, SEEK_CUR);
fseek_err = fseek(stream, 7, SEEK_CUR);
if (fseek_err)
goto file_error;
if (fread(&units, sizeof(units), 1, stream) < 1)
break;
@ -2242,17 +2254,16 @@ _process_jpeg(lxw_image_options *image_options)
if (marker == 0xFFDA)
break;
if (!feof(stream))
fseek(stream, offset, SEEK_CUR);
if (!feof(stream)) {
fseek_err = fseek(stream, offset, SEEK_CUR);
if (fseek_err)
goto file_error;
}
}
/* Ensure that we read some valid data from the file. */
if (width == 0) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"no size data found in file: %s.",
image_options->filename);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
if (width == 0)
goto file_error;
/* Set the image metadata. */
image_options->image_type = LXW_IMAGE_JPEG;
@ -2263,6 +2274,13 @@ _process_jpeg(lxw_image_options *image_options)
image_options->extension = lxw_strdup("jpeg");
return LXW_NO_ERROR;
file_error:
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"no size data found in file: %s.",
image_options->filename);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
/*
@ -2275,11 +2293,14 @@ _process_bmp(lxw_image_options *image_options)
uint32_t height = 0;
double x_dpi = 96;
double y_dpi = 96;
int fseek_err;
FILE *stream = image_options->stream;
/* Skip another 14 bytes to the start of the BMP height/width. */
fseek(stream, 14, SEEK_CUR);
fseek_err = fseek(stream, 14, SEEK_CUR);
if (fseek_err)
goto file_error;
if (fread(&width, sizeof(width), 1, stream) < 1)
width = 0;
@ -2288,12 +2309,8 @@ _process_bmp(lxw_image_options *image_options)
height = 0;
/* Ensure that we read some valid data from the file. */
if (width == 0) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"no size data found in file: %s.",
image_options->filename);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
if (width == 0)
goto file_error;
/* Set the image metadata. */
image_options->image_type = LXW_IMAGE_BMP;
@ -2304,6 +2321,13 @@ _process_bmp(lxw_image_options *image_options)
image_options->extension = lxw_strdup("bmp");
return LXW_NO_ERROR;
file_error:
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"no size data found in file: %s.",
image_options->filename);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
/*
@ -4861,12 +4885,16 @@ worksheet_insert_image_opt(lxw_worksheet *self,
if (!short_name) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"couldn't get basename for file: %s.", filename);
fclose(image_stream);
return LXW_ERROR_PARAMETER_VALIDATION;
}
/* Create a new object to hold the image options. */
options = calloc(1, sizeof(lxw_image_options));
RETURN_ON_MEM_ERROR(options, LXW_ERROR_MEMORY_MALLOC_FAILED);
if (!options) {
fclose(image_stream);
return LXW_ERROR_MEMORY_MALLOC_FAILED;
}
if (user_options) {
memcpy(options, user_options, sizeof(lxw_image_options));