Skip to content

Commit

Permalink
Added overflow check to get_num_images, defined num_images as unsigne…
Browse files Browse the repository at this point in the history
…d for conformity, relocated check for num images for exicution before allocation and image loading
  • Loading branch information
Eharve14 committed Jan 13, 2022
1 parent e27cfb3 commit ab6c7c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
15 changes: 10 additions & 5 deletions src/bin/jp2/opj_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down
17 changes: 11 additions & 6 deletions src/bin/jp2/opj_decompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*/

Expand All @@ -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;
Expand Down Expand Up @@ -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(&parameters);
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 10 additions & 6 deletions src/bin/jp2/opj_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*/

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit ab6c7c7

Please sign in to comment.