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_amf_wcontext context = {0};
struct gmio_memblock_helper mblock_helper = 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; const struct gmio_memblock* memblock = &mblock_helper.memblock;
/* Check validity of input parameters */ /* Check validity of input parameters */
@ -971,6 +971,7 @@ int gmio_amf_write_file(
} }
label_end: label_end:
fclose(file); if (file != NULL)
fclose(file);
return error; 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_begin = filepath;
const char* const pos_filepath_end = filepath + filepath_len; const char* const pos_filepath_end = filepath + filepath_len;
const char* pos_last_dot = pos_filepath_end; 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 while (it != pos_filepath_begin
&& *it != '/' && *it != '/'
&& *it != '\\') && *it != '\\')

View File

@ -32,6 +32,7 @@
#include "../error.h" #include "../error.h"
#include "string_ascii_utils.h" #include "string_ascii_utils.h"
#include <assert.h>
#include <locale.h> #include <locale.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
@ -49,21 +50,26 @@ bool gmio_lc_numeric_is_C()
|| gmio_ascii_stricmp(lc, "POSIX") == 0); || 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() void gmio_lc_numeric_save()
{ {
/* Save LC_NUMERIC /* Save LC_NUMERIC
* POSIX specifies that the pointer returned by setlocale(), not just the * POSIX specifies that the pointer returned by setlocale(), not just the
* contents of the pointed-to string, may be invalidated by subsequent calls * contents of the pointed-to string, may be invalidated by subsequent calls
* to setlocale */ * to setlocale() */
strncpy(global_lc_numeric, const char* lcnum = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, NULL), const size_t lcnum_len = strlen(lcnum != NULL ? lcnum : "");
GMIO_ARRAY_SIZE(global_lc_numeric)); 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() void gmio_lc_numeric_restore()
{ {
if (global_lc_numeric[0] != '\0') if (global_lcnum[0] != '\0')
setlocale(LC_NUMERIC, global_lc_numeric); 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" /* Parses the (optional) solid name that appears after token "endsolid"
* *
* It should be the same name as the one parsed with * 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); static int parse_solidname_end(struct gmio_stla_parse_data* data);
/* Parses "solid <name>" */ /* Parses "solid <name>" */
@ -678,8 +677,15 @@ void parse_facets(struct gmio_stla_parse_data* data)
func_add_triangle(creator_cookie, i_facet, &facet); func_add_triangle(creator_cookie, i_facet, &facet);
/* Eat next unknown token */ /* Eat next unknown token */
token_str->len = 0; token_str->len = 0;
gmio_stringstream_eat_word(&data->strstream, token_str); const enum gmio_eat_word_error eat_error =
data->token = stla_find_token_from_string(token_str); 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; ++i_facet;
} }
else { else {

View File

@ -29,11 +29,10 @@
#include "utest_lib.h" #include "utest_lib.h"
#include "../src/gmio_core/global.h"
#include "../src/gmio_core/memblock.h" #include "../src/gmio_core/memblock.h"
static struct gmio_memblock g_testamf_memblock;
#include "test_amf_io.c" #include "test_amf_io.c"
#include <stddef.h> #include <stddef.h>
/* Static memblock */ /* Static memblock */
@ -45,6 +44,7 @@ struct gmio_memblock gmio_memblock_for_tests()
void all_tests() void all_tests()
{ {
gmio_memblock_set_default_constructor(gmio_memblock_for_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_null);
UTEST_RUN(test_amf_write_doc_1_plaintext); 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);
UTEST_RUN(test_amf_write_doc_1_zip64_file); UTEST_RUN(test_amf_write_doc_1_zip64_file);
UTEST_RUN(test_amf_write_doc_1_task_iface); UTEST_RUN(test_amf_write_doc_1_task_iface);
gmio_memblock_deallocate(&g_testamf_memblock);
} }
UTEST_MAIN(all_tests) UTEST_MAIN(all_tests)

View File

@ -29,6 +29,9 @@
#include "utest_lib.h" #include "utest_lib.h"
#include "../src/gmio_core/memblock.h"
static struct gmio_memblock g_testcore_memblock;
#include "test_core.c" #include "test_core.c"
#include "test_core_benchmark_fast_atof.c" #include "test_core_benchmark_fast_atof.c"
#include "test_core_internal.c" #include "test_core_internal.c"
@ -36,6 +39,8 @@
void all_tests() void all_tests()
{ {
g_testcore_memblock = gmio_memblock_calloc(32, 1024); /* 32KB */
UTEST_RUN(test_core__buffer); UTEST_RUN(test_core__buffer);
UTEST_RUN(test_core__endian); UTEST_RUN(test_core__endian);
UTEST_RUN(test_core__error); UTEST_RUN(test_core__error);
@ -59,5 +64,7 @@ void all_tests()
UTEST_RUN(test_internal__zip_utils); UTEST_RUN(test_internal__zip_utils);
UTEST_RUN(test_internal__zlib_enumvalues); UTEST_RUN(test_internal__zlib_enumvalues);
UTEST_RUN(test_internal__file_utils); UTEST_RUN(test_internal__file_utils);
gmio_memblock_deallocate(&g_testcore_memblock);
} }
UTEST_MAIN(all_tests) 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; static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0}; struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1); wbuff.ptr = g_testamf_memblock.ptr;
wbuff.len = wbuffsize; wbuff.len = wbuffsize;
const struct __tamf__document testdoc = __tamf__create_doc_1(); const struct __tamf__document testdoc = __tamf__create_doc_1();
@ -302,7 +302,6 @@ static const char* test_amf_write_doc_1_plaintext()
#endif #endif
UTEST_COMPARE_INT(error, GMIO_ERROR_OK); UTEST_COMPARE_INT(error, GMIO_ERROR_OK);
/* printf("%s\n", wbuff.ptr); */ /* printf("%s\n", wbuff.ptr); */
free(wbuff.ptr);
return NULL; return NULL;
} }
@ -313,8 +312,10 @@ static const char* test_amf_write_doc_1_zip()
{ {
static const size_t wbuffsize = 8192; static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0}; 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; wbuff.len = wbuffsize;
ptr_g_memblock += wbuff.len;
const struct __tamf__document testdoc = __tamf__create_doc_1(); const struct __tamf__document testdoc = __tamf__create_doc_1();
const struct gmio_amf_document doc = __tamf_create_doc(&testdoc); 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 size_t amf_data_len = wbuff.pos;
const uint32_t crc32_amf_data = gmio_zlib_crc32(wbuff.ptr, amf_data_len); 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); memcpy(amf_data, wbuff.ptr, amf_data_len);
ptr_g_memblock += amf_data_len;
{ /* Write compressed(ZIP) */ { /* Write compressed(ZIP) */
wbuff.pos = 0; wbuff.pos = 0;
@ -376,24 +378,22 @@ static const char* test_amf_write_doc_1_zip()
/* -- Read and check compressed AMF data */ /* -- Read and check compressed AMF data */
wbuff.pos = lfh_read_len + zip_lfh.filename_len + zip_lfh.extrafield_len; 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; 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 uint8_t* amf_zdata = (const uint8_t*)wbuff.ptr + wbuff.pos;
const int error = gmio_zlib_uncompress_buffer( const int error = gmio_zlib_uncompress_buffer(
dest, &dest_len, amf_zdata, amf_zdata_len); dest, &dest_len, amf_zdata, amf_zdata_len);
printf("\n-- Info: z_len=%i src_len=%i\n", printf("\ninfo: z_len=%u src_len=%u\n",
amf_zdata_len, amf_data_len); (unsigned)amf_zdata_len, (unsigned)amf_data_len);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error); UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
UTEST_COMPARE_UINT(dest_len, amf_data_len); UTEST_COMPARE_UINT(dest_len, amf_data_len);
UTEST_COMPARE_INT(memcmp(dest, amf_data, amf_data_len), 0); UTEST_COMPARE_INT(memcmp(dest, amf_data, amf_data_len), 0);
const uint32_t crc32_uncomp = gmio_zlib_crc32(dest, dest_len); const uint32_t crc32_uncomp = gmio_zlib_crc32(dest, dest_len);
UTEST_COMPARE_UINT(crc32_amf_data, crc32_uncomp); UTEST_COMPARE_UINT(crc32_amf_data, crc32_uncomp);
free(dest);
} }
} }
free(amf_data);
free(wbuff.ptr);
return NULL; return NULL;
} }
@ -401,7 +401,7 @@ static const char* test_amf_write_doc_1_zip64()
{ {
static const size_t wbuffsize = 8192; static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0}; struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1); wbuff.ptr = g_testamf_memblock.ptr;
wbuff.len = wbuffsize; wbuff.len = wbuffsize;
const struct __tamf__document testdoc = __tamf__create_doc_1(); 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); UTEST_COMPARE_UINT(amf_data_len, zip64_extra.uncompressed_size);
} }
free(wbuff.ptr);
return NULL; return NULL;
} }
@ -507,7 +506,7 @@ static const char* test_amf_write_doc_1_task_iface()
{ {
static const size_t wbuffsize = 8192; static const size_t wbuffsize = 8192;
struct gmio_rw_buffer wbuff = {0}; struct gmio_rw_buffer wbuff = {0};
wbuff.ptr = calloc(wbuffsize, 1); wbuff.ptr = g_testamf_memblock.ptr;
wbuff.len = wbuffsize; wbuff.len = wbuffsize;
const struct __tamf__document testdoc = __tamf__create_doc_1(); const struct __tamf__document testdoc = __tamf__create_doc_1();
const struct gmio_amf_document doc = __tamf_create_doc(&testdoc); 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_COMPARE_INT(error, GMIO_ERROR_OK);
UTEST_ASSERT(!task.progress_error); UTEST_ASSERT(!task.progress_error);
UTEST_COMPARE_INT(task.current_value, task.max_value); 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}; 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); UTEST_ASSERT(task.current_value < task.max_value);
} }
free(wbuff.ptr);
return NULL; return NULL;
} }

View File

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