Skip to content

Commit

Permalink
add more check on file read
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed Sep 28, 2024
1 parent 77d6388 commit 2ea41e7
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions src/ccode/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,49 +122,48 @@ void create_dir(const char *dir) {
c_mkdir(tmp, 0755);
}

static char * c_read_file(const char * f_name, int * err, size_t * f_size) {
char * buffer;
size_t length;
FILE * f = fopen(f_name, "rb");
size_t read_length;

if (f) {
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);

/* 1 GiB; best not to load a whole large file in one string */
if (length > 1073741824) {
* err = FILE_TO_LARGE;

return "";
}

buffer = (char * ) calloc(length + 1, sizeof(char));

if (length) {
read_length = fread(buffer, 1, length, f);
static char * c_read_file(const char * f_name, int * err, size_t * f_size) {
char *buffer = NULL;
FILE *f = fopen(f_name, "rb");
if (!f) {
*err = FILE_NOT_EXIST;
return NULL; /* Return NULL on error */
}

if (length != read_length) {
* err = FILE_READ_ERROR;
fseek(f, 0, SEEK_END);
size_t length = ftell(f);
fseek(f, 0, SEEK_SET);

return "";
}
}
if (length > 1073741824) {
fclose(f);
*err = FILE_TO_LARGE;
return ""; /* Return empty on error */
}

buffer = (char *)calloc(length + 1, sizeof(char));
if (!buffer) {
fclose(f);
*err = FILE_READ_ERROR;
return ""; /* Return empty on error */
}

* err = FILE_OK;
buffer[length] = '\0';
* f_size = length;
} else {
* err = FILE_NOT_EXIST;
size_t read_length = fread(buffer, 1, length, f);
fclose(f);

return "";
if (length != read_length) {
free(buffer); /* Free on error */
*err = FILE_READ_ERROR;
return ""; /* Return empty on error */
}

return buffer;
*err = FILE_OK;
buffer[length] = '\0';
*f_size = length;
return buffer; /* Return the allocated buffer */
}


char * readfile_raw(const char * filename) {
int err;
size_t f_size;
Expand All @@ -178,7 +177,7 @@ char * readfile_raw(const char * filename) {
} else if (err == FILE_READ_ERROR) {
ferror_add("%s (%d) %s\n", "failed to read file.", err,filename);
}
return "";
return ""; /* Return empty on error */
} else {
return f_data;
}
Expand Down

0 comments on commit 2ea41e7

Please sign in to comment.