Hopefully fix newly detected coverity defects

This commit is contained in:
Hugues Delorme 2016-03-03 17:34:20 +01:00
parent ffb980ae63
commit bdb3b1e7e6
7 changed files with 71 additions and 22 deletions

View File

@ -212,6 +212,7 @@ critical_factorization (const unsigned char *needle, size_t needle_len,
if (max_suffix_rev + 1 < max_suffix + 1) if (max_suffix_rev + 1 < max_suffix + 1)
return max_suffix + 1; return max_suffix + 1;
*period = p; *period = p;
/* coverity[overflow_sink] */
return max_suffix_rev + 1; return max_suffix_rev + 1;
} }

View File

@ -55,20 +55,33 @@ struct gmio_string
/*! Returns an initialized struct gmio_const_string object */ /*! Returns an initialized struct gmio_const_string object */
GMIO_INLINE struct gmio_const_string gmio_const_string(const char* ptr, size_t len); GMIO_INLINE struct gmio_const_string gmio_const_string(const char* ptr, size_t len);
/*! Returns an initialized struct gmio_string object */ /*! Returns an initialized struct gmio_string object
*
* string.max_len is set to max(len,max_len)
*/
GMIO_INLINE struct gmio_string gmio_string(char* ptr, size_t len, size_t max_len); GMIO_INLINE struct gmio_string gmio_string(char* ptr, size_t len, size_t max_len);
/*! Clears the contents of the string \p str and makes it null */ /*! Clears the contents of the string \p str and makes it null */
GMIO_INLINE void gmio_string_clear(struct gmio_string* str); GMIO_INLINE void gmio_string_clear(struct gmio_string* str);
/*! Clears the contents of the string \p str and makes it null */ /*! Returns a pointer after the last character of \p str */
GMIO_INLINE const char* gmio_string_end(const struct gmio_string* str); GMIO_INLINE const char* gmio_string_end(const struct gmio_string* str);
/*! Copies contents of \p src into \p dst */
GMIO_INLINE void gmio_string_copy(
struct gmio_string* dst, const struct gmio_string* src);
/*! Copies contents of C-string \p src into \p dst */
GMIO_INLINE char* gmio_cstr_copy(
char* dst, size_t dst_capacity, const char* src, size_t src_len);
/* /*
* -- Implementation * -- Implementation
*/ */
#include <string.h>
#include "min_max.h"
struct gmio_const_string gmio_const_string(const char* ptr, size_t len) struct gmio_const_string gmio_const_string(const char* ptr, size_t len)
{ {
struct gmio_const_string cstr; struct gmio_const_string cstr;
@ -82,7 +95,7 @@ struct gmio_string gmio_string(char* ptr, size_t len, size_t max_len)
struct gmio_string str; struct gmio_string str;
str.ptr = ptr; str.ptr = ptr;
str.len = len; str.len = len;
str.max_len = max_len; str.max_len = GMIO_MAX(len, max_len);
return str; return str;
} }
@ -97,4 +110,26 @@ const char* gmio_string_end(const struct gmio_string* str)
return &str->ptr[str->len]; return &str->ptr[str->len];
} }
void gmio_string_copy(
struct gmio_string* dst, const struct gmio_string* src)
{
const size_t dst_new_len = GMIO_MIN(dst->max_len, src->len);
strncpy(dst->ptr, src->ptr, dst_new_len);
dst->len = dst_new_len;
}
char* gmio_cstr_copy(
char* dst, size_t dst_capacity, const char* src, size_t src_len)
{
const size_t copy_len =
dst_capacity > 0 ?
GMIO_MIN(dst_capacity - 1, src_len) :
0;
if (copy_len > 0) {
strncpy(dst, src, copy_len);
dst[copy_len] = '\0';
}
return dst + copy_len;
}
#endif /* GMIO_INTERNAL_STRING_H */ #endif /* GMIO_INTERNAL_STRING_H */

View File

@ -423,7 +423,7 @@ int gmio_stla_eat_next_token_inplace(
if (stream_char == NULL || gmio_ascii_isspace(*stream_char)) { if (stream_char == NULL || gmio_ascii_isspace(*stream_char)) {
if (*expected_token_str == 0) { if (*expected_token_str == 0) {
data->token = expected_token; data->token = expected_token;
return 0; return 0; /* Success */
} }
error = true; error = true;
} }
@ -438,8 +438,8 @@ int gmio_stla_eat_next_token_inplace(
} }
} }
if (error) { /* Error, copy the wrong token in data->token_str */
/* Copy the wrong token in data->token_str */ {
size_t i = 0; size_t i = 0;
/* -- Copy the matching part of the expected token */ /* -- Copy the matching part of the expected token */
{ {
@ -460,12 +460,12 @@ int gmio_stla_eat_next_token_inplace(
data->token_str.len = i; data->token_str.len = i;
data->token = stla_find_token_from_string(&data->token_str); data->token = stla_find_token_from_string(&data->token_str);
}
/* Notify error */ /* Notify error */
stla_error_token_expected(data, expected_token); stla_error_token_expected(data, expected_token);
return GMIO_STLA_PARSE_ERROR; return GMIO_STLA_PARSE_ERROR;
} }
return 0;
}
int gmio_stla_eat_until_token( int gmio_stla_eat_until_token(
struct gmio_stla_parse_data* data, struct gmio_stla_parse_data* data,

View File

@ -21,8 +21,10 @@
struct gmio_stlb_header gmio_stlb_header_str(const char* str) struct gmio_stlb_header gmio_stlb_header_str(const char* str)
{ {
struct gmio_stlb_header header = {0}; struct gmio_stlb_header header = {0};
if (str != NULL) if (str != NULL) {
/* coverity[buffer_size_warning] */
strncpy((char*)header.data, str, GMIO_STLB_HEADER_SIZE); strncpy((char*)header.data, str, GMIO_STLB_HEADER_SIZE);
}
return header; return header;
} }

View File

@ -18,6 +18,7 @@
#include "../src/gmio_core/internal/min_max.h" #include "../src/gmio_core/internal/min_max.h"
#include "../src/gmio_core/internal/numeric_utils.h" #include "../src/gmio_core/internal/numeric_utils.h"
#include "../src/gmio_core/internal/safe_cast.h" #include "../src/gmio_core/internal/safe_cast.h"
#include "../src/gmio_core/internal/string.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -62,10 +63,11 @@ static void gmio_stl_data__begin_solid(
if (infos->format == GMIO_STL_FORMAT_ASCII) { if (infos->format == GMIO_STL_FORMAT_ASCII) {
memset(data->solid_name, 0, sizeof(data->solid_name)); memset(data->solid_name, 0, sizeof(data->solid_name));
if (infos->stla_solid_name != NULL) { if (infos->stla_solid_name != NULL) {
const size_t len = gmio_cstr_copy(
GMIO_MIN(sizeof(data->solid_name), data->solid_name,
sizeof(data->solid_name),
infos->stla_solid_name,
strlen(infos->stla_solid_name)); strlen(infos->stla_solid_name));
strncpy(data->solid_name, infos->stla_solid_name, len);
} }
/* Try to guess how many vertices we could have assume we'll need /* Try to guess how many vertices we could have assume we'll need

View File

@ -80,14 +80,14 @@ const char* test_internal__benchmark_gmio_fast_atof()
{ "str->float", &benchmark_fast_atof, NULL, &benchmark_strtod, NULL }, { "str->float", &benchmark_fast_atof, NULL, &benchmark_strtod, NULL },
{0} {0}
}; };
struct benchmark_cmp_result bmk_res = {0}; struct benchmark_cmp_result bmk_res[] = { {0}, {0} };
const struct benchmark_cmp_result_header header = { "fast_atof", "strtod" }; const struct benchmark_cmp_result_header header = { "fast_atof", "strtod" };
struct benchmark_cmp_result_array bmk_res_array = {0}; struct benchmark_cmp_result_array bmk_res_array = {0};
test_internal__fill_float_array(); test_internal__fill_float_array();
benchmark_cmp_batch(2, bmk_arg, &bmk_res, NULL, NULL); benchmark_cmp_batch(2, bmk_arg, bmk_res, NULL, NULL);
bmk_res_array.ptr = &bmk_res; bmk_res_array.ptr = bmk_res;
bmk_res_array.count = 1; bmk_res_array.count = GMIO_ARRAY_SIZE(bmk_res) - 1;
puts("\n"); puts("\n");
benchmark_print_results( benchmark_print_results(
BENCHMARK_PRINT_FORMAT_MARKDOWN, BENCHMARK_PRINT_FORMAT_MARKDOWN,
@ -95,7 +95,7 @@ const char* test_internal__benchmark_gmio_fast_atof()
bmk_res_array); bmk_res_array);
#ifndef GMIO_DEBUG_BUILD /* Check only for release builds */ #ifndef GMIO_DEBUG_BUILD /* Check only for release builds */
UTEST_ASSERT((1.05*bmk_res.func1_exec_time_ms) < bmk_res.func2_exec_time_ms); UTEST_ASSERT((1.05*bmk_res[0].func1_exec_time_ms) < bmk_res[0].func2_exec_time_ms);
#endif #endif
return NULL; return NULL;

View File

@ -20,6 +20,7 @@
#include "../src/gmio_core/error.h" #include "../src/gmio_core/error.h"
#include "../src/gmio_core/internal/min_max.h" #include "../src/gmio_core/internal/min_max.h"
#include "../src/gmio_core/internal/string.h"
#include "../src/gmio_stl/stl_error.h" #include "../src/gmio_stl/stl_error.h"
#include "../src/gmio_stl/stl_infos.h" #include "../src/gmio_stl/stl_infos.h"
#include "../src/gmio_stl/stl_io.h" #include "../src/gmio_stl/stl_io.h"
@ -44,7 +45,11 @@ void stl_testcase_result__begin_solid(
if (res != NULL) { if (res != NULL) {
res->solid_name[0] = 0; res->solid_name[0] = 0;
if (infos->stla_solid_name != NULL) if (infos->stla_solid_name != NULL)
strcpy(res->solid_name, infos->stla_solid_name); gmio_cstr_copy(
res->solid_name,
sizeof(res->solid_name),
infos->stla_solid_name,
strlen(infos->stla_solid_name));
} }
} }
} }
@ -344,7 +349,11 @@ const char* test_stla_write()
struct gmio_stl_mesh_creator creator = struct gmio_stl_mesh_creator creator =
gmio_stl_data_mesh_creator(&data_stla); gmio_stl_data_mesh_creator(&data_stla);
size_t i = 0; size_t i = 0;
strncpy(trim_header_str, header_str, sizeof(header_str)); gmio_cstr_copy(
trim_header_str,
sizeof(trim_header_str),
header_str,
sizeof(header_str));
gmio_string_trim_from_end(trim_header_str, sizeof(header_str)); gmio_string_trim_from_end(trim_header_str, sizeof(header_str));
error = gmio_stl_read_file(model_filepath_out, &creator, NULL); error = gmio_stl_read_file(model_filepath_out, &creator, NULL);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error); UTEST_COMPARE_INT(GMIO_ERROR_OK, error);