Fix some TODO concerning error handling

This commit is contained in:
Hugues Delorme 2017-03-30 09:50:36 +02:00
parent 1d3cd5fb35
commit c1c58f087c
6 changed files with 66 additions and 9 deletions

View File

@ -670,14 +670,12 @@ static size_t gmio_amf_ostringstream_write_zlib(
} while (z_stream->avail_out == 0); } while (z_stream->avail_out == 0);
/* Check all input was used */ /* Check all input was used */
if (z_stream->avail_in != 0) { if (z_stream->avail_in != 0) {
/* TODO: set more precise error */ context->error = GMIO_ERROR_ZLIB_DEFLATE_NOT_ALL_INPUT_USED;
context->error = GMIO_ERROR_UNKNOWN;
return total_written_len; return total_written_len;
} }
/* Check stream is complete */ /* Check stream is complete */
if (context->z_flush == Z_FINISH && z_retcode != Z_STREAM_END) { if (context->z_flush == Z_FINISH && z_retcode != Z_STREAM_END) {
/* TODO: set more precise error */ context->error = GMIO_ERROR_ZLIB_DEFLATE_STREAM_INCOMPLETE;
context->error = GMIO_ERROR_UNKNOWN;
return total_written_len; return total_written_len;
} }
context->z_compressed_size += total_written_len; context->z_compressed_size += total_written_len;
@ -867,7 +865,8 @@ int gmio_amf_write(
/* Check validity of input parameters */ /* Check validity of input parameters */
context.error = GMIO_ERROR_OK; context.error = GMIO_ERROR_OK;
/* TODO: check stream function pointers */ if (!gmio_check_ostream(&context.error, stream))
goto label_end;
if (!gmio_check_memblock(&context.error, memblock)) if (!gmio_check_memblock(&context.error, memblock))
goto label_end; goto label_end;
if (!gmio_amf_check_document(&context.error, doc)) if (!gmio_amf_check_document(&context.error, doc))

View File

@ -64,9 +64,15 @@ enum gmio_error
/*! Argument size for the memory block is too small */ /*! Argument size for the memory block is too small */
GMIO_ERROR_INVALID_MEMBLOCK_SIZE, GMIO_ERROR_INVALID_MEMBLOCK_SIZE,
/*! Provided gmio_stream is \c NULL */
GMIO_ERROR_NULL_STREAM,
/*! An error occurred with gmio_stream */ /*! An error occurred with gmio_stream */
GMIO_ERROR_STREAM, GMIO_ERROR_STREAM,
/*! Some required gmio_stream function pointer is NULL */
GMIO_ERROR_STREAM_FUNC_REQUIRED,
/*! Task was stopped by user, that is to say /*! Task was stopped by user, that is to say
* gmio_task_iface::func_is_stop_requested() returned true */ * gmio_task_iface::func_is_stop_requested() returned true */
GMIO_ERROR_TASK_STOPPED, GMIO_ERROR_TASK_STOPPED,
@ -105,6 +111,12 @@ enum gmio_error
* gmio_zlib_compress_options::memory_usage */ * gmio_zlib_compress_options::memory_usage */
GMIO_ERROR_ZLIB_INVALID_COMPRESS_MEMORY_USAGE, GMIO_ERROR_ZLIB_INVALID_COMPRESS_MEMORY_USAGE,
/*! All input to be deflated(compressed) was not processed */
GMIO_ERROR_ZLIB_DEFLATE_NOT_ALL_INPUT_USED,
/*! Deflate failure to flush pending output */
GMIO_ERROR_ZLIB_DEFLATE_STREAM_INCOMPLETE,
/* ZIP */ /* ZIP */
/*! Zip64 format requires the compiler to provide a 64b integer type */ /*! Zip64 format requires the compiler to provide a 64b integer type */
GMIO_ERROR_ZIP_INT64_TYPE_REQUIRED, GMIO_ERROR_ZIP_INT64_TYPE_REQUIRED,

View File

@ -185,7 +185,7 @@ enum gmio_bool_value
# define GMIO_RESTRICT __restrict__ /* Compatible with C90 */ # define GMIO_RESTRICT __restrict__ /* Compatible with C90 */
# elif defined(_MSC_VER) # elif defined(_MSC_VER)
# define GMIO_RESTRICT __restrict # define GMIO_RESTRICT __restrict
# elif defined(GMIO_HAVE_C99_RESTRICT) /* TODO: add cmake detectection */ # elif defined(GMIO_HAVE_C99_RESTRICT) /* TODO: add cmake detection */
# define GMIO_RESTRICT restrict # define GMIO_RESTRICT restrict
# else # else
/*! Expands to the C compiler specific restrict keyword (if any) */ /*! Expands to the C compiler specific restrict keyword (if any) */

View File

@ -32,6 +32,7 @@
#include "locale_utils.h" #include "locale_utils.h"
#include "../error.h" #include "../error.h"
#include "../memblock.h" #include "../memblock.h"
#include "../stream.h"
bool gmio_check_memblock(int *error, const struct gmio_memblock* mblock) bool gmio_check_memblock(int *error, const struct gmio_memblock* mblock)
{ {
@ -56,3 +57,43 @@ bool gmio_check_lc_numeric(int *error)
*error = GMIO_ERROR_BAD_LC_NUMERIC; *error = GMIO_ERROR_BAD_LC_NUMERIC;
return gmio_no_error(*error); return gmio_no_error(*error);
} }
bool gmio_check_istream(int *error, const struct gmio_stream *stream)
{
if (stream == NULL)
*error = GMIO_ERROR_NULL_STREAM;
else if (stream->func_at_end == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_error == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_read == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_size == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_get_pos == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_set_pos == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
return gmio_no_error(*error);
}
bool gmio_check_ostream(int *error, const struct gmio_stream *stream)
{
if (stream == NULL)
*error = GMIO_ERROR_NULL_STREAM;
else if (stream->func_error == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_write == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_get_pos == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
else if (stream->func_set_pos == NULL)
*error = GMIO_ERROR_STREAM_FUNC_REQUIRED;
return gmio_no_error(*error);
}
bool gmio_check_stream(int *error, const struct gmio_stream *stream)
{
return gmio_check_istream(error, stream)
&& gmio_check_ostream(error, stream);
}

View File

@ -32,12 +32,17 @@
#include "../global.h" #include "../global.h"
#include <stddef.h> #include <stddef.h>
struct gmio_memblock; struct gmio_memblock;
struct gmio_stream;
bool gmio_check_memblock( bool gmio_check_memblock(
int* error, const struct gmio_memblock* mblock); int* error, const struct gmio_memblock* mblock);
bool gmio_check_memblock_size( bool gmio_check_memblock_size(
int* error, const struct gmio_memblock* mblock, size_t minsize); int* error, const struct gmio_memblock* mblock, size_t minsize);
/*! Checks gmio_lc_numeric_is_C(), if false sets \p *error to /* Checks gmio_lc_numeric_is_C()
* \c GMIO_ERROR_BAD_LC_NUMERIC*/ * If false sets *error to GMIO_ERROR_BAD_LC_NUMERIC */
bool gmio_check_lc_numeric(int* error); bool gmio_check_lc_numeric(int* error);
bool gmio_check_istream(int* error, const struct gmio_stream* stream);
bool gmio_check_ostream(int* error, const struct gmio_stream* stream);
bool gmio_check_stream(int* error, const struct gmio_stream* stream);

View File

@ -270,7 +270,7 @@ static const char* test_stla_write()
{ {
const char* model_filepath = filepath_stlb_grabcad_arm11; const char* model_filepath = filepath_stlb_grabcad_arm11;
const char* model_filepath_out = "temp/solid.stla"; const char* model_filepath_out = "temp/solid.stla";
struct gmio_stl_data data = {0}; /* TODO: fix memory leak on error */ struct gmio_stl_data data = {0};
char header_str[GMIO_STLB_HEADER_SIZE + 1] = {0}; char header_str[GMIO_STLB_HEADER_SIZE + 1] = {0};
/* Read input model file */ /* Read input model file */