libmpm

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

commit 659583c4236c369f6ffffd25a8db4dfe57448344
parent e459acb9fc7ad11febdd90e97c934b26e66e5710
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu,  1 Jun 2017 19:20:05 +0200

Fix(Compile) & Fix(Packer): Add a NULL check in compile_cleanup, and some error strings in JSON parser of packer

Diffstat:
Msrc/compile.c | 5+++--
Msrc/packer.c | 15+++++++++++++++
Mtests/test_compile.c | 38++++++++++++++++++++++++++++----------
3 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/src/compile.c b/src/compile.c @@ -74,6 +74,9 @@ compile_t *package_install_init(packer_t *ctx) { } bool package_install_cleanup(compile_t *ctx) { + if (ctx == NULL) + return true; + if (chdir(ctx->old_pwd) != 0) { SET_ERR(ERR_CHDIR_FAILED); @@ -278,8 +281,6 @@ bool install_archive(packer_t *ctx, compile_t *ptr) { if (packer_extract_archive(ctx, DEFAULT_EXTRACT_DIR) == false) { - SET_ERR_STR("Cannot extract archive\n"); - SET_ERR(ERR_ARCHIVE_EXTRACT); packer_free(ctx); return ret; } diff --git a/src/packer.c b/src/packer.c @@ -237,6 +237,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (obj == NULL || json_object_get_type(obj) != json_type_object) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR("Bad global JSON type for compilation section."); return false; } @@ -255,6 +256,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(tmp) != json_type_array) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR_FMT("Bad JSON type for %s, expected an array", name); goto cleanup; } @@ -276,6 +278,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(array_tmp) != json_type_string) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR("Bad JSON type for configure array, expected a string"); goto cleanup; } @@ -296,6 +299,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) else { SET_ERR(ERR_BAD_JSON); + SET_ERR_STR("Unknown JSON type in configure section"); goto cleanup; } } @@ -305,6 +309,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(tmp) != json_type_string) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR_FMT("Bad JSON type for %s, expected a string", name); goto cleanup; } @@ -318,6 +323,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(tmp) != json_type_string) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR_FMT("Bad JSON type for %s, expected a string", name); goto cleanup; } @@ -331,6 +337,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(tmp) != json_type_string) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR_FMT("Bad JSON type for %s, expected a string", name); goto cleanup; } @@ -344,6 +351,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(tmp) != json_type_array) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR_FMT("Bad JSON type for %s, expected an array", name); goto cleanup; } @@ -365,6 +373,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) if (json_object_get_type(array_tmp) != json_type_string) { SET_ERR(ERR_BAD_JSON_TYPE); + SET_ERR_STR("Bad JSON type in the env array, expected string"); goto cleanup; } @@ -385,6 +394,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) else { SET_ERR(ERR_BAD_JSON); + SET_ERR_STR("Bad JSON type in the env array, expected a string / array"); goto cleanup; } } @@ -393,6 +403,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) else { SET_ERR(ERR_BAD_JSON); + SET_ERR_STR_FMT("Unknown JSON token: %s", name); goto cleanup; } @@ -1033,6 +1044,7 @@ bool packer_extract_archive(packer_t *ctx, const char *dir) { if (ctx->type != PACKER_TYPE_ARCHIVE) { SET_ERR(ERR_BAD_ARCHIVE_TYPE); + SET_ERR_STR("Not an MPX archive"); return false; } @@ -1040,6 +1052,7 @@ bool packer_extract_archive(packer_t *ctx, const char *dir) { if (fd == -1) { SET_ERR(ERR_OPEN); + SET_ERR_STR_FMT("Can't open archive '%s'", ctx->str); return false; } @@ -1060,6 +1073,7 @@ bool packer_extract_archive(packer_t *ctx, const char *dir) { if (chdir(dir) == -1) { SET_ERR(ERR_CHDIR_FAILED); + SET_ERR_STR_FMT("Cannot go in the directory '%s'\n", dir); goto cleanup; } @@ -1069,6 +1083,7 @@ bool packer_extract_archive(packer_t *ctx, const char *dir) { if (mkdir(ctx->out_dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1) { SET_ERR(ERR_MKDIR_FAILED); + SET_ERR_STR_FMT("Cannot create directory '%s'\n", ctx->out_dir); goto cleanup; } diff --git a/tests/test_compile.c b/tests/test_compile.c @@ -191,46 +191,64 @@ TEST(package_install_cleanup) { TEST(full_install) { packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + compile_t *ptr = NULL; - TEST_ASSERT(install_archive(ctx) == true, "An error happened") + TEST_ASSERT_FMT(install_archive(ctx, ptr) == true, "An error happened: %s", GET_ERR_STR()); return TEST_SUCCESS; } TEST(full_install_2) { - packer_t *ctx; + packer_t *ctx = NULL; + compile_t *ptr = NULL; ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_malloc_fail(0); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_malloc_fail(9); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_fork_fail(0); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_fork_fail(1); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_fork_fail(2); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_fork_fail(3); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_fork_fail(4); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_fork_fail(5); - TEST_ASSERT(install_archive(ctx) == false, "Error did not raise"); + TEST_ASSERT(install_archive(ctx, ptr) == false, "Error did not raise"); + package_install_cleanup(ptr); + recursive_delete(DEFAULT_EXTRACT_DIR "/test-2.0"); return TEST_SUCCESS; }