From c1c58f087c1b6ba93a40eb7f8223ddfdc7e21deb Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Thu, 30 Mar 2017 09:50:36 +0200 Subject: [PATCH] Fix some TODO concerning error handling --- src/gmio_amf/amf_io.c | 9 +++--- src/gmio_core/error.h | 12 ++++++++ src/gmio_core/global.h | 2 +- src/gmio_core/internal/error_check.c | 41 ++++++++++++++++++++++++++++ src/gmio_core/internal/error_check.h | 9 ++++-- tests/test_stl_io.c | 2 +- 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/gmio_amf/amf_io.c b/src/gmio_amf/amf_io.c index c9936e1..41a5515 100644 --- a/src/gmio_amf/amf_io.c +++ b/src/gmio_amf/amf_io.c @@ -670,14 +670,12 @@ static size_t gmio_amf_ostringstream_write_zlib( } while (z_stream->avail_out == 0); /* Check all input was used */ if (z_stream->avail_in != 0) { - /* TODO: set more precise error */ - context->error = GMIO_ERROR_UNKNOWN; + context->error = GMIO_ERROR_ZLIB_DEFLATE_NOT_ALL_INPUT_USED; return total_written_len; } /* Check stream is complete */ if (context->z_flush == Z_FINISH && z_retcode != Z_STREAM_END) { - /* TODO: set more precise error */ - context->error = GMIO_ERROR_UNKNOWN; + context->error = GMIO_ERROR_ZLIB_DEFLATE_STREAM_INCOMPLETE; return total_written_len; } context->z_compressed_size += total_written_len; @@ -867,7 +865,8 @@ int gmio_amf_write( /* Check validity of input parameters */ 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)) goto label_end; if (!gmio_amf_check_document(&context.error, doc)) diff --git a/src/gmio_core/error.h b/src/gmio_core/error.h index eb24dd2..e514cca 100644 --- a/src/gmio_core/error.h +++ b/src/gmio_core/error.h @@ -64,9 +64,15 @@ enum gmio_error /*! Argument size for the memory block is too small */ GMIO_ERROR_INVALID_MEMBLOCK_SIZE, + /*! Provided gmio_stream is \c NULL */ + GMIO_ERROR_NULL_STREAM, + /*! An error occurred with gmio_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 * gmio_task_iface::func_is_stop_requested() returned true */ GMIO_ERROR_TASK_STOPPED, @@ -105,6 +111,12 @@ enum gmio_error * gmio_zlib_compress_options::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 */ /*! Zip64 format requires the compiler to provide a 64b integer type */ GMIO_ERROR_ZIP_INT64_TYPE_REQUIRED, diff --git a/src/gmio_core/global.h b/src/gmio_core/global.h index bdb7b41..a3c9db4 100644 --- a/src/gmio_core/global.h +++ b/src/gmio_core/global.h @@ -185,7 +185,7 @@ enum gmio_bool_value # define GMIO_RESTRICT __restrict__ /* Compatible with C90 */ # elif defined(_MSC_VER) # 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 # else /*! Expands to the C compiler specific restrict keyword (if any) */ diff --git a/src/gmio_core/internal/error_check.c b/src/gmio_core/internal/error_check.c index 1405ed1..870868c 100644 --- a/src/gmio_core/internal/error_check.c +++ b/src/gmio_core/internal/error_check.c @@ -32,6 +32,7 @@ #include "locale_utils.h" #include "../error.h" #include "../memblock.h" +#include "../stream.h" 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; 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); +} diff --git a/src/gmio_core/internal/error_check.h b/src/gmio_core/internal/error_check.h index abb53e6..5a76727 100644 --- a/src/gmio_core/internal/error_check.h +++ b/src/gmio_core/internal/error_check.h @@ -32,12 +32,17 @@ #include "../global.h" #include struct gmio_memblock; +struct gmio_stream; bool gmio_check_memblock( int* error, const struct gmio_memblock* mblock); bool gmio_check_memblock_size( int* error, const struct gmio_memblock* mblock, size_t minsize); -/*! Checks gmio_lc_numeric_is_C(), if false sets \p *error to - * \c GMIO_ERROR_BAD_LC_NUMERIC*/ +/* Checks gmio_lc_numeric_is_C() + * If false sets *error to GMIO_ERROR_BAD_LC_NUMERIC */ 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); diff --git a/tests/test_stl_io.c b/tests/test_stl_io.c index 8861e12..ed33b75 100644 --- a/tests/test_stl_io.c +++ b/tests/test_stl_io.c @@ -270,7 +270,7 @@ static const char* test_stla_write() { const char* model_filepath = filepath_stlb_grabcad_arm11; 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}; /* Read input model file */