Skip to content

Commit f5e26c4

Browse files
committed
fixes #165
1 parent 3ebfeb5 commit f5e26c4

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/spiffs_hydrogen.c

-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
481481
if ((fd->flags & SPIFFS_O_APPEND)) {
482482
fd->fdoffset = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;
483483
}
484-
485484
offset = fd->fdoffset;
486485

487486
#if SPIFFS_CACHE_WR

src/spiffs_nucleus.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -1061,12 +1061,18 @@ void spiffs_cb_object_event(
10611061
#if SPIFFS_TEMPORAL_FD_CACHE
10621062
if (cur_fd->score == 0) continue; // never used fd
10631063
#endif
1064-
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);
1064+
SPIFFS_DBG(" callback: setting fd "_SPIPRIfd":"_SPIPRIid"(fdoffs:"_SPIPRIi" offs:"_SPIPRIi") objix_hdr_pix to "_SPIPRIpg", size:"_SPIPRIi"\n",
1065+
SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, cur_fd->fdoffset, cur_fd->offset, new_pix, new_size);
10651066
cur_fd->objix_hdr_pix = new_pix;
10661067
if (new_size != 0) {
10671068
// update size and offsets for fds to this file
10681069
cur_fd->size = new_size;
10691070
u32_t act_new_size = new_size == SPIFFS_UNDEFINED_LEN ? 0 : new_size;
1071+
#if SPIFFS_CACHE_WR
1072+
if (act_new_size > 0 && cur_fd->cache_page) {
1073+
act_new_size = MAX(act_new_size, cur_fd->cache_page->offset + cur_fd->cache_page->size);
1074+
}
1075+
#endif
10701076
if (cur_fd->offset > act_new_size) {
10711077
cur_fd->offset = act_new_size;
10721078
}

src/test/test_bugreports.c

+32
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,37 @@ TEST(remove_release_fd_152) {
11991199
return TEST_RES_OK;
12001200
} TEST_END
12011201

1202+
TEST(certain_file_size_fail_165) {
1203+
fs_reset_specific(0, 0, 512*1024, 4*1024, 64*1024, 256);
1204+
const int NUM = 134;
1205+
const int SIZ = 200;
1206+
u8_t buf[SIZ];
1207+
1208+
TEST_CHECK_EQ(SPIFFS_creat(FS, "test", 0), SPIFFS_OK);
1209+
spiffs_file fd = SPIFFS_open(FS, "test", SPIFFS_O_CREAT | SPIFFS_O_WRONLY, 0);
1210+
TEST_CHECK_GT(fd, 0);
1211+
1212+
int i;
1213+
for (i = 0; i < NUM; i++) {
1214+
TEST_CHECK_EQ(SPIFFS_write(FS, fd, buf, SIZ), SIZ);
1215+
}
1216+
TEST_CHECK_EQ(SPIFFS_close(FS, fd), SPIFFS_OK);
1217+
fd = SPIFFS_open(FS, "test", SPIFFS_O_RDONLY, 0);
1218+
TEST_CHECK_GT(fd, 0);
1219+
1220+
spiffs_stat s;
1221+
TEST_CHECK_EQ(SPIFFS_fstat(FS, fd, &s), SPIFFS_OK);
1222+
TEST_CHECK_EQ(s.size, NUM*SIZ);
1223+
1224+
int size = 0;
1225+
for (i = 0; i < NUM; i++) {
1226+
size += SPIFFS_read(FS, fd, buf, SIZ);
1227+
}
1228+
TEST_CHECK_EQ(size, NUM*SIZ);
1229+
1230+
return TEST_RES_OK;
1231+
} TEST_END
1232+
12021233

12031234
SUITE_TESTS(bug_tests)
12041235
ADD_TEST(nodemcu_full_fs_1)
@@ -1221,6 +1252,7 @@ SUITE_TESTS(bug_tests)
12211252
ADD_TEST(fuzzer_found_3)
12221253
ADD_TEST(fuzzer_found_4)
12231254
ADD_TEST(remove_release_fd_152)
1255+
ADD_TEST(certain_file_size_fail_165)
12241256
ADD_TEST_NON_DEFAULT(fuzzer_found_single_1)
12251257
ADD_TEST_NON_DEFAULT(log_afl_test)
12261258
ADD_TEST_NON_DEFAULT(afl_test)

src/test/test_spiffs.c

+8
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,14 @@ int read_and_verify_fd(spiffs_file fd, char *name) {
645645
printf(" read_and_verify: could not stat file %s\n", name);
646646
return res;
647647
}
648+
649+
off_t fsize = lseek(pfd, 0, SEEK_END);
650+
if (s.size != fsize) {
651+
printf(" read_and_verify: size differs, %s spiffs:%d!=fs:%ld\n", name, s.size, fsize);
652+
return -1;
653+
}
654+
lseek(pfd, 0, SEEK_SET);
655+
648656
if (s.size == 0) {
649657
SPIFFS_close(&__fs, fd);
650658
close(pfd);

0 commit comments

Comments
 (0)