Skip to content

Commit 635356e

Browse files
dezgegkeveryang
authored andcommitted
UPSTREAM: fs/fat: Check malloc return values and fix memory leaks
Check malloc() return values and properly unwind on errors so memory allocated for fat_itr structures get freed properly. Also fixes a leak of fsdata.fatbuf in fat_size(). Fixes: 2460098 ("fs/fat: Reduce stack usage") Change-Id: If2abd822a136b40375f6b0052c88c0d9deb3a632 Reported-by: Coverity (CID: 167225, 167233, 167234) Signed-off-by: Tuomas Tynkkynen <[email protected]> Reviewed-by: Tom Rini <[email protected]> Signed-off-by: Kever Yang <[email protected]> (cherry picked from commit af609e3)
1 parent 86597fe commit 635356e

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

fs/fat/fat.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,12 +1038,15 @@ int fat_exists(const char *filename)
10381038
int ret;
10391039

10401040
itr = malloc_cache_aligned(sizeof(fat_itr));
1041+
if (!itr)
1042+
return 0;
10411043
ret = fat_itr_root(itr, &fsdata);
10421044
if (ret)
1043-
return 0;
1045+
goto out;
10441046

10451047
ret = fat_itr_resolve(itr, filename, TYPE_ANY);
10461048
free(fsdata.fatbuf);
1049+
out:
10471050
free(itr);
10481051
return ret == 0;
10491052
}
@@ -1055,9 +1058,11 @@ int fat_size(const char *filename, loff_t *size)
10551058
int ret;
10561059

10571060
itr = malloc_cache_aligned(sizeof(fat_itr));
1061+
if (!itr)
1062+
return -ENOMEM;
10581063
ret = fat_itr_root(itr, &fsdata);
10591064
if (ret)
1060-
return ret;
1065+
goto out_free_itr;
10611066

10621067
ret = fat_itr_resolve(itr, filename, TYPE_FILE);
10631068
if (ret) {
@@ -1071,12 +1076,13 @@ int fat_size(const char *filename, loff_t *size)
10711076
*size = 0;
10721077
ret = 0;
10731078
}
1074-
goto out;
1079+
goto out_free_both;
10751080
}
10761081

10771082
*size = FAT2CPU32(itr->dent->size);
1083+
out_free_both:
10781084
free(fsdata.fatbuf);
1079-
out:
1085+
out_free_itr:
10801086
free(itr);
10811087
return ret;
10821088
}
@@ -1089,19 +1095,22 @@ int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
10891095
int ret;
10901096

10911097
itr = malloc_cache_aligned(sizeof(fat_itr));
1098+
if (!itr)
1099+
return -ENOMEM;
10921100
ret = fat_itr_root(itr, &fsdata);
10931101
if (ret)
1094-
return ret;
1102+
goto out_free_itr;
10951103

10961104
ret = fat_itr_resolve(itr, filename, TYPE_FILE);
10971105
if (ret)
1098-
goto out;
1106+
goto out_free_both;
10991107

11001108
printf("reading %s\n", filename);
11011109
ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
11021110

1103-
out:
1111+
out_free_both:
11041112
free(fsdata.fatbuf);
1113+
out_free_itr:
11051114
free(itr);
11061115
return ret;
11071116
}
@@ -1147,17 +1156,18 @@ int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
11471156

11481157
ret = fat_itr_root(&dir->itr, &dir->fsdata);
11491158
if (ret)
1150-
goto fail;
1159+
goto fail_free_dir;
11511160

11521161
ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
11531162
if (ret)
1154-
goto fail;
1163+
goto fail_free_both;
11551164

11561165
*dirsp = (struct fs_dir_stream *)dir;
11571166
return 0;
11581167

1159-
fail:
1168+
fail_free_both:
11601169
free(dir->fsdata.fatbuf);
1170+
fail_free_dir:
11611171
free(dir);
11621172
return ret;
11631173
}

0 commit comments

Comments
 (0)