From 85e36aab1258d8d9ec7b61ce013f167ef8e03ae0 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 5 Mar 2020 22:38:15 +0100 Subject: Change the signature of sqfs_compressor_create to return an error code Make sure the function has a way of telling the caller *why* it failed. This way, the function can convey whether it had an internal error, an allocation failure, whether the arguments are totaly nonsensical, or simply that the compressor *or specific configuration* is not supported. Signed-off-by: David Oberhollenzer --- lib/sqfs/comp/compressor.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'lib/sqfs/comp/compressor.c') diff --git a/lib/sqfs/comp/compressor.c b/lib/sqfs/comp/compressor.c index 28de2c5..3b48030 100644 --- a/lib/sqfs/comp/compressor.c +++ b/lib/sqfs/comp/compressor.c @@ -12,8 +12,8 @@ #include "internal.h" -typedef sqfs_compressor_t *(*compressor_fun_t) - (const sqfs_compressor_config_t *cfg); +typedef int (*compressor_fun_t)(const sqfs_compressor_config_t *cfg, + sqfs_compressor_t **out); static compressor_fun_t compressors[SQFS_COMP_MAX + 1] = { #ifdef WITH_GZIP @@ -81,17 +81,24 @@ bool sqfs_compressor_exists(SQFS_COMPRESSOR id) return (compressors[id] != NULL); } -sqfs_compressor_t *sqfs_compressor_create(const sqfs_compressor_config_t *cfg) +int sqfs_compressor_create(const sqfs_compressor_config_t *cfg, + sqfs_compressor_t **out) { sqfs_u8 padd0[sizeof(cfg->opt)]; int ret; - if (cfg == NULL || cfg->id < SQFS_COMP_MIN || cfg->id > SQFS_COMP_MAX) - return NULL; + /* check compressor ID */ + if (cfg == NULL) + return SQFS_ERROR_ARG_INVALID; + + if (cfg->id < SQFS_COMP_MIN || cfg->id > SQFS_COMP_MAX) + return SQFS_ERROR_UNSUPPORTED; if (compressors[cfg->id] == NULL) - return NULL; + return SQFS_ERROR_UNSUPPORTED; + /* make sure the padding bytes are cleared, so we could theoretically + turn them into option fields in the future and remain compatible */ memset(padd0, 0, sizeof(padd0)); switch (cfg->id) { @@ -117,9 +124,9 @@ sqfs_compressor_t *sqfs_compressor_create(const sqfs_compressor_config_t *cfg) } if (ret != 0) - return NULL; + return SQFS_ERROR_ARG_INVALID; - return compressors[cfg->id](cfg); + return compressors[cfg->id](cfg, out); } const char *sqfs_compressor_name_from_id(SQFS_COMPRESSOR id) -- cgit v1.2.3