From ab6c7c720334f57232d33c609d904f3b955d4f2a Mon Sep 17 00:00:00 2001 From: Eharve14 <71228603+Eharve14@users.noreply.github.com> Date: Thu, 13 Jan 2022 15:20:48 -0500 Subject: [PATCH] Added overflow check to get_num_images, defined num_images as unsigned for conformity, relocated check for num images for exicution before allocation and image loading --- src/bin/jp2/opj_compress.c | 15 ++++++++++----- src/bin/jp2/opj_decompress.c | 17 +++++++++++------ src/bin/jp2/opj_dump.c | 16 ++++++++++------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 646f1375..1072f5b5 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -486,6 +486,10 @@ static unsigned int get_num_images(char *imgdirpath) continue; } num_images++; + if(num_images == 0) { + fprintf(stderr, "Integer overflow detected when reading %s\n", imgdirpath); + return 0; + } } closedir(dir); return num_images; @@ -1957,6 +1961,11 @@ int main(int argc, char **argv) /* Read directory if necessary */ if (img_fol.set_imgdir == 1) { num_images = get_num_images(img_fol.imgdirpath); + if (num_images == 0) { + fprintf(stdout, "Folder is empty\n"); + ret = 0; + goto fin; + } dirptr = (dircnt_t*)malloc(sizeof(dircnt_t)); if (dirptr) { dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof( @@ -1974,11 +1983,7 @@ int main(int argc, char **argv) ret = 0; goto fin; } - if (num_images == 0) { - fprintf(stdout, "Folder is empty\n"); - ret = 0; - goto fin; - } + } else { num_images = 1; } diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index 0d3021cd..3a6b409f 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -374,7 +374,7 @@ int get_num_images(char *imgdirpath) { DIR *dir; struct dirent* content; - int num_images = 0; + unsigned int num_images = 0; /*Reading the input images from given input directory*/ @@ -389,6 +389,10 @@ int get_num_images(char *imgdirpath) continue; } num_images++; + if(num_images == 0) { + fprintf(stderr, "Integer overflow detected when reading %s\n", imgdirpath); + return 0; + } } closedir(dir); return num_images; @@ -1367,6 +1371,11 @@ int main(int argc, char **argv) if (img_fol.set_imgdir == 1) { int it_image; num_images = get_num_images(img_fol.imgdirpath); + if (num_images == 0) { + fprintf(stderr, "Folder is empty\n"); + failed = 1; + goto fin; + } dirptr = (dircnt_t*)calloc(1, sizeof(dircnt_t)); if (!dirptr) { destroy_parameters(¶meters); @@ -1394,11 +1403,7 @@ int main(int argc, char **argv) failed = 1; goto fin; } - if (num_images == 0) { - fprintf(stderr, "Folder is empty\n"); - failed = 1; - goto fin; - } + } else { num_images = 1; } diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c index 46b976a9..8eb73ada 100644 --- a/src/bin/jp2/opj_dump.c +++ b/src/bin/jp2/opj_dump.c @@ -126,7 +126,7 @@ static int get_num_images(char *imgdirpath) { DIR *dir; struct dirent* content; - int num_images = 0; + unsigned int num_images = 0; /*Reading the input images from given input directory*/ @@ -141,6 +141,10 @@ static int get_num_images(char *imgdirpath) continue; } num_images++; + if(num_images == 0) { + fprintf(stderr, "Integer overflow detected when reading images from %s\n", imgdirpath); + return 0; + } } closedir(dir); return num_images; @@ -510,7 +514,10 @@ int main(int argc, char *argv[]) if (img_fol.set_imgdir == 1) { int it_image; num_images = get_num_images(img_fol.imgdirpath); - + if (num_images == 0) { + fprintf(stdout, "Folder is empty\n"); + goto fails; + } dirptr = (dircnt_t*)malloc(sizeof(dircnt_t)); if (!dirptr) { return EXIT_FAILURE; @@ -536,10 +543,7 @@ int main(int argc, char *argv[]) if (load_images(dirptr, img_fol.imgdirpath) == 1) { goto fails; } - if (num_images == 0) { - fprintf(stdout, "Folder is empty\n"); - goto fails; - } + } else { num_images = 1; }