From 44e734ead915640275cc1cd5c94ff0b905e8e5ca Mon Sep 17 00:00:00 2001 From: seleznevae Date: Tue, 17 Apr 2018 20:14:50 +0300 Subject: [PATCH] [F] Fixed compiler warnings --- include/fort.h | 2 +- src/cell.c | 20 ++++++++++---------- src/cell.h | 4 ++-- src/fort.c | 2 +- src/fort_impl.c | 12 ++++++++++-- src/fort_impl.h | 8 +++++--- src/options.c | 6 +++--- src/options.h | 10 +++++----- src/row.c | 8 ++++---- src/row.h | 2 +- src/string_buffer.c | 34 ++++++++++++++++++++-------------- 11 files changed, 62 insertions(+), 46 deletions(-) diff --git a/include/fort.h b/include/fort.h index 5790ab8..f0093aa 100644 --- a/include/fort.h +++ b/include/fort.h @@ -524,7 +524,7 @@ FT_EXTERN int ft_set_default_cell_option(uint32_t option, int value); * - 0: Success; cell option was changed. * - (-1): !!!!!!!! todo */ -FT_EXTERN int ft_set_cell_option(FTABLE *table, unsigned row, unsigned col, uint32_t option, int value); +FT_EXTERN int ft_set_cell_option(FTABLE *table, size_t row, size_t col, uint32_t option, int value); /** * Table options identifiers diff --git a/src/cell.c b/src/cell.c index 057bd1c..edef0ca 100644 --- a/src/cell.c +++ b/src/cell.c @@ -36,7 +36,7 @@ void destroy_cell(fort_cell_t *cell) F_FREE(cell); } -unsigned int hint_width_cell(const fort_cell_t *cell, const context_t *context) +size_t 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,24 +45,24 @@ unsigned int hint_width_cell(const fort_cell_t *cell, const context_t *context) assert(cell); assert(context); - 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; + size_t cell_padding_left = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_LEFT_PADDING); + size_t cell_padding_right = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_RIGHT_PADDING); + size_t 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, (unsigned)get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_MIN_WIDTH)); + result = MAX(result, (size_t)get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_MIN_WIDTH)); return result; } -unsigned int hint_height_cell(const fort_cell_t *cell, const context_t *context) +size_t hint_height_cell(const fort_cell_t *cell, const context_t *context) { assert(cell); assert(context); - 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; + size_t cell_padding_top = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TOP_PADDING); + size_t cell_padding_bottom = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_BOTTOM_PADDING); + size_t cell_empty_string_height = get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_EMPTY_STR_HEIGHT); + size_t 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); result += text_height == 0 ? cell_empty_string_height : text_height; diff --git a/src/cell.h b/src/cell.h index 45bc0b9..b7d768f 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); -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); +size_t hint_width_cell(const fort_cell_t *cell, const context_t *context); +size_t hint_height_cell(const fort_cell_t *cell, const context_t *context); /* diff --git a/src/fort.c b/src/fort.c index 1a262ea..5273a49 100644 --- a/src/fort.c +++ b/src/fort.c @@ -824,7 +824,7 @@ int ft_set_border_style(FTABLE *table, struct ft_border_style *style) -int ft_set_cell_option(FTABLE *table, unsigned row, unsigned col, uint32_t option, int value) +int ft_set_cell_option(FTABLE *table, size_t row, size_t col, uint32_t option, int value) { assert(table); diff --git a/src/fort_impl.c b/src/fort_impl.c index 1f44471..001f834 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -148,6 +148,10 @@ int snprint_n_chars(char *buf, size_t length, size_t n, char ch) if (n == 0) return 0; + /* To ensure valid return value it is safely not print such big strings */ + if (n > INT_MAX) + return -1; + int status = snprintf(buf, length, "%0*d", (int)n, 0); if (status < 0) return status; @@ -157,7 +161,7 @@ int snprint_n_chars(char *buf, size_t length, size_t n, char ch) *buf = ch; buf++; } - return n; + return (int)n; } @@ -169,6 +173,10 @@ int wsnprint_n_chars(wchar_t *buf, size_t length, size_t n, wchar_t ch) if (n == 0) return 0; + /* To ensure valid return value it is safely not print such big strings */ + if (n > INT_MAX) + return -1; + int status = swprintf(buf, length, L"%0*d", (int)n, 0); if (status < 0) return status; @@ -178,5 +186,5 @@ int wsnprint_n_chars(wchar_t *buf, size_t length, size_t n, wchar_t ch) *buf = ch; buf++; } - return n; + return (int)n; } diff --git a/src/fort_impl.h b/src/fort_impl.h index 89d10d9..8e692f8 100644 --- a/src/fort_impl.h +++ b/src/fort_impl.h @@ -1,6 +1,10 @@ #ifndef FORT_IMPL_H #define FORT_IMPL_H +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS /* To disable warnings for unsafe functions */ +#endif + #include #include #include @@ -8,9 +12,7 @@ #include #include "fort.h" -#if defined(FT_MICROSOFT_COMPILER) -#define _CRT_SECURE_NO_WARNINGS /* To disable warnings for unsafe functions */ -#endif + #define FORT_COL_SEPARATOR '|' diff --git a/src/options.c b/src/options.c index ed3426e..8a4d402 100644 --- a/src/options.c +++ b/src/options.c @@ -86,7 +86,7 @@ void destroy_cell_opt_container(fort_cell_opt_container_t *cont) destroy_vector(cont); } -const fort_cell_options_t *cget_cell_opt(const fort_cell_opt_container_t *cont, unsigned row, unsigned col) +const fort_cell_options_t *cget_cell_opt(const fort_cell_opt_container_t *cont, size_t row, size_t col) { assert(cont); size_t sz = vector_size(cont); @@ -99,7 +99,7 @@ const fort_cell_options_t *cget_cell_opt(const fort_cell_opt_container_t *cont, return NULL; } -fort_cell_options_t *get_cell_opt_and_create_if_not_exists(fort_cell_opt_container_t *cont, unsigned row, unsigned col) +fort_cell_options_t *get_cell_opt_and_create_if_not_exists(fort_cell_opt_container_t *cont, size_t row, size_t col) { assert(cont); size_t sz = vector_size(cont); @@ -186,7 +186,7 @@ fort_fail: } -fort_status_t set_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option, int value) +fort_status_t set_cell_option(fort_cell_opt_container_t *cont, size_t row, size_t col, uint32_t option, int value) { fort_cell_options_t *opt = get_cell_opt_and_create_if_not_exists(cont, row, col); if (opt == NULL) diff --git a/src/options.h b/src/options.h index b08a1d9..aab23a4 100644 --- a/src/options.h +++ b/src/options.h @@ -26,8 +26,8 @@ typedef struct vector vector_t; struct fort_cell_options { - unsigned cell_row; - unsigned cell_col; + size_t cell_row; + size_t cell_col; uint32_t options; unsigned int col_min_width; enum ft_text_alignment align; @@ -46,9 +46,9 @@ typedef struct fort_cell_options fort_cell_options_t; typedef vector_t fort_cell_opt_container_t; fort_cell_opt_container_t *create_cell_opt_container(void); void destroy_cell_opt_container(fort_cell_opt_container_t *cont); -const fort_cell_options_t* cget_cell_opt(const fort_cell_opt_container_t *cont, unsigned row, unsigned col); -fort_cell_options_t* get_cell_opt_and_create_if_not_exists(fort_cell_opt_container_t *cont, unsigned row, unsigned col); -fort_status_t set_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option, int value); +const fort_cell_options_t* cget_cell_opt(const fort_cell_opt_container_t *cont, size_t row, size_t col); +fort_cell_options_t* get_cell_opt_and_create_if_not_exists(fort_cell_opt_container_t *cont, size_t row, size_t col); +fort_status_t set_cell_option(fort_cell_opt_container_t *cont, size_t row, size_t col, uint32_t option, int value); fort_status_t unset_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option); int get_cell_opt_value_hierarcial(const fort_table_options_t *options, size_t row, size_t column, uint32_t option); diff --git a/src/row.c b/src/row.c index dff3468..422b6ef 100644 --- a/src/row.c +++ b/src/row.c @@ -52,7 +52,7 @@ void destroy_row(fort_row_t *row) -unsigned int columns_in_row(const fort_row_t *row) +size_t columns_in_row(const fort_row_t *row) { if (row == NULL || row->cells == NULL) return 0; @@ -414,7 +414,7 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) while (1) { va_list va; va_copy(va, *va_args); - int virtual_sz = vsnprintf(buffer->str.cstr, string_buffer_capacity(buffer)/*buffer->str_sz*/, fmt, va); + int virtual_sz = vsnprintf(buffer->str.cstr, string_buffer_capacity(buffer), fmt, va); va_end(va); /* If error encountered */ if (virtual_sz < 0) @@ -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; - unsigned int cols_in_row = columns_in_row(row); + size_t 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; - unsigned int cols_in_row = columns_in_row(row); + size_t 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 bec125d..68f3701 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); -unsigned int columns_in_row(const fort_row_t *row); +size_t 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 97f662a..bb98bbf 100644 --- a/src/string_buffer.c +++ b/src/string_buffer.c @@ -3,18 +3,21 @@ #include "assert.h" #include "wchar.h" #include "wcwidth.h" +#include /***************************************************************************** * STRING BUFFER * ***************************************************************************/ -static int str_iter_width(const char *beg, const char *end) +static ptrdiff_t str_iter_width(const char *beg, const char *end) { - return end - beg; + assert(end >= beg); + return (end - beg); } -static int wcs_iter_width(const wchar_t *beg, const wchar_t *end) +static ptrdiff_t wcs_iter_width(const wchar_t *beg, const wchar_t *end) { + assert(end >= beg); return mk_wcswidth(beg, (end - beg)); } @@ -293,8 +296,8 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, char *buf, size_t if ((buf_len - 1) < content_width) return -1; - int left = 0; - int right = 0; + size_t left = 0; + size_t right = 0; switch (get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TEXT_ALIGN)) { case FT_ALIGNED_LEFT: @@ -313,9 +316,6 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, char *buf, size_t assert(0); break; } - if (left < 0 || right < 0) - return -1; - int written = 0; int tmp = 0; @@ -331,9 +331,13 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, char *buf, size_t old_value = *end; *(CHAR_TYPE *)end = NULL_CHAR; + ptrdiff_t str_it_width = STR_ITER_WIDTH(beg, end); + if (str_it_width < 0 || content_width < (size_t)str_it_width) + return - 1; + CHCK_RSLT_ADD_TO_WRITTEN(SNPRINTF(buf + written, buf_len - written, SNPRINTF_FMT_STR, (int)(end - beg), beg)); *(CHAR_TYPE *)end = old_value; - CHCK_RSLT_ADD_TO_WRITTEN(SNPRINT_N_CHARS(buf + written, buf_len - written, (int)(content_width - STR_ITER_WIDTH(beg, end)), SPACE_CHAR)); + CHCK_RSLT_ADD_TO_WRITTEN(SNPRINT_N_CHARS(buf + written, buf_len - written, (content_width - (size_t)str_it_width), SPACE_CHAR)); CHCK_RSLT_ADD_TO_WRITTEN(SNPRINT_N_CHARS(buf + written, buf_len - written, right, SPACE_CHAR)); return written; @@ -375,8 +379,8 @@ int buffer_wprintf(string_buffer_t *buffer, size_t buffer_row, wchar_t *buf, siz if ((buf_len - 1) < content_width) return -1; - int left = 0; - int right = 0; + size_t left = 0; + size_t right = 0; switch (get_cell_opt_value_hierarcial(context->table_options, context->row, context->column, FT_COPT_TEXT_ALIGN)) { case FT_ALIGNED_LEFT: @@ -395,8 +399,6 @@ int buffer_wprintf(string_buffer_t *buffer, size_t buffer_row, wchar_t *buf, siz assert(0); break; } - if (left < 0 || right < 0) - return -1; int written = 0; int tmp = 0; @@ -412,9 +414,13 @@ int buffer_wprintf(string_buffer_t *buffer, size_t buffer_row, wchar_t *buf, siz old_value = *end; *(CHAR_TYPE *)end = NULL_CHAR; + ptrdiff_t str_it_width = STR_ITER_WIDTH(beg, end); + if (str_it_width < 0 || content_width < (size_t)str_it_width) + return - 1; + CHCK_RSLT_ADD_TO_WRITTEN(SNPRINTF(buf + written, buf_len - written, SNPRINTF_FMT_STR, (int)(end - beg), beg)); *(CHAR_TYPE *)end = old_value; - CHCK_RSLT_ADD_TO_WRITTEN(SNPRINT_N_CHARS(buf + written, buf_len - written, (int)(content_width - STR_ITER_WIDTH(beg, end)), SPACE_CHAR)); + CHCK_RSLT_ADD_TO_WRITTEN(SNPRINT_N_CHARS(buf + written, buf_len - written, (content_width - (size_t)str_it_width), SPACE_CHAR)); CHCK_RSLT_ADD_TO_WRITTEN(SNPRINT_N_CHARS(buf + written, buf_len - written, right, SPACE_CHAR)); return written;