Skip to content

Commit

Permalink
target: fix a memory leak in image_open
Browse files Browse the repository at this point in the history
Change-Id: I629be26e7752858091ad58c2b3b07f43e22e8c23
Signed-off-by: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7935
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
  • Loading branch information
en-sc authored and borneoa committed Nov 11, 2023
1 parent d209598 commit 0f26118
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/target/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,12 +968,13 @@ int image_open(struct image *image, const char *url, const char *type_string)

retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY);
if (retval != ERROR_OK)
return retval;
goto free_mem_on_error;

size_t filesize;
retval = fileio_size(image_binary->fileio, &filesize);
if (retval != ERROR_OK) {
fileio_close(image_binary->fileio);
return retval;
goto free_mem_on_error;
}

image->num_sections = 1;
Expand All @@ -988,14 +989,14 @@ int image_open(struct image *image, const char *url, const char *type_string)

retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT);
if (retval != ERROR_OK)
return retval;
goto free_mem_on_error;

retval = image_ihex_buffer_complete(image);
if (retval != ERROR_OK) {
LOG_ERROR(
"failed buffering IHEX image, check server output for additional information");
fileio_close(image_ihex->fileio);
return retval;
goto free_mem_on_error;
}
} else if (image->type == IMAGE_ELF) {
struct image_elf *image_elf;
Expand All @@ -1004,12 +1005,12 @@ int image_open(struct image *image, const char *url, const char *type_string)

retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY);
if (retval != ERROR_OK)
return retval;
goto free_mem_on_error;

retval = image_elf_read_headers(image);
if (retval != ERROR_OK) {
fileio_close(image_elf->fileio);
return retval;
goto free_mem_on_error;
}
} else if (image->type == IMAGE_MEMORY) {
struct target *target = get_target(url);
Expand Down Expand Up @@ -1039,14 +1040,14 @@ int image_open(struct image *image, const char *url, const char *type_string)

retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT);
if (retval != ERROR_OK)
return retval;
goto free_mem_on_error;

retval = image_mot_buffer_complete(image);
if (retval != ERROR_OK) {
LOG_ERROR(
"failed buffering S19 image, check server output for additional information");
fileio_close(image_mot->fileio);
return retval;
goto free_mem_on_error;
}
} else if (image->type == IMAGE_BUILDER) {
image->num_sections = 0;
Expand All @@ -1067,6 +1068,11 @@ int image_open(struct image *image, const char *url, const char *type_string)
}

return retval;

free_mem_on_error:
free(image->type_private);
image->type_private = NULL;
return retval;
};

int image_read_section(struct image *image,
Expand Down

0 comments on commit 0f26118

Please sign in to comment.