Skip to content

Commit

Permalink
stlink-org#1214 issue fix
Browse files Browse the repository at this point in the history
Error in file size comparizon.
Due to type casting, instead of compare file size with max. singed int value, it compares with -1. Then function returns with error message.
  • Loading branch information
hydroconstructor committed Jan 9, 2022
1 parent 4132973 commit b519c63
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#define O_BINARY 0
#endif

#ifndef MAX_FILE_SIZE
#define MAX_FILE_SIZE (1<<20) // signed long int max value
#endif

#ifdef _MSC_VER
#define __attribute__(x)
#endif
Expand Down Expand Up @@ -2200,8 +2204,9 @@ static int map_file(mapped_file_t *mf, const char *path) {

if (sizeof(st.st_size) != sizeof(size_t)) {
// on 32 bit systems, check if there is an overflow
if (st.st_size > (off_t)SSIZE_MAX) {
fprintf(stderr, "mmap() size_t overflow for file %s\n", path);
if (st.st_size > (off_t)MAX_FILE_SIZE /*1 GB*/ ) {
// limit file size to 1 GB
fprintf(stderr, "mmap() file %s too big\n", path);
goto on_error;
}
}
Expand Down

0 comments on commit b519c63

Please sign in to comment.