Merge pull request #2171 from cesanta/null_fs

Check for calloc() failure in filesystem code
This commit is contained in:
Sergey Lyubka authored and GitHub committed 2023-04-29 11:59:03 +01:00
commit a0952e0012
3 files changed
+20 -16

No files matched your search

+10 -8
View File
@@ -788,12 +788,13 @@ 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;
memcpy(fp, &f, sizeof(*fp)); if ((fp = calloc(1, sizeof(*fp))) != NULL) {
return fp; memcpy(fp, &f, sizeof(*fp));
} else { return fp;
return NULL; }
} }
return NULL;
} }
static void ff_close(void *fp) { static void ff_close(void *fp) {
@@ -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;
} }
+6 -5
View File
@@ -73,12 +73,13 @@ 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;
memcpy(fp, &f, sizeof(*fp)); if ((fp = calloc(1, sizeof(*fp))) != NULL) {
return fp; memcpy(fp, &f, sizeof(*fp));
} else { return fp;
return NULL; }
} }
return NULL;
} }
static void ff_close(void *fp) { static void ff_close(void *fp) {
+4 -3
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;
} }