@@ -1572,6 +1572,7 @@ static int lfs_dir_commit_commit(void *p, lfs_tag_t tag, const void *buffer) {
15721572static int lfs_dir_compact (lfs_t * lfs ,
15731573 lfs_mdir_t * dir , const struct lfs_mattr * attrs , int attrcount ,
15741574 lfs_mdir_t * source , uint16_t begin , uint16_t end ) {
1575+ int counter = 0 ;
15751576 // save some state in case block is bad
15761577 const lfs_block_t oldpair [2 ] = {dir -> pair [0 ], dir -> pair [1 ]};
15771578 bool relocated = false;
@@ -1793,6 +1794,11 @@ static int lfs_dir_compact(lfs_t *lfs,
17931794 lfs_cache_drop (lfs , & lfs -> pcache );
17941795 if (!tired ) {
17951796 LFS_DEBUG ("Bad block at 0x%" PRIx32 , dir -> pair [1 ]);
1797+ if (counter > 10 ) {
1798+ LFS_DEBUG ("Exiting bad bad block loop after 10 retries" );
1799+ return LFS_ERR_CORRUPT ;
1800+ }
1801+ counter ++ ;
17961802 }
17971803
17981804 // can't relocate superblock, filesystem is now frozen
@@ -2321,6 +2327,8 @@ static int lfs_ctz_extend(lfs_t *lfs,
23212327 lfs_cache_t * pcache , lfs_cache_t * rcache ,
23222328 lfs_block_t head , lfs_size_t size ,
23232329 lfs_block_t * block , lfs_off_t * off ) {
2330+ int counter = 0 ;
2331+
23242332 while (true) {
23252333 // go ahead and grab a block
23262334 lfs_block_t nblock ;
@@ -2412,6 +2420,12 @@ static int lfs_ctz_extend(lfs_t *lfs,
24122420
24132421 // just clear cache and try a new block
24142422 lfs_cache_drop (lfs , pcache );
2423+
2424+ if (counter > 10 ) {
2425+ LFS_DEBUG ("Exiting bad bad block loop after 10 retries" );
2426+ return LFS_ERR_CORRUPT ;
2427+ }
2428+ counter ++ ;
24152429 }
24162430}
24172431#endif
@@ -2652,6 +2666,8 @@ static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file) {
26522666
26532667#ifndef LFS_READONLY
26542668static int lfs_file_relocate (lfs_t * lfs , lfs_file_t * file ) {
2669+ int counter = 0 ;
2670+
26552671 while (true) {
26562672 // just relocate what exists into new block
26572673 lfs_block_t nblock ;
@@ -2717,6 +2733,12 @@ static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
27172733
27182734 // just clear cache and try a new block
27192735 lfs_cache_drop (lfs , & lfs -> pcache );
2736+
2737+ if (counter > 10 ) {
2738+ LFS_DEBUG ("Exiting bad bad block loop after 10 retries" );
2739+ return LFS_ERR_CORRUPT ;
2740+ }
2741+ counter ++ ;
27202742 }
27212743}
27222744#endif
@@ -5432,4 +5454,3 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) {
54325454 return err ;
54335455}
54345456#endif
5435-
0 commit comments