libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit e891931dfb93134d1c0f8c1fedcee9d2406cb1dc
parent 9df0f77cac8930cf61bd45bac7bda7ce91a1dcae
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu,  8 Jun 2017 18:55:41 +0200

Fix(Tests): Now running as expected:

Some tests still fail though, and I think coverage is shitty, gotta work on that.

Diffstat:
Minc/packer.h | 25++++++++++++++++++-------
Msrc/packer.c | 41++++++++++++++++++++++++++---------------
Msrc/packer_file.c | 3+++
Mtests/test_packer.c | 3++-
4 files changed, 49 insertions(+), 23 deletions(-)

diff --git a/inc/packer.h b/inc/packer.h @@ -129,10 +129,13 @@ static inline void packer_header_package_init(packer_header_t *ptr) { * \param ptr Pointer to the header structure */ static inline void packer_header_package_free(packer_header_t *ptr) { - free(ptr->package.name); - free(ptr->package.version); - free(ptr->package.description); - free(ptr->package.categ); + if (ptr == NULL) + return ; + + FREE(ptr->package.name); + FREE(ptr->package.version); + FREE(ptr->package.description); + FREE(ptr->package.categ); } /*! @@ -154,15 +157,20 @@ static inline void packer_header_comp_init(packer_header_t *ptr) { * \param ptr Pointer to the header structure */ static inline void packer_header_comp_free(packer_header_t *ptr) { + if (ptr == NULL) + return ; + if (ptr->compilation.configure) list_free(ptr->compilation.configure, &vector_string_free); if (ptr->compilation.env) list_free(ptr->compilation.env, &vector_string_free); - free(ptr->compilation.make); - free(ptr->compilation.test); - free(ptr->compilation.install); + ptr->compilation.env = NULL; + ptr->compilation.configure = NULL; + FREE(ptr->compilation.make); + FREE(ptr->compilation.test); + FREE(ptr->compilation.install); } /*! @@ -180,7 +188,10 @@ static inline void packer_header_deps_init(packer_header_t *ptr) { * \param ptr Pointer to the header structure */ static inline void packer_header_deps_free(packer_header_t *ptr) { + if (ptr == NULL) + return ; list_free(ptr->dependencies.list, NULL); + ptr->dependencies.list = NULL; } /*! diff --git a/src/packer.c b/src/packer.c @@ -55,6 +55,10 @@ MPX_STATIC packer_header_t *packer_header_init(void) { return NULL; } + packer_header_package_init(ret); + packer_header_comp_init(ret); + packer_header_deps_init(ret); + return ret; } @@ -126,8 +130,6 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) it = json_object_iter_begin(obj); it_end = json_object_iter_end(obj); - packer_header_comp_init(ctx->header); - /* Iterating for each member */ while (!json_object_iter_equal(&it, &it_end)) { @@ -359,8 +361,6 @@ MPX_STATIC bool packer_read_config_deps(packer_t *ctx, struct json_object *obj) return false; } - packer_header_deps_init(ctx->header); - /* Iterate over each member of the array */ for (len = json_object_array_length(obj), i = 0; i < len; i++) { @@ -404,7 +404,6 @@ MPX_STATIC bool packer_read_config_package(packer_t *ctx, struct json_object *ob /* Get the first and last member */ it = json_object_iter_begin(obj); it_end = json_object_iter_end(obj); - packer_header_package_init(ctx->header); /* Iterating over each member */ while (!json_object_iter_equal(&it, &it_end)) @@ -770,7 +769,8 @@ MPX_STATIC int read_package_header_package(const char *file, packer_t *ctx) int ret = 0; double sbu, inst_size; - packer_header_package_init(ctx->header); + if (ctx == NULL) + return ret; ctx->__pkg_name = strdup(file + ret); if (ctx->__pkg_name == NULL) @@ -808,7 +808,8 @@ MPX_STATIC int read_package_header_package(const char *file, packer_t *ctx) cleanup: SET_ERR(ERR_MEMORY); - packer_header_package_free(ctx->header); + if (ctx != NULL) + packer_header_package_free(ctx->header); return 0; } @@ -875,7 +876,8 @@ MPX_STATIC int read_package_header_compilation(char *file, packer_t *ctx) { int ret = 0; - packer_header_comp_init(ctx->header); + if (ctx == NULL) + return ret; if (read_conf_opt(file, &ctx->header->compilation.configure, &ret) != true) goto cleanup; @@ -911,7 +913,8 @@ MPX_STATIC int read_package_header_compilation(char *file, packer_t *ctx) return ret; cleanup: - packer_header_comp_free(ctx->header); + if (ctx != NULL) + packer_header_comp_free(ctx->header); return 0; } @@ -920,8 +923,6 @@ MPX_STATIC int read_package_header_dependencies(const char *file, packer_t *ctx) u32_t list_len = 0; char *tmp = NULL; - packer_header_deps_init(ctx->header); - /* Get the list size */ memcpy(&list_len, file, sizeof(u32_t)); /* Endianness */ @@ -948,7 +949,8 @@ MPX_STATIC int read_package_header_dependencies(const char *file, packer_t *ctx) return ret; cleanup: - packer_header_deps_free(ctx->header); + if (ctx != NULL) + packer_header_deps_free(ctx->header); return 0; } @@ -956,7 +958,7 @@ MPX_STATIC bool read_package_header(char *file_content, packer_t *ctx, int *s_re { int ret = 0, tmp; - if (file_content == NULL) + if (file_content == NULL || ctx == NULL) { SET_ERR(ERR_BAD_PTR); return false; @@ -996,8 +998,11 @@ MPX_STATIC bool read_package_header(char *file_content, packer_t *ctx, int *s_re return true; cleanup: - packer_header_free(ctx->header); - ctx->header = NULL; + if (ctx != NULL) + { + packer_header_free(ctx->header); + ctx->header = NULL; + } return false; } @@ -1006,6 +1011,9 @@ bool packer_read_archive_header(packer_t *ctx) { bool ret; int cur = 0; + if (ctx == NULL) + return false; + if (ctx->type != PACKER_TYPE_ARCHIVE) return false; @@ -1029,6 +1037,9 @@ bool packer_extract_archive(packer_t *ctx, const char *dir) { off_t size, ctr; char old_pwd[PATH_MAX]; + if (ctx == NULL) + return false; + /* Save the current working directory */ getcwd(old_pwd, sizeof(old_pwd)); diff --git a/src/packer_file.c b/src/packer_file.c @@ -125,6 +125,9 @@ bool get_file_information(packer_file_t *file) { char *chunk = NULL; off_t file_size; + if (file == NULL) + return false; + if (stat(file->fn, &f_stat) == -1) return false; diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -589,7 +589,8 @@ TEST(packer_file_from_binary_to_disk) { TEST(get_file_information) { packer_file_t *file; - file = malloc(sizeof(file)); + file = malloc(sizeof(*file)); + TEST_ASSERT(file != NULL, "Malloc failed"); file->fn = strdup(PACKAGE_OUTPUT_FN); set_stat_fail(0);