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.
#include <stdlib.h>
#include <math.h>
#include <assert.h>
+#include <limits.h>
#ifdef _WIN32
#include "windirent.h"
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);
/* 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(
ret = 0;
goto fin;
}
- if (num_images == 0) {
- fprintf(stdout, "Folder is empty\n");
- ret = 0;
- goto fin;
- }
+
} else {
num_images = 1;
}
#include <stdlib.h>
#include <math.h>
#include <time.h>
+#include <limits.h>
#ifdef _WIN32
#include "windirent.h"
/* -------------------------------------------------------------------------- */
/* Declarations */
-int get_num_images(char *imgdirpath);
+unsigned int get_num_images(char *imgdirpath);
int load_images(dircnt_t *dirptr, char *imgdirpath);
int get_file_format(const char *filename);
char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
/* -------------------------------------------------------------------------- */
-int get_num_images(char *imgdirpath)
+unsigned 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*/
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);
return num_images;
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);
failed = 1;
goto fin;
}
- if (num_images == 0) {
- fprintf(stderr, "Folder is empty\n");
- failed = 1;
- goto fin;
- }
+
} else {
num_images = 1;
}
#include <string.h>
#include <stdlib.h>
#include <math.h>
+#include <limits.h>
#ifdef _WIN32
#include "windirent.h"
/* -------------------------------------------------------------------------- */
/* Declarations */
-static int get_num_images(char *imgdirpath);
+static unsigned int get_num_images(char *imgdirpath);
static int load_images(dircnt_t *dirptr, char *imgdirpath);
static int get_file_format(const char *filename);
static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
}
/* -------------------------------------------------------------------------- */
-static int get_num_images(char *imgdirpath)
+static unsigned 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*/
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);
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;
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;
}