Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faulty intialization in spiffs_create_object #184

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build/run_valgrind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
valgrind -v --show-reachable=yes --track-origins=yes --leak-check=full ./linux_spiffs_test &> valgrind_output.txt

8 changes: 7 additions & 1 deletion src/spiffs_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ static spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix)
int i;
for (i = 0; i < cache->cpage_count; i++) {
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
if (NULL == cp)
{
return 0;
}
if ((cache->cpage_use_map & (1<<i)) &&
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
cp->pix == pix ) {
Expand Down Expand Up @@ -165,6 +169,7 @@ s32_t spiffs_phys_rd(
if (res2 != SPIFFS_OK) {
// honor read failure before possible write failure (bad idea?)
res = res2;
return res2;
}
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
_SPIFFS_MEMCPY(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
Expand All @@ -174,6 +179,7 @@ s32_t spiffs_phys_rd(
if (res2 != SPIFFS_OK) {
// honor read failure before possible write failure (bad idea?)
res = res2;
return res2;
}
}
}
Expand Down Expand Up @@ -277,7 +283,7 @@ void spiffs_cache_fd_release(spiffs *fs, spiffs_cache_page *cp) {
}
spiffs_cache_page_free(fs, cp->ix, 0);

cp->obj_id = 0;
cp->obj_id = SPIFFS_OBJ_ID_DELETED;
}

#endif
Expand Down
8 changes: 5 additions & 3 deletions src/spiffs_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ s32_t spiffs_gc_quick(
// Checks if garbage collecting is necessary. If so a candidate block is found,
// cleansed and erased
s32_t spiffs_gc_check(
spiffs *fs,
u32_t len) {
s32_t res;
spiffs *fs,
u32_t len
)
{
s32_t res = SPIFFS_OK;
s32_t free_pages =
(SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs)) * (fs->block_count-2)
- fs->stats_p_allocated - fs->stats_p_deleted;
Expand Down
17 changes: 11 additions & 6 deletions src/spiffs_hydrogen.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) {
SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
}
SPIFFS_LOCK(fs);
spiffs_obj_id obj_id;
spiffs_obj_id obj_id = SPIFFS_OBJ_ID_DELETED;
s32_t res;

res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path);
Expand Down Expand Up @@ -245,7 +245,7 @@ spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs

if ((flags & SPIFFS_O_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
#if !SPIFFS_READ_ONLY
spiffs_obj_id obj_id;
spiffs_obj_id obj_id = SPIFFS_OBJ_ID_DELETED;
// no need to enter conflicting name here, already looked for it above
res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);
if (res < SPIFFS_OK) {
Expand Down Expand Up @@ -727,7 +727,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) {
(void)fh;
spiffs_page_object_ix_header objix_hdr;
spiffs_obj_id obj_id;
spiffs_obj_id obj_id = SPIFFS_OBJ_ID_DELETED;
s32_t res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,
SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
SPIFFS_API_CHECK_RES(fs, res);
Expand Down Expand Up @@ -1026,12 +1026,17 @@ static s32_t spiffs_read_dir_v(
void *user_var_p) {
(void)user_const_p;
s32_t res;
spiffs_page_object_ix_header objix_hdr;
spiffs_page_object_ix_header objix_hdr = {.p_hdr = {0}};
if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
(obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {
return SPIFFS_VIS_COUNTINUE;
}

if (NULL == user_var_p)
{
return SPIFFS_VIS_END;
}

spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
Expand Down Expand Up @@ -1062,8 +1067,8 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {
}
SPIFFS_LOCK(d->fs);

spiffs_block_ix bix;
int entry;
spiffs_block_ix bix = 0;
int entry = 0;
s32_t res;
struct spiffs_dirent *ret = 0;

Expand Down
105 changes: 56 additions & 49 deletions src/spiffs_nucleus.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static s32_t spiffs_page_data_check(spiffs *fs, spiffs_fd *fd, spiffs_page_ix pi
return SPIFFS_ERR_INDEX_REF_INVALID;
}
#if SPIFFS_PAGE_CHECK
spiffs_page_header ph;
spiffs_page_header ph = {0};
res = _spiffs_rd(
fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
fd->file_nbr,
Expand Down Expand Up @@ -45,7 +45,7 @@ static s32_t spiffs_page_index_check(spiffs *fs, spiffs_fd *fd, spiffs_page_ix p
return SPIFFS_ERR_INDEX_INVALID;
}
#if SPIFFS_PAGE_CHECK
spiffs_page_header ph;
spiffs_page_header ph = {0};
res = _spiffs_rd(
fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
fd->file_nbr,
Expand Down Expand Up @@ -233,7 +233,8 @@ s32_t spiffs_erase_block(
// here we ignore res, just try erasing the block
while (size > 0) {
SPIFFS_DBG("erase "_SPIPRIad":"_SPIPRIi"\n", addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));
SPIFFS_HAL_ERASE(fs, addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));
res = SPIFFS_HAL_ERASE(fs, addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));
SPIFFS_CHECK_RES(res);

addr += SPIFFS_CFG_PHYS_ERASE_SZ(fs);
size -= SPIFFS_CFG_PHYS_ERASE_SZ(fs);
Expand Down Expand Up @@ -275,10 +276,10 @@ s32_t spiffs_probe(

// Read three magics, as one block may be in an aborted erase state.
// At least two of these must contain magic and be in decreasing order.
spiffs_obj_id magic[3];
spiffs_obj_id bix_count[3];
spiffs_obj_id magic[3] = {0};
spiffs_obj_id bix_count[3] = {0};

spiffs_block_ix bix;
spiffs_block_ix bix = {0};
for (bix = 0; bix < 3; bix++) {
paddr = SPIFFS_MAGIC_PADDR(&dummy_fs, bix);
#if SPIFFS_HAL_CALLBACK_EXTRA
Expand Down Expand Up @@ -347,7 +348,7 @@ static s32_t spiffs_obj_lu_scan_v(
// Checks magic if enabled
s32_t spiffs_obj_lu_scan(
spiffs *fs) {
s32_t res;
s32_t res = SPIFFS_OK;
spiffs_block_ix bix;
int entry;
#if SPIFFS_USE_MAGIC
Expand All @@ -362,7 +363,7 @@ s32_t spiffs_obj_lu_scan(
spiffs_obj_id erase_count_max = 0;
while (bix < fs->block_count) {
#if SPIFFS_USE_MAGIC
spiffs_obj_id magic;
spiffs_obj_id magic = 0;
res = _spiffs_rd(fs,
SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
0, SPIFFS_MAGIC_PADDR(fs, bix) ,
Expand All @@ -379,7 +380,7 @@ s32_t spiffs_obj_lu_scan(
}
}
#endif
spiffs_obj_id erase_count;
spiffs_obj_id erase_count = 0;
res = _spiffs_rd(fs,
SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
0, SPIFFS_ERASE_COUNT_PADDR(fs, bix) ,
Expand Down Expand Up @@ -451,8 +452,9 @@ s32_t spiffs_obj_lu_find_free(
spiffs_block_ix starting_block,
int starting_lu_entry,
spiffs_block_ix *block_ix,
int *lu_entry) {
s32_t res;
int *lu_entry)
{
s32_t res = SPIFFS_OK;
if (!fs->cleaning && fs->free_blocks < 2) {
res = spiffs_gc_quick(fs, 0);
if (res == SPIFFS_ERR_NO_DELETED_BLOCKS) {
Expand Down Expand Up @@ -505,8 +507,8 @@ static s32_t spiffs_obj_lu_find_id_and_span_v(
int ix_entry,
const void *user_const_p,
void *user_var_p) {
s32_t res;
spiffs_page_header ph;
s32_t res = SPIFFS_OK;
spiffs_page_header ph = {0};
spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
res = _spiffs_rd(fs, 0, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_header), (u8_t *)&ph);
Expand All @@ -530,9 +532,9 @@ s32_t spiffs_obj_lu_find_id_and_span(
spiffs_span_ix spix,
spiffs_page_ix exclusion_pix,
spiffs_page_ix *pix) {
s32_t res;
spiffs_block_ix bix;
int entry;
s32_t res = SPIFFS_OK;
spiffs_block_ix bix = {0};
int entry = 0;

res = spiffs_obj_lu_find_entry_visitor(fs,
fs->cursor_block_ix,
Expand Down Expand Up @@ -569,9 +571,9 @@ s32_t spiffs_obj_lu_find_id_and_span_by_phdr(
spiffs_span_ix spix,
spiffs_page_ix exclusion_pix,
spiffs_page_ix *pix) {
s32_t res;
spiffs_block_ix bix;
int entry;
s32_t res = SPIFFS_OK;
spiffs_block_ix bix = {0};
int entry = 0;

res = spiffs_obj_lu_find_entry_visitor(fs,
fs->cursor_block_ix,
Expand Down Expand Up @@ -670,7 +672,7 @@ static s32_t spiffs_populate_ix_map_v(
const void *user_const_p,
void *user_var_p) {
(void)user_const_p;
s32_t res;
s32_t res = SPIFFS_OK;
spiffs_ix_map_populate_state *state = (spiffs_ix_map_populate_state *)user_var_p;
spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);

Expand Down Expand Up @@ -710,7 +712,7 @@ static s32_t spiffs_populate_ix_map_v(

// populates index map, from vector entry start to vector entry end, inclusive
s32_t spiffs_populate_ix_map(spiffs *fs, spiffs_fd *fd, u32_t vec_entry_start, u32_t vec_entry_end) {
s32_t res;
s32_t res = SPIFFS_OK;
spiffs_ix_map *map = fd->ix_map;
spiffs_ix_map_populate_state state;
vec_entry_start = MIN((u32_t)(map->end_spix - map->start_spix), vec_entry_start);
Expand Down Expand Up @@ -760,8 +762,8 @@ s32_t spiffs_page_allocate_data(
u8_t finalize,
spiffs_page_ix *pix) {
s32_t res = SPIFFS_OK;
spiffs_block_ix bix;
int entry;
spiffs_block_ix bix = {0};
int entry= 0;

// find free entry
res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
Expand Down Expand Up @@ -817,12 +819,12 @@ s32_t spiffs_page_move(
spiffs_page_header *page_hdr,
spiffs_page_ix src_pix,
spiffs_page_ix *dst_pix) {
s32_t res;
s32_t res = SPIFFS_OK;
u8_t was_final = 0;
spiffs_page_header *p_hdr;
spiffs_block_ix bix;
int entry;
spiffs_page_ix free_pix;
spiffs_page_header *p_hdr = NULL;
spiffs_block_ix bix = 0;
int entry = 0;
spiffs_page_ix free_pix = 0;

// find free entry
res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
Expand Down Expand Up @@ -876,7 +878,7 @@ s32_t spiffs_page_move(
s32_t spiffs_page_delete(
spiffs *fs,
spiffs_page_ix pix) {
s32_t res;
s32_t res = SPIFFS_OK;
// mark deleted entry in source object lookup
spiffs_obj_id d_obj_id = SPIFFS_OBJ_ID_DELETED;
res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_DELE,
Expand Down Expand Up @@ -910,16 +912,18 @@ s32_t spiffs_page_delete(
#if !SPIFFS_READ_ONLY
// Create an object index header page with empty index and undefined length
s32_t spiffs_object_create(
spiffs *fs,
spiffs_obj_id obj_id,
const u8_t name[],
const u8_t meta[],
spiffs_obj_type type,
spiffs_page_ix *objix_hdr_pix) {
s32_t res = SPIFFS_OK;
spiffs_block_ix bix;
spiffs_page_object_ix_header oix_hdr;
int entry;
spiffs * fs,
spiffs_obj_id obj_id,
const u8_t name[],
const u8_t meta[],
spiffs_obj_type type,
spiffs_page_ix * objix_hdr_pix
)
{
s32_t res = SPIFFS_OK;
spiffs_block_ix bix = 0;
spiffs_page_object_ix_header oix_hdr = {.p_hdr = {0}};
int entry = 0;

res = spiffs_gc_check(fs, SPIFFS_DATA_PAGE_SIZE(fs));
SPIFFS_CHECK_RES(res);
Expand All @@ -944,7 +948,7 @@ s32_t spiffs_object_create(
oix_hdr.p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED);
oix_hdr.type = type;
oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page
strncpy((char*)oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
strncpy((char*)oix_hdr.name, (const char*)name, sizeof(oix_hdr.name));
#if SPIFFS_OBJ_META_LEN
if (meta) {
_SPIFFS_MEMCPY(oix_hdr.meta, meta, SPIFFS_OBJ_META_LEN);
Expand Down Expand Up @@ -987,8 +991,8 @@ s32_t spiffs_object_update_index_hdr(
u32_t size,
spiffs_page_ix *new_pix) {
s32_t res = SPIFFS_OK;
spiffs_page_object_ix_header *objix_hdr;
spiffs_page_ix new_objix_hdr_pix;
spiffs_page_object_ix_header *objix_hdr = NULL;
spiffs_page_ix new_objix_hdr_pix = 0;

obj_id |= SPIFFS_OBJ_ID_IX_FLAG;

Expand All @@ -1007,7 +1011,7 @@ s32_t spiffs_object_update_index_hdr(

// change name
if (name) {
strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
strncpy((char*)objix_hdr->name, (const char*)name, sizeof(objix_hdr->name));
}
#if SPIFFS_OBJ_META_LEN
if (meta) {
Expand Down Expand Up @@ -1051,7 +1055,7 @@ void spiffs_cb_object_event(
#endif
// update index caches in all file descriptors
spiffs_obj_id obj_id = obj_id_raw & ~SPIFFS_OBJ_ID_IX_FLAG;
u32_t i;
u32_t i = 0;
spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
SPIFFS_DBG(" CALLBACK %s obj_id:"_SPIPRIid" spix:"_SPIPRIsp" npix:"_SPIPRIpg" nsz:"_SPIPRIi"\n", (const char *[]){"UPD", "NEW", "DEL", "MOV", "HUP","???"}[MIN(ev,5)],
obj_id_raw, spix, new_pix, new_size);
Expand Down Expand Up @@ -1158,7 +1162,7 @@ s32_t spiffs_object_open_by_id(
spiffs_flags flags,
spiffs_mode mode) {
s32_t res = SPIFFS_OK;
spiffs_page_ix pix;
spiffs_page_ix pix = 0;

res = spiffs_obj_lu_find_id_and_span(fs, obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &pix);
SPIFFS_CHECK_RES(res);
Expand All @@ -1177,8 +1181,8 @@ s32_t spiffs_object_open_by_page(
spiffs_mode mode) {
(void)mode;
s32_t res = SPIFFS_OK;
spiffs_page_object_ix_header oix_hdr;
spiffs_obj_id obj_id;
spiffs_page_object_ix_header oix_hdr = {.p_hdr = {0}};
spiffs_obj_id obj_id = SPIFFS_OBJ_ID_DELETED;

res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&oix_hdr);
Expand Down Expand Up @@ -2133,7 +2137,7 @@ static s32_t spiffs_obj_lu_find_free_obj_id_compact_v(spiffs *fs, spiffs_obj_id
s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8_t *conflicting_name) {
s32_t res = SPIFFS_OK;
u32_t max_objects = (fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) / 2;
spiffs_free_obj_id_state state;
spiffs_free_obj_id_state state = {0};
spiffs_obj_id free_obj_id = SPIFFS_OBJ_ID_FREE;
state.min_obj_id = 1;
state.max_obj_id = max_objects + 1;
Expand All @@ -2148,7 +2152,10 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8
u32_t i, j;
SPIFFS_DBG("free_obj_id: BITM min:"_SPIPRIid" max:"_SPIPRIid"\n", state.min_obj_id, state.max_obj_id);

memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
if (fs->work != NULL)
{
memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
}
res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v,
conflicting_name, &state.min_obj_id, 0, 0);
if (res == SPIFFS_VIS_END) res = SPIFFS_OK;
Expand Down
Loading