瀏覽代碼

Fixed issue with negative modulo with unaligned lookaheads

When the lookahead buffer wraps around in an unaligned filesystem, it's
possible for blocks at the beginning of the disk to have a negative distance
from the lookahead, but still reside in the lookahead buffer.

Switching to signed modulo doesn't quite work due to how negative modulo
is implemented in C, so the simple solution is to shift the region to be
positive.
Christopher Haster 8 年之前
父節點
當前提交
26dd49aa04
共有 2 個文件被更改,包括 9 次插入2 次删除
  1. 4 1
      .travis.yml
  2. 5 1
      lfs.c

+ 4 - 1
.travis.yml

@@ -9,6 +9,9 @@ script:
         -include stdio.h -Werror' make all size
 
     # run tests
-    - CFLAGS="-DLFS_READ_SIZE=16  -DLFS_PROG_SIZE=16"  make test
+    - make test
+
+    # run tests with a few different configurations
     - CFLAGS="-DLFS_READ_SIZE=1   -DLFS_PROG_SIZE=1"   make test
     - CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test
+    - CFLAGS="-DLFS_BLOCK_COUNT=1023" make test

+ 5 - 1
lfs.c

@@ -262,7 +262,10 @@ int lfs_deorphan(lfs_t *lfs);
 static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
     lfs_t *lfs = p;
 
-    lfs_block_t off = (block - lfs->free.start) % lfs->cfg->block_count;
+    lfs_block_t off = (((lfs_soff_t)(block - lfs->free.start)
+                % (lfs_soff_t)(lfs->cfg->block_count))
+            + lfs->cfg->block_count) % lfs->cfg->block_count;
+
     if (off < lfs->cfg->lookahead) {
         lfs->free.lookahead[off / 32] |= 1U << (off % 32);
     }
@@ -994,6 +997,7 @@ static int lfs_index_extend(lfs_t *lfs,
         if (err) {
             return err;
         }
+        assert(*block >= 2 && *block <= lfs->cfg->block_count);
 
         err = lfs_bd_erase(lfs, *block);
         if (err) {