From 4e2f21fb059a7c92c281cee8a80cb536dc0b1ef1 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 16 Apr 2018 22:33:05 +0300 Subject: [PATCH] [F] Fixed compiler warnings --- src/cell.c | 30 +++++++++++++++--------------- src/cell.h | 4 ++-- src/fort_impl.h | 4 ++++ src/options.c | 20 ++++++++++++++++++++ src/options.h | 20 ++++++++++---------- src/row.c | 12 ++++++------ src/row.h | 2 +- src/string_buffer.c | 4 +++- 8 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/cell.c b/src/cell.c index 40419d3..057bd1c 100644 --- a/src/cell.c +++ b/src/cell.c @@ -36,7 +36,7 @@ void destroy_cell(fort_cell_t *cell) F_FREE(cell); } -int hint_width_cell(const fort_cell_t *cell, const context_t *context) +unsigned int hint_width_cell(const fort_cell_t *cell, const context_t *context) { /* todo: * At the moment min width includes paddings. Maybe it is better that min width weren't include @@ -45,23 +45,23 @@ int hint_width_cell(const fort_cell_t *cell, const context_t *context) assert(cell); assert(context); - int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); - int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); - int result = cell_padding_left + cell_padding_right; + unsigned int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); + unsigned int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); + unsigned int result = cell_padding_left + cell_padding_right; if (cell->str_buffer && cell->str_buffer->str.data) { result += buffer_text_width(cell->str_buffer); } - result = MAX(result, get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_MIN_WIDTH)); + result = MAX(result, (unsigned)get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_MIN_WIDTH)); return result; } -int hint_height_cell(const fort_cell_t *cell, const context_t *context) +unsigned int hint_height_cell(const fort_cell_t *cell, const context_t *context) { assert(cell); assert(context); - int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); - int cell_padding_bottom = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_BOTTOM_PADDING); - int cell_empty_string_height = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_EMPTY_STR_HEIGHT); + unsigned int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); + unsigned int cell_padding_bottom = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_BOTTOM_PADDING); + unsigned int cell_empty_string_height = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_EMPTY_STR_HEIGHT); int result = cell_padding_top + cell_padding_bottom; if (cell->str_buffer && cell->str_buffer->str.data) { size_t text_height = buffer_text_height(cell->str_buffer); @@ -105,9 +105,9 @@ int cell_printf(fort_cell_t *cell, size_t row, char *buf, size_t buf_len, const return -1; } - int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); - int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); - int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); + unsigned int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); + unsigned int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); + unsigned int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); if (row >= hint_height_cell(cell, context) || row < cell_padding_top @@ -147,9 +147,9 @@ int cell_wprintf(fort_cell_t *cell, size_t row, wchar_t *buf, size_t buf_len, co return -1; } - int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); - int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); - int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); + unsigned int cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); + unsigned int cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); + unsigned int cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); if (row >= hint_height_cell(cell, context) || row < cell_padding_top diff --git a/src/cell.h b/src/cell.h index 563d80e..45bc0b9 100644 --- a/src/cell.h +++ b/src/cell.h @@ -13,8 +13,8 @@ fort_cell_t * create_cell(void); void destroy_cell(fort_cell_t *cell); -int hint_width_cell(const fort_cell_t *cell, const context_t *context); -int hint_height_cell(const fort_cell_t *cell, const context_t *context); +unsigned int hint_width_cell(const fort_cell_t *cell, const context_t *context); +unsigned int hint_height_cell(const fort_cell_t *cell, const context_t *context); /* diff --git a/src/fort_impl.h b/src/fort_impl.h index 0da5f98..89d10d9 100644 --- a/src/fort_impl.h +++ b/src/fort_impl.h @@ -127,4 +127,8 @@ int wsnprint_n_chars(wchar_t *buf, size_t length, size_t n, wchar_t ch); written += tmp; \ } while(0) + +#define CHECK_NOT_NEGATIVE(x) \ + do { if (x < 0) goto fort_fail; } while (0) + #endif /* FORT_IMPL_H */ diff --git a/src/options.c b/src/options.c index a6fbfa4..ed3426e 100644 --- a/src/options.c +++ b/src/options.c @@ -156,24 +156,33 @@ static fort_status_t set_cell_option_impl(fort_cell_options_t *opt, uint32_t opt OPTION_SET(opt->options, option); if (OPTION_IS_SET(option, FT_COPT_MIN_WIDTH)) { + CHECK_NOT_NEGATIVE(value); opt->col_min_width = value; } else if (OPTION_IS_SET(option, FT_COPT_TEXT_ALIGN)) { opt->align = (enum ft_text_alignment)value; } else if (OPTION_IS_SET(option, FT_COPT_TOP_PADDING)) { + CHECK_NOT_NEGATIVE(value); opt->cell_padding_top = value; } else if (OPTION_IS_SET(option, FT_COPT_BOTTOM_PADDING)) { + CHECK_NOT_NEGATIVE(value); opt->cell_padding_bottom = value; } else if (OPTION_IS_SET(option, FT_COPT_LEFT_PADDING)) { + CHECK_NOT_NEGATIVE(value); opt->cell_padding_left = value; } else if (OPTION_IS_SET(option, FT_COPT_RIGHT_PADDING)) { + CHECK_NOT_NEGATIVE(value); opt->cell_padding_right = value; } else if (OPTION_IS_SET(option, FT_COPT_EMPTY_STR_HEIGHT)) { + CHECK_NOT_NEGATIVE(value); opt->cell_empty_string_height = value; } else if (OPTION_IS_SET(option, FT_COPT_ROW_TYPE)) { opt->row_type = (enum ft_row_type)value; } return FT_SUCCESS; + +fort_fail: + return FT_EINVAL; } @@ -329,6 +338,7 @@ fort_entire_table_options_t g_entire_table_options = { static fort_status_t set_entire_table_option_internal(fort_entire_table_options_t *options, uint32_t option, int value) { assert(options); + CHECK_NOT_NEGATIVE(value); if (OPTION_IS_SET(option, FT_TOPT_LEFT_MARGIN)) { options->left_margin = value; } else if (OPTION_IS_SET(option, FT_TOPT_TOP_MARGIN)) { @@ -341,6 +351,9 @@ static fort_status_t set_entire_table_option_internal(fort_entire_table_options_ return FT_EINVAL; } return FT_SUCCESS; + +fort_fail: + return FT_EINVAL; } fort_status_t set_entire_table_option(fort_table_options_t *table_options, uint32_t option, int value) @@ -361,6 +374,13 @@ fort_table_options_t g_table_options = { /* border_style */ BASIC_STYLE, NULL, /* cell_options */ + /* entire_table_options */ + { + 0, /* left_margin */ + 0, /* top_margin */ + 0, /* right_margin */ + 0 /* bottom_margin */ + } }; diff --git a/src/options.h b/src/options.h index 72e8e0f..b08a1d9 100644 --- a/src/options.h +++ b/src/options.h @@ -29,13 +29,13 @@ struct fort_cell_options unsigned cell_row; unsigned cell_col; uint32_t options; - int col_min_width; + unsigned int col_min_width; enum ft_text_alignment align; - int cell_padding_top; - int cell_padding_bottom; - int cell_padding_left; - int cell_padding_right; - int cell_empty_string_height; + unsigned int cell_padding_top; + unsigned int cell_padding_bottom; + unsigned int cell_padding_left; + unsigned int cell_padding_right; + unsigned int cell_empty_string_height; enum ft_row_type row_type; }; @@ -125,10 +125,10 @@ extern struct fort_border_style FORT_EMPTY_STYLE; struct fort_entire_table_options { - int left_margin; - int top_margin; - int right_margin; - int bottom_margin; + unsigned int left_margin; + unsigned int top_margin; + unsigned int right_margin; + unsigned int bottom_margin; }; typedef struct fort_entire_table_options fort_entire_table_options_t; extern fort_entire_table_options_t g_entire_table_options; diff --git a/src/row.c b/src/row.c index c820a6f..dff3468 100644 --- a/src/row.c +++ b/src/row.c @@ -52,7 +52,7 @@ void destroy_row(fort_row_t *row) -int columns_in_row(const fort_row_t *row) +unsigned int columns_in_row(const fort_row_t *row) { if (row == NULL || row->cells == NULL) return 0; @@ -324,7 +324,7 @@ fort_row_t *create_row_from_string(const char *str) { char *pos = NULL; char *base_pos = NULL; - int number_of_separators = 0; + unsigned int number_of_separators = 0; fort_row_t *row = create_row(); if (row == NULL) @@ -417,11 +417,11 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) int virtual_sz = vsnprintf(buffer->str.cstr, string_buffer_capacity(buffer)/*buffer->str_sz*/, fmt, va); va_end(va); /* If error encountered */ - if (virtual_sz == -1) + if (virtual_sz < 0) goto clear; /* Successful write */ - if (virtual_sz < string_buffer_capacity(buffer)) + if ((size_t)virtual_sz < string_buffer_capacity(buffer)) break; /* Otherwise buffer was too small, so incr. buffer size ant try again. */ @@ -463,7 +463,7 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col if (row == NULL) return -1; - int cols_in_row = columns_in_row(row); + unsigned int cols_in_row = columns_in_row(row); if (cols_in_row > col_width_arr_sz) return -1; @@ -533,7 +533,7 @@ int wsnprintf_row(const fort_row_t *row, wchar_t *buffer, size_t buf_sz, size_t if (row == NULL) return -1; - int cols_in_row = columns_in_row(row); + unsigned int cols_in_row = columns_in_row(row); if (cols_in_row > col_width_arr_sz) return -1; diff --git a/src/row.h b/src/row.h index 4dc3148..bec125d 100644 --- a/src/row.h +++ b/src/row.h @@ -24,7 +24,7 @@ fort_row_t * create_row_from_string(const char *str); fort_row_t* create_row_from_fmt_string(const char* fmt, va_list *va_args); -int columns_in_row(const fort_row_t *row); +unsigned int columns_in_row(const fort_row_t *row); fort_cell_t *get_cell_implementation(fort_row_t *row, size_t col, enum PolicyOnNull policy); fort_cell_t *get_cell(fort_row_t *row, size_t col); diff --git a/src/string_buffer.c b/src/string_buffer.c index 3cc64fa..97f662a 100644 --- a/src/string_buffer.c +++ b/src/string_buffer.c @@ -260,7 +260,9 @@ size_t buffer_text_width(string_buffer_t *buffer) return max_length; int line_width = mk_wcswidth(beg, end - beg); - max_length = MAX(max_length, line_width); + if (line_width < 0) /* For safety */ + line_width = 0; + max_length = MAX(max_length, (size_t)line_width); ++n; }