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

View File

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

View File

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