Browse Source

Bug in truncate function #48

Unsigned overflow bug, and actually missing tests when
truncating to other than zero.
pull/51/head
Peter Andersson 9 years ago
parent
commit
98b0357265
  1. 2
      src/spiffs_nucleus.c
  2. 40
      src/test/test_bugreports.c

2
src/spiffs_nucleus.c

@ -1482,7 +1482,7 @@ s32_t spiffs_object_truncate(
SPIFFS_DBG("truncate: got data pix %04x\n", data_pix); SPIFFS_DBG("truncate: got data pix %04x\n", data_pix);
if (cur_size - SPIFFS_DATA_PAGE_SIZE(fs) >= new_size) { if (new_size == 0 || remove || cur_size - new_size >= SPIFFS_DATA_PAGE_SIZE(fs)) {
// delete full data page // delete full data page
res = spiffs_page_data_check(fs, fd, data_pix, data_spix); res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) { if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) {

40
src/test/test_bugreports.c

@ -373,10 +373,48 @@ TEST(zero_sized_file_44) {
TEST_CHECK_LT(res, 0); TEST_CHECK_LT(res, 0);
TEST_CHECK_EQ(SPIFFS_errno(FS), SPIFFS_ERR_END_OF_OBJECT); TEST_CHECK_EQ(SPIFFS_errno(FS), SPIFFS_ERR_END_OF_OBJECT);
return TEST_RES_OK;
} TEST_END(zero_sized_file_44)
TEST(truncate_48) {
fs_reset();
u32_t len = SPIFFS_DATA_PAGE_SIZE(FS)/2;
s32_t res = test_create_and_write_file("small", len, len);
TEST_CHECK_GE(res, 0);
spiffs_file fd = SPIFFS_open(FS, "small", SPIFFS_RDWR, 0);
TEST_CHECK_GE(fd, 0);
spiffs_fd *desc;
res = spiffs_fd_get(FS, fd, &desc);
TEST_CHECK_GE(res, 0);
TEST_CHECK_EQ(desc->size, len);
u32_t new_len = len/2;
res = spiffs_object_truncate(desc, new_len, 0);
TEST_CHECK_GE(res, 0);
TEST_CHECK_EQ(desc->size, new_len);
res = SPIFFS_close(FS, fd);
TEST_CHECK_GE(res, 0);
spiffs_stat s;
res = SPIFFS_stat(FS, "small", &s);
TEST_CHECK_GE(res, 0);
TEST_CHECK_EQ(s.size, new_len);
res = SPIFFS_remove(FS, "small");
TEST_CHECK_GE(res, 0);
fd = SPIFFS_open(FS, "small", SPIFFS_RDWR, 0);
TEST_CHECK_LT(fd, 0);
TEST_CHECK_EQ(SPIFFS_errno(FS), SPIFFS_ERR_NOT_FOUND);
return TEST_RES_OK; return TEST_RES_OK;
} TEST_END(zero_sized_file_44) } TEST_END(truncate_48)
SUITE_END(bug_tests) SUITE_END(bug_tests)

Loading…
Cancel
Save