From e35cb88328f5ca7a0aaf7a1694113af34b267a73 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Fri, 9 Oct 2015 12:03:14 +0200 Subject: [PATCH] Made sure that iso_file_get_old_image_sections() returns non-NULL only if section_count > 0. Made sure that callers in libisofs expect all possible outcome as announced by API description. --- libisofs/fs_image.c | 8 ++++++-- libisofs/libisofs.h | 3 ++- libisofs/node.c | 12 ++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libisofs/fs_image.c b/libisofs/fs_image.c index 6eec9b6..a95de3d 100644 --- a/libisofs/fs_image.c +++ b/libisofs/fs_image.c @@ -3873,7 +3873,7 @@ int iso_analyze_isohybrid(IsoImage *image, int flag) §ions, 0); if (ret < 0) return ret; - if (section_count > 0) + if (ret > 0 && section_count > 0) eltorito_lba = sections[0].block; free(sections); @@ -4728,7 +4728,7 @@ int iso_analyze_alpha_boot(IsoImage *image, IsoDataSource *src, int flag) file = (IsoFile *) node; ret = iso_file_get_old_image_sections(file, §ion_count, §ions, 0); - if (ret > 0) { + if (ret > 0 && section_count > 0) { size = sections[0].size / 512 + !!(sections[0].size % 512); free(sections); if (size != sai->alpha_boot_image_size) @@ -6387,6 +6387,8 @@ int iso_file_get_old_image_sections(IsoFile *file, int *section_count, if (flag != 0) { return ISO_WRONG_ARG_VALUE; } + *section_count = 0; + *sections = NULL; if (file->from_old_session != 0) { /* @@ -6419,6 +6421,8 @@ int iso_file_get_old_image_sections(IsoFile *file, int *section_count, ifsdata = data->src->data; *section_count = ifsdata->nsections; + if (*section_count <= 0) + return 1; *sections = malloc(ifsdata->nsections * sizeof(struct iso_file_section)); if (*sections == NULL) { diff --git a/libisofs/libisofs.h b/libisofs/libisofs.h index 1cf0440..489c413 100644 --- a/libisofs/libisofs.h +++ b/libisofs/libisofs.h @@ -5492,7 +5492,8 @@ int iso_file_get_old_image_lba(IsoFile *file, uint32_t *lba, int flag); * @param section_count * Returns the number of extent entries in sections array. * @param sections - * Returns the array of file sections. Apply free() to dispose it. + * Returns the array of file sections if section_count > 0. + * In this case, apply free() to dispose it. * @param flag * Reserved for future usage, submit 0 * @return diff --git a/libisofs/node.c b/libisofs/node.c index 1ecc916..757701e 100644 --- a/libisofs/node.c +++ b/libisofs/node.c @@ -1193,16 +1193,17 @@ int iso_file_get_old_image_lba(IsoFile *file, uint32_t *lba, int flag) { int ret; int section_count; - struct iso_file_section *sections; + struct iso_file_section *sections = NULL; + if (file == NULL || lba == NULL) { return ISO_NULL_POINTER; } - ret = iso_file_get_old_image_sections(file, §ion_count, §ions, flag); - if (ret <= 0) { + ret = iso_file_get_old_image_sections(file, §ion_count, §ions, 0); + if (ret <= 0) return ret; - } if (section_count != 1) { - free(sections); + if (sections != NULL) + free(sections); return ISO_WRONG_ARG_VALUE; } *lba = sections[0].block; @@ -1211,7 +1212,6 @@ int iso_file_get_old_image_lba(IsoFile *file, uint32_t *lba, int flag) } - /* * Like iso_file_get_old_image_lba(), but take an IsoNode. *