Skip to content

Commit 794f047

Browse files
committed
probably fixes #152 SPIFFS_remove() doesn't free file descriptor
1 parent c57c386 commit 794f047

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

src/spiffs_hydrogen.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
7878
" fdsz:"_SPIPRIi " cachesz:"_SPIPRIi
7979
"\n",
8080
__func__,
81-
SPIFFS_CFG_PHYS_SZ(fs), SPIFFS_CFG_LOG_PAGE_SZ(fs), SPIFFS_CFG_LOG_BLOCK_SZ(fs),
82-
SPIFFS_CFG_PHYS_ADDR(fs),
81+
SPIFFS_CFG_PHYS_SZ(fs),
82+
SPIFFS_CFG_LOG_PAGE_SZ(fs),
83+
SPIFFS_CFG_LOG_BLOCK_SZ(fs),
8384
SPIFFS_CFG_PHYS_ERASE_SZ(fs),
85+
SPIFFS_CFG_PHYS_ADDR(fs),
8486
fd_space_size, cache_size);
8587
void *user_data;
8688
SPIFFS_LOCK(fs);

src/spiffs_nucleus.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,13 +1052,15 @@ void spiffs_cb_object_event(
10521052
obj_id_raw, spix, new_pix, new_size);
10531053
for (i = 0; i < fs->fd_count; i++) {
10541054
spiffs_fd *cur_fd = &fds[i];
1055-
#if SPIFFS_TEMPORAL_FD_CACHE
1056-
if (cur_fd->score == 0 || (cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue;
1057-
#else
1058-
if (cur_fd->file_nbr == 0 || (cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue;
1055+
if ((cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue; // fd not related to updated file
1056+
#if !SPIFFS_TEMPORAL_FD_CACHE
1057+
if (cur_fd->file_nbr == 0) continue; // fd closed
10591058
#endif
1060-
if (spix == 0) {
1059+
if (spix == 0) { // object index header update
10611060
if (ev != SPIFFS_EV_IX_DEL) {
1061+
#if SPIFFS_TEMPORAL_FD_CACHE
1062+
if (cur_fd->score == 0) continue; // never used fd
1063+
#endif
10621064
SPIFFS_DBG(" callback: setting fd "_SPIPRIfd":"_SPIPRIid"(fdoffs:"_SPIPRIi" offs:"_SPIPRIi") objix_hdr_pix to "_SPIPRIpg", size:"_SPIPRIi"\n", SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, cur_fd->fdoffset, cur_fd->offset, new_pix, new_size);
10631065
cur_fd->objix_hdr_pix = new_pix;
10641066
if (new_size != 0) {
@@ -1086,10 +1088,11 @@ void spiffs_cb_object_event(
10861088
spiffs_cache_fd_release(fs, cur_fd->cache_page);
10871089
}
10881090
#endif
1091+
SPIFFS_DBG(" callback: release fd "_SPIPRIfd":"_SPIPRIid" span:"_SPIPRIsp" objix_pix to "_SPIPRIpg"\n", SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, spix, new_pix);
10891092
cur_fd->file_nbr = 0;
10901093
cur_fd->obj_id = SPIFFS_OBJ_ID_DELETED;
10911094
}
1092-
}
1095+
} // object index header update
10931096
if (cur_fd->cursor_objix_spix == spix) {
10941097
if (ev != SPIFFS_EV_IX_DEL) {
10951098
SPIFFS_DBG(" callback: setting fd "_SPIPRIfd":"_SPIPRIid" span:"_SPIPRIsp" objix_pix to "_SPIPRIpg"\n", SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, spix, new_pix);
@@ -1098,7 +1101,7 @@ void spiffs_cb_object_event(
10981101
cur_fd->cursor_objix_pix = 0;
10991102
}
11001103
}
1101-
}
1104+
} // fd update loop
11021105

11031106
#if SPIFFS_IX_MAP
11041107

@@ -2245,7 +2248,7 @@ s32_t spiffs_fd_find_new(spiffs *fs, spiffs_fd **fd, const char *name) {
22452248
}
22462249
}
22472250

2248-
// find the free fd with least score
2251+
// find the free fd with least score or name match
22492252
for (i = 0; i < fs->fd_count; i++) {
22502253
spiffs_fd *cur_fd = &fds[i];
22512254
if (cur_fd->file_nbr == 0) {

src/test/test_bugreports.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,30 @@ TEST(seek_bug_148) {
11761176
} TEST_END
11771177

11781178

1179+
TEST(remove_release_fd_152) {
1180+
int res;
1181+
fs_reset_specific(0, 0, 64*1024, 4096, 4096, 256);
1182+
u8_t buf[1024];
1183+
memrand(buf, sizeof(buf));
1184+
TEST_CHECK_EQ(count_taken_fds(FS), 0);
1185+
spiffs_file fd1 = SPIFFS_open(FS, "removemeandloseafd", SPIFFS_O_CREAT | SPIFFS_O_RDWR, 0);
1186+
TEST_CHECK_GT(fd1, 0);
1187+
TEST_CHECK_EQ(count_taken_fds(FS), 1);
1188+
TEST_CHECK_EQ(SPIFFS_write(FS, fd1, &buf, sizeof(buf)), sizeof(buf));
1189+
TEST_CHECK_EQ(SPIFFS_close(FS, fd1), SPIFFS_OK);
1190+
TEST_CHECK_EQ(count_taken_fds(FS), 0);
1191+
spiffs_file fd2 = SPIFFS_open(FS, "removemeandloseafd", SPIFFS_O_RDWR, 0);
1192+
TEST_CHECK_GT(fd2, 0);
1193+
TEST_CHECK_EQ(count_taken_fds(FS), 1);
1194+
spiffs_file fd3 = SPIFFS_open(FS, "removemeandloseafd", SPIFFS_O_RDWR, 0);
1195+
TEST_CHECK_GT(fd3, 0);
1196+
TEST_CHECK_EQ(count_taken_fds(FS), 2);
1197+
TEST_CHECK_EQ(SPIFFS_remove(FS, "removemeandloseafd"), SPIFFS_OK);
1198+
TEST_CHECK_EQ(count_taken_fds(FS), 0);
1199+
return TEST_RES_OK;
1200+
} TEST_END
1201+
1202+
11791203
SUITE_TESTS(bug_tests)
11801204
ADD_TEST(nodemcu_full_fs_1)
11811205
ADD_TEST(nodemcu_full_fs_2)
@@ -1196,6 +1220,7 @@ SUITE_TESTS(bug_tests)
11961220
ADD_TEST(fuzzer_found_2)
11971221
ADD_TEST(fuzzer_found_3)
11981222
ADD_TEST(fuzzer_found_4)
1223+
ADD_TEST(remove_release_fd_152)
11991224
ADD_TEST_NON_DEFAULT(fuzzer_found_single_1)
12001225
ADD_TEST_NON_DEFAULT(log_afl_test)
12011226
ADD_TEST_NON_DEFAULT(afl_test)

src/test/test_spiffs.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,5 +1091,13 @@ int run_file_config(int cfg_count, tfile_conf* cfgs, int max_runs, int max_concu
10911091
return 0;
10921092
}
10931093

1094-
1095-
1094+
int count_taken_fds(spiffs *fs) {
1095+
int i;
1096+
spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
1097+
int taken = 0;
1098+
for (i = 0; i < fs->fd_count; i++) {
1099+
spiffs_fd *cur_fd = &fds[i];
1100+
if (cur_fd->file_nbr) taken++;
1101+
}
1102+
return taken;
1103+
}

src/test/test_spiffs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void invoke_error_after_read_bytes(u32_t b, char once_only);
9090
void invoke_error_after_write_bytes(u32_t b, char once_only);
9191
void fs_set_validate_flashing(int i);
9292
int get_error_count();
93+
int count_taken_fds(spiffs *fs);
9394

9495
void memrand(u8_t *b, int len);
9596
int test_create_file(char *name);

0 commit comments

Comments
 (0)