Skip to content

Commit 9499afd

Browse files
robert-hhpeter-pycom
authored andcommitted
vfs_littlefs_file.c: Prevent double close of a file
An attempt to close a file twice resulted in a crash, because buffers should have been freed again. This change checks first, if a file is already closed.
1 parent ed4e191 commit 9499afd

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

esp32/littlefs/vfs_littlefs_file.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
100100
return 0;
101101

102102
} else if (request == MP_STREAM_CLOSE) {
103+
if (self->littlefs == NULL) {
104+
return 0;
105+
}
103106

104107
xSemaphoreTake(self->littlefs->mutex, portMAX_DELAY);
105108
int res = littlefs_close_common_helper(&self->littlefs->lfs, &self->fp, &self->cfg, &self->timestamp_update);
106109
xSemaphoreGive(self->littlefs->mutex);
110+
111+
self->littlefs = NULL; // indicate a closed file
107112
if (res < 0) {
108113
*errcode = littleFsErrorToErrno(res);
109114
return MP_STREAM_ERROR;
110115
}
111-
// Free up the object so GC does not need to do that
112-
m_del_obj(pyb_file_obj_t, self);
113-
114116
return 0;
115117
} else {
116118
*errcode = MP_EINVAL;

0 commit comments

Comments
 (0)