From 27b6cc829bcf0aa6f01f13240e0b9f70427652c2 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 23 Sep 2019 10:43:39 -0500 Subject: [PATCH 1/9] Fixed off-by-one null terminator in tests Found by mr-at-eo --- tests/test_paths.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_paths.sh b/tests/test_paths.sh index f720e39..cfdcd98 100755 --- a/tests/test_paths.sh +++ b/tests/test_paths.sh @@ -179,7 +179,7 @@ echo "--- Really big path test ---" scripts/test.py << TEST lfs_mount(&lfs, &cfg) => 0; memset(path, 'w', LFS_NAME_MAX); - path[LFS_NAME_MAX+1] = '\0'; + path[LFS_NAME_MAX] = '\0'; lfs_mkdir(&lfs, path) => 0; lfs_remove(&lfs, path) => 0; lfs_file_open(&lfs, &file, path, @@ -189,7 +189,7 @@ scripts/test.py << TEST memcpy(path, "coffee/", strlen("coffee/")); memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX); - path[strlen("coffee/")+LFS_NAME_MAX+1] = '\0'; + path[strlen("coffee/")+LFS_NAME_MAX] = '\0'; lfs_mkdir(&lfs, path) => 0; lfs_remove(&lfs, path) => 0; lfs_file_open(&lfs, &file, path, From 0b5a78e2cd5c0fc927a6828e01a0aee08d1f5e0d Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Mon, 30 Sep 2019 08:28:03 +1000 Subject: [PATCH 2/9] Adjust lfs_dir_find return code to ensure 32 bit value. lfs_dir_find returns either a negative return code or a tag. For 32 bit machines with int as 32 bits this co-incides, but for smaller bit processors, we need to ensure a 32 bit value is returned so change the return type to lfs_stag_t. --- lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index e2ab1e5..c8583ae 100644 --- a/lfs.c +++ b/lfs.c @@ -1062,7 +1062,7 @@ static int lfs_dir_find_match(void *data, return LFS_CMP_EQ; } -static int lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir, +static lfs_stag_t lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir, const char **path, uint16_t *id) { // we reduce path to a single name if we can find it const char *name = *path; From d40302c5e3defb024b317545ca810e6ba531aa2c Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Tue, 1 Oct 2019 13:51:52 +1000 Subject: [PATCH 3/9] lfs_rename: Cast error return codes to int. For correctness, cast the lfs_stag_t variables to int when returning a negative error code. --- lfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lfs.c b/lfs.c index c8583ae..d1d5435 100644 --- a/lfs.c +++ b/lfs.c @@ -3171,7 +3171,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { lfs_stag_t oldtag = lfs_dir_find(lfs, &oldcwd, &oldpath, NULL); if (oldtag < 0 || lfs_tag_id(oldtag) == 0x3ff) { LFS_TRACE("lfs_rename -> %d", (oldtag < 0) ? oldtag : LFS_ERR_INVAL); - return (oldtag < 0) ? oldtag : LFS_ERR_INVAL; + return (oldtag < 0) ? (int)oldtag : LFS_ERR_INVAL; } // find new entry @@ -3181,7 +3181,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { if ((prevtag < 0 || lfs_tag_id(prevtag) == 0x3ff) && !(prevtag == LFS_ERR_NOENT && newid != 0x3ff)) { LFS_TRACE("lfs_rename -> %d", (prevtag < 0) ? prevtag : LFS_ERR_INVAL); - return (prevtag < 0) ? prevtag : LFS_ERR_INVAL; + return (prevtag < 0) ? (int)prevtag : LFS_ERR_INVAL; } lfs_mdir_t prevdir; @@ -3202,7 +3202,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair); if (res < 0) { LFS_TRACE("lfs_rename -> %d", res); - return res; + return (int)res; } lfs_pair_fromle32(prevpair); From 97f86af4e914496d2e728df0186881476f2916b1 Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Tue, 1 Oct 2019 13:56:51 +1000 Subject: [PATCH 4/9] lfs_remove: Cast tag/error return codes to int. For correctness, cast the lfs_stag_t variables to int when returning a negative error code. --- lfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lfs.c b/lfs.c index d1d5435..046af95 100644 --- a/lfs.c +++ b/lfs.c @@ -3097,7 +3097,7 @@ int lfs_remove(lfs_t *lfs, const char *path) { lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); if (tag < 0 || lfs_tag_id(tag) == 0x3ff) { LFS_TRACE("lfs_remove -> %d", (tag < 0) ? tag : LFS_ERR_INVAL); - return (tag < 0) ? tag : LFS_ERR_INVAL; + return (tag < 0) ? (int)tag : LFS_ERR_INVAL; } lfs_mdir_t dir; @@ -3108,7 +3108,7 @@ int lfs_remove(lfs_t *lfs, const char *path) { LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair); if (res < 0) { LFS_TRACE("lfs_remove -> %d", res); - return res; + return (int)res; } lfs_pair_fromle32(pair); From 8cca58f1a6162592696ec7964c5bd7f2582de924 Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Tue, 1 Oct 2019 14:20:43 +1000 Subject: [PATCH 5/9] lfs_file_truncate: ensure lfs_file_seek return code is lsf_soff_t and cast error returns To ensure 16 bit devices do not invalidly truncate lfs_file_write return codes, change the return variable to be lfs_ssize_t which is the lfs_file_write return code and cast to int if it is a negative error code. --- lfs.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lfs.c b/lfs.c index 046af95..bf60852 100644 --- a/lfs.c +++ b/lfs.c @@ -3006,10 +3006,10 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } else if (size > oldsize) { // flush+seek if not already at end if (file->pos != oldsize) { - int err = lfs_file_seek(lfs, file, 0, LFS_SEEK_END); - if (err < 0) { - LFS_TRACE("lfs_file_truncate -> %d", err); - return err; + lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_END); + if (res < 0) { + LFS_TRACE("lfs_file_truncate -> %d", res); + return (int)res; } } @@ -3018,16 +3018,16 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %d", res); - return res; + return (int)res; } } } // restore pos - int err = lfs_file_seek(lfs, file, pos, LFS_SEEK_SET); - if (err < 0) { - LFS_TRACE("lfs_file_truncate -> %d", err); - return err; + lfs_soff_t res = lfs_file_seek(lfs, file, pos, LFS_SEEK_SET); + if (res < 0) { + LFS_TRACE("lfs_file_truncate -> %d", res); + return (int)res; } LFS_TRACE("lfs_file_truncate -> %d", 0); From 241dbc6f86fc37efb2e918b649a8ab0ba36b2155 Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Tue, 1 Oct 2019 14:22:01 +1000 Subject: [PATCH 6/9] lfs_stat: Cast error return codes to int. For correctness, cast the lfs_stag_t variables to int when returning a negative error code. --- lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index bf60852..e856758 100644 --- a/lfs.c +++ b/lfs.c @@ -3076,7 +3076,7 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); if (tag < 0) { LFS_TRACE("lfs_stat -> %d", tag); - return tag; + return (int)tag; } int err = lfs_dir_getinfo(lfs, &cwd, lfs_tag_id(tag), info); From 955b296bcc893cb78566355b2195ea51cb4a30f6 Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Tue, 1 Oct 2019 14:22:25 +1000 Subject: [PATCH 7/9] lfs_file_rewind: Cast error return codes to int. For correctness, cast the lfs_stag_t variables to int when returning a negative error code. --- lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index e856758..8bb3653 100644 --- a/lfs.c +++ b/lfs.c @@ -3047,7 +3047,7 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_rewind -> %d", res); - return res; + return (int)res; } LFS_TRACE("lfs_file_rewind -> %d", 0); From ba088aa2136986902ecff9711c12537650a7396f Mon Sep 17 00:00:00 2001 From: Sipke Vriend Date: Tue, 1 Oct 2019 15:24:17 +1000 Subject: [PATCH 8/9] lfs_dir_*: Cast error return codes to int. For correctness, cast the lfs_stag_t variables to int when returning a negative error code. --- lfs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lfs.c b/lfs.c index 8bb3653..95a3b6a 100644 --- a/lfs.c +++ b/lfs.c @@ -977,7 +977,7 @@ static int lfs_dir_fetch(lfs_t *lfs, lfs_mdir_t *dir, const lfs_block_t pair[2]) { // note, mask=-1, tag=0 can never match a tag since this // pattern has the invalid bit set - return lfs_dir_fetchmatch(lfs, dir, pair, -1, 0, NULL, NULL, NULL); + return (int)lfs_dir_fetchmatch(lfs, dir, pair, -1, 0, NULL, NULL, NULL); } static int lfs_dir_getgstate(lfs_t *lfs, const lfs_mdir_t *dir, @@ -1010,7 +1010,7 @@ static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir, lfs_stag_t tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x780, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_NAME, id, lfs->name_max+1), info->name); if (tag < 0) { - return tag; + return (int)tag; } info->type = lfs_tag_type3(tag); @@ -1019,7 +1019,7 @@ static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir, tag = lfs_dir_get(lfs, dir, LFS_MKTAG(0x700, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz); if (tag < 0) { - return tag; + return (int)tag; } lfs_ctz_fromle32(&ctz); From 4d068a154d10f8b40dbda17977a4c016c1193c45 Mon Sep 17 00:00:00 2001 From: Roy Kupershmid Date: Sun, 13 Oct 2019 19:58:11 +0300 Subject: [PATCH 9/9] Update README example code in accordance to the block_cycles change An addition to 38a2a8d. When executing the given example in README, you immediately get an assertion error because block_cycles is initiated to 0. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b50dd31..8017983 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ const struct lfs_config cfg = { .block_count = 128, .cache_size = 16, .lookahead_size = 16, + .block_cycles = 500, }; // entry point