From 10d3f420735925bcc747384198fdc07bb8faf0c2 Mon Sep 17 00:00:00 2001 From: "Neil.wrz" Date: Wed, 24 May 2023 18:35:33 -0700 Subject: [PATCH 10/15] fix memory leak of top_layer Signed-off-by: Neil.wrz --- .../oci/storage/image_store/image_store.c | 3 +- .../remote_layer_support/image_remote_impl.c | 50 +++++++++++++------ .../overlay_remote_impl.c | 4 +- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/daemon/modules/image/oci/storage/image_store/image_store.c b/src/daemon/modules/image/oci/storage/image_store/image_store.c index 473ba3c8..65b90832 100644 --- a/src/daemon/modules/image/oci/storage/image_store/image_store.c +++ b/src/daemon/modules/image/oci/storage/image_store/image_store.c @@ -3681,7 +3681,8 @@ int remote_append_image_by_directory_with_lock(const char *id) nret = snprintf(image_path, sizeof(image_path), "%s/%s", g_image_store->dir, id); if (nret < 0 || (size_t)nret >= sizeof(image_path)) { ERROR("Failed to get image path"); - return -1; + ret = -1; + goto out; } ret = append_image_by_directory(image_path); diff --git a/src/daemon/modules/image/oci/storage/remote_layer_support/image_remote_impl.c b/src/daemon/modules/image/oci/storage/remote_layer_support/image_remote_impl.c index 92bf901d..b4a53ec1 100644 --- a/src/daemon/modules/image/oci/storage/remote_layer_support/image_remote_impl.c +++ b/src/daemon/modules/image/oci/storage/remote_layer_support/image_remote_impl.c @@ -126,11 +126,43 @@ out: return ret; } +static int check_top_layer_and_add_image(const char *id) +{ + char *top_layer = NULL; + int ret = 0; + + top_layer = remote_image_get_top_layer_from_json(id); + if (top_layer == NULL) { + WARN("Can't get top layer id for image: %s, image not add", id); + return 0; + } + + if (!remote_layer_layer_valid(top_layer)) { + WARN("Current not find valid under layer, remote image:%s not add", id); + if (!map_remove(image_byid_new, (void *)id)) { + WARN("image %s will not be loaded from remote.", id); + } + goto out; + } + + if (remote_append_image_by_directory_with_lock(id) != 0) { + ERROR("Failed to load image into memrory: %s", id); + if (!map_remove(image_byid_new, (void *)id)) { + WARN("image %s will not be loaded from remote", id); + } + ret = -1; + } + +out: + free(top_layer); + + return ret; +} + static int remote_image_add(void *data) { char **array_added = NULL; char **array_deleted = NULL; - char *top_layer = NULL; map_t *tmp_map = NULL; bool exist = true; size_t i = 0; @@ -144,20 +176,7 @@ static int remote_image_add(void *data) array_deleted = remote_deleted_layers(image_byid_old, image_byid_new); for (i = 0; i < util_array_len((const char **)array_added); i++) { - top_layer = remote_image_get_top_layer_from_json(array_added[i]); - if (top_layer != NULL && !remote_layer_layer_valid(top_layer)) { - WARN("Current not find valid under layer, remoet image:%s not added", array_added[i]); - if (!map_remove(image_byid_new, (void *)array_added[i])) { - WARN("image %s will not be loaded from remote.", array_added[i]); - } - continue; - } - - if (remote_append_image_by_directory_with_lock(array_added[i]) != 0) { - ERROR("Failed to load image into memrory: %s", array_added[i]); - if (!map_remove(image_byid_new, (void *)array_added[i])) { - WARN("image %s will not be loaded from remote", array_added[i]); - } + if (check_top_layer_and_add_image(array_added[i]) != 0) { ret = -1; } } @@ -179,7 +198,6 @@ static int remote_image_add(void *data) util_free_array(array_added); util_free_array(array_deleted); - free(top_layer); return ret; } diff --git a/src/daemon/modules/image/oci/storage/remote_layer_support/overlay_remote_impl.c b/src/daemon/modules/image/oci/storage/remote_layer_support/overlay_remote_impl.c index 30caf175..238506c2 100644 --- a/src/daemon/modules/image/oci/storage/remote_layer_support/overlay_remote_impl.c +++ b/src/daemon/modules/image/oci/storage/remote_layer_support/overlay_remote_impl.c @@ -292,7 +292,7 @@ free_out: return ret; } -static int remote_image_add(struct remote_overlay_data *data) +static int remote_overlay_add(struct remote_overlay_data *data) { int ret = 0; char **array_added = NULL; @@ -346,7 +346,7 @@ void remote_overlay_refresh(struct remote_overlay_data *data) return; } - if (remote_image_add(data) != 0) { + if (remote_overlay_add(data) != 0) { ERROR("refresh overlay failed"); } } -- 2.25.1