Fix detected coverity defects

This commit is contained in:
Hugues Delorme 2017-03-29 12:04:11 +02:00
parent 0d3a7e755c
commit 1521a12c71
8 changed files with 54 additions and 37 deletions

View File

@ -862,7 +862,7 @@ int gmio_amf_write(
struct gmio_amf_wcontext context = {0};
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
gmio_memblock_helper(&opts->stream_memblock);
const struct gmio_memblock* memblock = &mblock_helper.memblock;
/* Check validity of input parameters */
@ -971,6 +971,7 @@ int gmio_amf_write_file(
}
label_end:
fclose(file);
if (file != NULL)
fclose(file);
return error;
}

View File

@ -41,7 +41,7 @@ struct gmio_const_string gmio_fileutils_find_basefilename(const char* filepath)
const char* const pos_filepath_begin = filepath;
const char* const pos_filepath_end = filepath + filepath_len;
const char* pos_last_dot = pos_filepath_end;
const char* it = filepath_len != 0 ? pos_filepath_end - 1 : NULL;
const char* it = pos_filepath_end - 1;
while (it != pos_filepath_begin
&& *it != '/'
&& *it != '\\')

View File

@ -32,6 +32,7 @@
#include "../error.h"
#include "string_ascii_utils.h"
#include <assert.h>
#include <locale.h>
#include <stddef.h>
#include <string.h>
@ -49,21 +50,26 @@ bool gmio_lc_numeric_is_C()
|| gmio_ascii_stricmp(lc, "POSIX") == 0);
}
static char global_lc_numeric[64] = {0};
static char global_lcnum[64] = {0};
static const size_t maxlen_global_lcnum = GMIO_ARRAY_SIZE(global_lcnum);
void gmio_lc_numeric_save()
{
/* Save LC_NUMERIC
* POSIX specifies that the pointer returned by setlocale(), not just the
* contents of the pointed-to string, may be invalidated by subsequent calls
* to setlocale */
strncpy(global_lc_numeric,
setlocale(LC_NUMERIC, NULL),
GMIO_ARRAY_SIZE(global_lc_numeric));
* to setlocale() */
const char* lcnum = setlocale(LC_NUMERIC, NULL);
const size_t lcnum_len = strlen(lcnum != NULL ? lcnum : "");
const size_t lcnum_endpos = lcnum_len > 0 ? lcnum_len + 1 : 0;
assert(lcnum_endpos < maxlen_global_lcnum);
if (lcnum_len > 0)
strncpy(global_lcnum, lcnum, lcnum_len);
global_lcnum[lcnum_endpos] = '\0';
}
void gmio_lc_numeric_restore()
{
if (global_lc_numeric[0] != '\0')
setlocale(LC_NUMERIC, global_lc_numeric);
if (global_lcnum[0] != '\0')
setlocale(LC_NUMERIC, global_lcnum);
}

View File

@ -260,8 +260,7 @@ GMIO_INLINE bool stla_parsing_can_continue(
/* Parses the (optional) solid name that appears after token "endsolid"
*
* It should be the same name as the one parsed with
* gmio_stla_parse_solidname_beg()
*/
* gmio_stla_parse_solidname_beg() */
static int parse_solidname_end(struct gmio_stla_parse_data* data);
/* Parses "solid <name>" */
@ -678,8 +677,15 @@ void parse_facets(struct gmio_stla_parse_data* data)
func_add_triangle(creator_cookie, i_facet, &facet);
/* Eat next unknown token */
token_str->len = 0;
gmio_stringstream_eat_word(&data->strstream, token_str);
data->token = stla_find_token_from_string(token_str);
const enum gmio_eat_word_error eat_error =
gmio_stringstream_eat_word(&data->strstream, token_str);
if (eat_error == GMIO_EAT_WORD_ERROR_OK) {
data->token = stla_find_token_from_string(token_str);
}
else {
data->token = unknown_token;
data->error = true;
}
++i_facet;
}
else {

View File

@ -29,11 +29,10 @@
#include "utest_lib.h"
#include "../src/gmio_core/global.h"
#include "../src/gmio_core/memblock.h"
static struct gmio_memblock g_testamf_memblock;
#include "test_amf_io.c"
#include <stddef.h>
/* Static memblock */
@ -45,6 +44,7 @@ struct gmio_memblock gmio_memblock_for_tests()
void all_tests()
{
gmio_memblock_set_default_constructor(gmio_memblock_for_tests);
g_testamf_memblock = gmio_memblock_calloc(32, 1024); /* 32KB */
UTEST_RUN(test_amf_write_doc_null);
UTEST_RUN(test_amf_write_doc_1_plaintext);
@ -52,5 +52,7 @@ void all_tests()
UTEST_RUN(test_amf_write_doc_1_zip64);
UTEST_RUN(test_amf_write_doc_1_zip64_file);
UTEST_RUN(test_amf_write_doc_1_task_iface);
gmio_memblock_deallocate(&g_testamf_memblock);
}
UTEST_MAIN(all_tests)

View File

@ -29,6 +29,9 @@
#include "utest_lib.h"
#include "../src/gmio_core/memblock.h"
static struct gmio_memblock g_testcore_memblock;
#include "test_core.c"
#include "test_core_benchmark_fast_atof.c"
#include "test_core_internal.c"
@ -36,6 +39,8 @@
void all_tests()
{
g_testcore_memblock = gmio_memblock_calloc(32, 1024); /* 32KB */
UTEST_RUN(test_core__buffer);
UTEST_RUN(test_core__endian);
UTEST_RUN(test_core__error);
@ -59,5 +64,7 @@ void all_tests()
UTEST_RUN(test_internal__zip_utils);
UTEST_RUN(test_internal__zlib_enumvalues);
UTEST_RUN(test_internal__file_utils);
gmio_memblock_deallocate(&g_testcore_memblock);
}
UTEST_MAIN(all_tests)

View File

@ -288,7 +288,7 @@ static const char* test_amf_write_doc_1_plaintext()
{
static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1);
wbuff.ptr = g_testamf_memblock.ptr;
wbuff.len = wbuffsize;
const struct __tamf__document testdoc = __tamf__create_doc_1();
@ -302,7 +302,6 @@ static const char* test_amf_write_doc_1_plaintext()
#endif
UTEST_COMPARE_INT(error, GMIO_ERROR_OK);
/* printf("%s\n", wbuff.ptr); */
free(wbuff.ptr);
return NULL;
}
@ -313,8 +312,10 @@ static const char* test_amf_write_doc_1_zip()
{
static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1);
uint8_t* ptr_g_memblock = g_testamf_memblock.ptr;
wbuff.ptr = ptr_g_memblock;
wbuff.len = wbuffsize;
ptr_g_memblock += wbuff.len;
const struct __tamf__document testdoc = __tamf__create_doc_1();
const struct gmio_amf_document doc = __tamf_create_doc(&testdoc);
@ -327,8 +328,9 @@ static const char* test_amf_write_doc_1_zip()
const size_t amf_data_len = wbuff.pos;
const uint32_t crc32_amf_data = gmio_zlib_crc32(wbuff.ptr, amf_data_len);
uint8_t* amf_data = calloc(amf_data_len, 1);
uint8_t* amf_data = ptr_g_memblock;
memcpy(amf_data, wbuff.ptr, amf_data_len);
ptr_g_memblock += amf_data_len;
{ /* Write compressed(ZIP) */
wbuff.pos = 0;
@ -376,24 +378,22 @@ static const char* test_amf_write_doc_1_zip()
/* -- Read and check compressed AMF data */
wbuff.pos = lfh_read_len + zip_lfh.filename_len + zip_lfh.extrafield_len;
{
uint8_t* dest = calloc(amf_data_len, 1);
uint8_t* dest = ptr_g_memblock;
size_t dest_len = amf_data_len;
ptr_g_memblock += dest_len;
const uint8_t* amf_zdata = (const uint8_t*)wbuff.ptr + wbuff.pos;
const int error = gmio_zlib_uncompress_buffer(
dest, &dest_len, amf_zdata, amf_zdata_len);
printf("\n-- Info: z_len=%i src_len=%i\n",
amf_zdata_len, amf_data_len);
printf("\ninfo: z_len=%u src_len=%u\n",
(unsigned)amf_zdata_len, (unsigned)amf_data_len);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
UTEST_COMPARE_UINT(dest_len, amf_data_len);
UTEST_COMPARE_INT(memcmp(dest, amf_data, amf_data_len), 0);
const uint32_t crc32_uncomp = gmio_zlib_crc32(dest, dest_len);
UTEST_COMPARE_UINT(crc32_amf_data, crc32_uncomp);
free(dest);
}
}
free(amf_data);
free(wbuff.ptr);
return NULL;
}
@ -401,7 +401,7 @@ static const char* test_amf_write_doc_1_zip64()
{
static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1);
wbuff.ptr = g_testamf_memblock.ptr;
wbuff.len = wbuffsize;
const struct __tamf__document testdoc = __tamf__create_doc_1();
@ -464,7 +464,6 @@ static const char* test_amf_write_doc_1_zip64()
UTEST_COMPARE_UINT(amf_data_len, zip64_extra.uncompressed_size);
}
free(wbuff.ptr);
return NULL;
}
@ -507,7 +506,7 @@ static const char* test_amf_write_doc_1_task_iface()
{
static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1);
wbuff.ptr = g_testamf_memblock.ptr;
wbuff.len = wbuffsize;
const struct __tamf__document testdoc = __tamf__create_doc_1();
const struct gmio_amf_document doc = __tamf_create_doc(&testdoc);
@ -520,7 +519,7 @@ static const char* test_amf_write_doc_1_task_iface()
UTEST_COMPARE_INT(error, GMIO_ERROR_OK);
UTEST_ASSERT(!task.progress_error);
UTEST_COMPARE_INT(task.current_value, task.max_value);
printf("\n-- Info: max_value=%d\n", task.max_value);
printf("\ninfo: max_value=%d\n", (int)task.max_value);
}
uint8_t memblock[256] = {0};
@ -533,6 +532,5 @@ static const char* test_amf_write_doc_1_task_iface()
UTEST_ASSERT(task.current_value < task.max_value);
}
free(wbuff.ptr);
return NULL;
}

View File

@ -343,8 +343,8 @@ static const char* test_internal__stringstream()
static const char* test_internal__ostringstream()
{
static const size_t size = 8192;
char* input = malloc(size);
char* output = malloc(size);
char* input = g_testcore_memblock.ptr;
char* output = (char*)g_testcore_memblock.ptr + size;
char strbuff[256] = {0};
struct gmio_rw_buffer rwbuff = gmio_rw_buffer(output, size, 0);
struct gmio_ostringstream sstream =
@ -418,8 +418,6 @@ static const char* test_internal__ostringstream()
}
}
free(input);
free(output);
return NULL;
}
@ -608,7 +606,7 @@ static const char* __tc__zip_compare_entry(
static const char* test_internal__zip_utils()
{
static const unsigned bytes_size = 1024;
uint8_t* bytes = calloc(bytes_size, 1);
uint8_t* bytes = g_testcore_memblock.ptr;
struct gmio_rw_buffer wbuff = gmio_rw_buffer(bytes, bytes_size, 0);
struct gmio_stream stream = gmio_stream_buffer(&wbuff);
int error;
@ -843,7 +841,6 @@ static const char* test_internal__zip_utils()
UTEST_COMPARE_UINT(fcookie.zdata_len, zip_dd.compressed_size);
}
free(bytes);
return NULL;
}