summaryrefslogtreecommitdiff
path: root/src/bin/jp2/opj_compress.c
diff options
context:
space:
mode:
authorEharve14 <71228603+Eharve14@users.noreply.github.com>2022-01-15 09:33:03 -0500
committerGitHub <noreply@github.com>2022-01-15 15:33:03 +0100
commit6e4588f379be0eb5b62fff65bf96aa1ca556ea96 (patch)
treefd368e86c7da174acfb1ee45aeba6a534b00b467 /src/bin/jp2/opj_compress.c
parent1daaa0b909aebdf71be36238d16dfbec83c494ed (diff)
Added check for integer overflow in get_num_images (#1397)
As discussed in pull request 1396, added a check for integer overflow. Change list: Defined num_images as unsigned int Moved the if statement to check for an empty directory to the beginning of the read directory section Added a check to see if num images would roll back to zero when incrementing.
Diffstat (limited to 'src/bin/jp2/opj_compress.c')
-rw-r--r--src/bin/jp2/opj_compress.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c
index 646f1375..2bbe5c4c 100644
--- a/src/bin/jp2/opj_compress.c
+++ b/src/bin/jp2/opj_compress.c
@@ -44,6 +44,7 @@
#include <stdlib.h>
#include <math.h>
#include <assert.h>
+#include <limits.h>
#ifdef _WIN32
#include "windirent.h"
@@ -485,6 +486,11 @@ static unsigned int get_num_images(char *imgdirpath)
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
continue;
}
+ if (num_images == UINT_MAX) {
+ fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
+ num_images = 0;
+ break;
+ }
num_images++;
}
closedir(dir);
@@ -1957,6 +1963,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 +1985,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;
}