Check for calloc() failure in filesystem code

This commit is contained in:
Sergio R. Caprile 2023-04-27 11:19:02 -03:00
parent 4593332204
commit f015a8686e
3 changed files with 20 additions and 16 deletions

View File

@ -788,12 +788,13 @@ static void *ff_open(const char *path, int flags) {
unsigned char mode = FA_READ;
if (flags & MG_FS_WRITE) mode |= FA_WRITE | FA_OPEN_ALWAYS | FA_OPEN_APPEND;
if (f_open(&f, path, mode) == 0) {
FIL *fp = calloc(1, sizeof(*fp));
memcpy(fp, &f, sizeof(*fp));
return fp;
} else {
return NULL;
FIL *fp;
if ((fp = calloc(1, sizeof(*fp))) != NULL) {
memcpy(fp, &f, sizeof(*fp));
return fp;
}
}
return NULL;
}
static void ff_close(void *fp) {
@ -913,9 +914,10 @@ static void *packed_open(const char *path, int flags) {
struct packed_file *fp = NULL;
if (data == NULL) return NULL;
if (flags & MG_FS_WRITE) return NULL;
fp = (struct packed_file *) calloc(1, sizeof(*fp));
fp->size = size;
fp->data = data;
if ((fp = (struct packed_file *) calloc(1, sizeof(*fp))) != NULL) {
fp->size = size;
fp->data = data;
}
return (void *) fp;
}

View File

@ -73,12 +73,13 @@ static void *ff_open(const char *path, int flags) {
unsigned char mode = FA_READ;
if (flags & MG_FS_WRITE) mode |= FA_WRITE | FA_OPEN_ALWAYS | FA_OPEN_APPEND;
if (f_open(&f, path, mode) == 0) {
FIL *fp = calloc(1, sizeof(*fp));
memcpy(fp, &f, sizeof(*fp));
return fp;
} else {
return NULL;
FIL *fp;
if ((fp = calloc(1, sizeof(*fp))) != NULL) {
memcpy(fp, &f, sizeof(*fp));
return fp;
}
}
return NULL;
}
static void ff_close(void *fp) {

View File

@ -67,9 +67,10 @@ static void *packed_open(const char *path, int flags) {
struct packed_file *fp = NULL;
if (data == NULL) return NULL;
if (flags & MG_FS_WRITE) return NULL;
fp = (struct packed_file *) calloc(1, sizeof(*fp));
fp->size = size;
fp->data = data;
if ((fp = (struct packed_file *) calloc(1, sizeof(*fp))) != NULL) {
fp->size = size;
fp->data = data;
}
return (void *) fp;
}