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.
This commit is contained in:
parent
1daaa0b909
commit
6e4588f379
|
@ -44,6 +44,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include "windirent.h"
|
#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) {
|
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (num_images == UINT_MAX) {
|
||||||
|
fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
|
||||||
|
num_images = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
num_images++;
|
num_images++;
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
@ -1957,6 +1963,11 @@ int main(int argc, char **argv)
|
||||||
/* Read directory if necessary */
|
/* Read directory if necessary */
|
||||||
if (img_fol.set_imgdir == 1) {
|
if (img_fol.set_imgdir == 1) {
|
||||||
num_images = get_num_images(img_fol.imgdirpath);
|
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));
|
dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
|
||||||
if (dirptr) {
|
if (dirptr) {
|
||||||
dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof(
|
dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof(
|
||||||
|
@ -1974,11 +1985,7 @@ int main(int argc, char **argv)
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto fin;
|
goto fin;
|
||||||
}
|
}
|
||||||
if (num_images == 0) {
|
|
||||||
fprintf(stdout, "Folder is empty\n");
|
|
||||||
ret = 0;
|
|
||||||
goto fin;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
num_images = 1;
|
num_images = 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include "windirent.h"
|
#include "windirent.h"
|
||||||
|
@ -160,7 +161,7 @@ typedef struct opj_decompress_params {
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* Declarations */
|
/* Declarations */
|
||||||
int get_num_images(char *imgdirpath);
|
unsigned int get_num_images(char *imgdirpath);
|
||||||
int load_images(dircnt_t *dirptr, char *imgdirpath);
|
int load_images(dircnt_t *dirptr, char *imgdirpath);
|
||||||
int get_file_format(const char *filename);
|
int get_file_format(const char *filename);
|
||||||
char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
|
char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
|
||||||
|
@ -370,11 +371,11 @@ static OPJ_BOOL parse_precision(const char* option,
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
int get_num_images(char *imgdirpath)
|
unsigned int get_num_images(char *imgdirpath)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent* content;
|
struct dirent* content;
|
||||||
int num_images = 0;
|
unsigned int num_images = 0;
|
||||||
|
|
||||||
/*Reading the input images from given input directory*/
|
/*Reading the input images from given input directory*/
|
||||||
|
|
||||||
|
@ -388,7 +389,13 @@ int get_num_images(char *imgdirpath)
|
||||||
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
|
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (num_images == UINT_MAX) {
|
||||||
|
fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
|
||||||
|
num_images = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
num_images++;
|
num_images++;
|
||||||
|
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return num_images;
|
return num_images;
|
||||||
|
@ -1367,6 +1374,11 @@ int main(int argc, char **argv)
|
||||||
if (img_fol.set_imgdir == 1) {
|
if (img_fol.set_imgdir == 1) {
|
||||||
int it_image;
|
int it_image;
|
||||||
num_images = get_num_images(img_fol.imgdirpath);
|
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));
|
dirptr = (dircnt_t*)calloc(1, sizeof(dircnt_t));
|
||||||
if (!dirptr) {
|
if (!dirptr) {
|
||||||
destroy_parameters(¶meters);
|
destroy_parameters(¶meters);
|
||||||
|
@ -1394,11 +1406,7 @@ int main(int argc, char **argv)
|
||||||
failed = 1;
|
failed = 1;
|
||||||
goto fin;
|
goto fin;
|
||||||
}
|
}
|
||||||
if (num_images == 0) {
|
|
||||||
fprintf(stderr, "Folder is empty\n");
|
|
||||||
failed = 1;
|
|
||||||
goto fin;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
num_images = 1;
|
num_images = 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include "windirent.h"
|
#include "windirent.h"
|
||||||
|
@ -82,7 +83,7 @@ typedef struct img_folder {
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* Declarations */
|
/* 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 load_images(dircnt_t *dirptr, char *imgdirpath);
|
||||||
static int get_file_format(const char *filename);
|
static int get_file_format(const char *filename);
|
||||||
static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
|
static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
|
||||||
|
@ -122,11 +123,11 @@ static void decode_help_display(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
static int get_num_images(char *imgdirpath)
|
static unsigned int get_num_images(char *imgdirpath)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent* content;
|
struct dirent* content;
|
||||||
int num_images = 0;
|
unsigned int num_images = 0;
|
||||||
|
|
||||||
/*Reading the input images from given input directory*/
|
/*Reading the input images from given input directory*/
|
||||||
|
|
||||||
|
@ -140,6 +141,11 @@ static int get_num_images(char *imgdirpath)
|
||||||
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
|
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (num_images == UINT_MAX) {
|
||||||
|
fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
|
||||||
|
num_images = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
num_images++;
|
num_images++;
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
@ -510,7 +516,10 @@ int main(int argc, char *argv[])
|
||||||
if (img_fol.set_imgdir == 1) {
|
if (img_fol.set_imgdir == 1) {
|
||||||
int it_image;
|
int it_image;
|
||||||
num_images = get_num_images(img_fol.imgdirpath);
|
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));
|
dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
|
||||||
if (!dirptr) {
|
if (!dirptr) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
@ -536,10 +545,7 @@ int main(int argc, char *argv[])
|
||||||
if (load_images(dirptr, img_fol.imgdirpath) == 1) {
|
if (load_images(dirptr, img_fol.imgdirpath) == 1) {
|
||||||
goto fails;
|
goto fails;
|
||||||
}
|
}
|
||||||
if (num_images == 0) {
|
|
||||||
fprintf(stdout, "Folder is empty\n");
|
|
||||||
goto fails;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
num_images = 1;
|
num_images = 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue