Skip to content

Commit 7eaf61c

Browse files
committed
Fixed memory leak when files/dirs error
Error path did not clean up after itself correctly
1 parent 9f8d0d6 commit 7eaf61c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

LittleFileSystem.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,16 @@ int LittleFileSystem::stat(const char *name, struct stat *st) {
332332
////// File operations //////
333333
int LittleFileSystem::file_open(fs_file_t *file, const char *path, int flags) {
334334
lfs_file_t *f = new lfs_file_t;
335-
*file = f;
336335
_mutex.lock();
337336
LFS_INFO("file_open(%p, \"%s\", 0x%x)", *file, path, flags);
338337
int err = lfs_file_open(&_lfs, f, path, lfs_fromflags(flags));
339338
LFS_INFO("file_open -> %d", lfs_toerror(err));
340339
_mutex.unlock();
340+
if (!err) {
341+
*file = f;
342+
} else {
343+
delete f;
344+
}
341345
return lfs_toerror(err);
342346
}
343347

@@ -416,12 +420,16 @@ off_t LittleFileSystem::file_size(fs_file_t file) {
416420
////// Dir operations //////
417421
int LittleFileSystem::dir_open(fs_dir_t *dir, const char *path) {
418422
lfs_dir_t *d = new lfs_dir_t;
419-
*dir = d;
420423
_mutex.lock();
421424
LFS_INFO("dir_open(%p, \"%s\")", *dir, path);
422425
int err = lfs_dir_open(&_lfs, d, path);
423426
LFS_INFO("dir_open -> %d", lfs_toerror(err));
424427
_mutex.unlock();
428+
if (!err) {
429+
*dir = d;
430+
} else {
431+
delete d;
432+
}
425433
return lfs_toerror(err);
426434
}
427435

0 commit comments

Comments
 (0)