Skip to content

Commit 26dd49a

Browse files
committed
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.
1 parent 0982020 commit 26dd49a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ script:
99
-include stdio.h -Werror' make all size
1010

1111
# run tests
12-
- CFLAGS="-DLFS_READ_SIZE=16 -DLFS_PROG_SIZE=16" make test
12+
- make test
13+
14+
# run tests with a few different configurations
1315
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test
1416
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test
17+
- CFLAGS="-DLFS_BLOCK_COUNT=1023" make test

lfs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ int lfs_deorphan(lfs_t *lfs);
262262
static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
263263
lfs_t *lfs = p;
264264

265-
lfs_block_t off = (block - lfs->free.start) % lfs->cfg->block_count;
265+
lfs_block_t off = (((lfs_soff_t)(block - lfs->free.start)
266+
% (lfs_soff_t)(lfs->cfg->block_count))
267+
+ lfs->cfg->block_count) % lfs->cfg->block_count;
268+
266269
if (off < lfs->cfg->lookahead) {
267270
lfs->free.lookahead[off / 32] |= 1U << (off % 32);
268271
}
@@ -994,6 +997,7 @@ static int lfs_index_extend(lfs_t *lfs,
994997
if (err) {
995998
return err;
996999
}
1000+
assert(*block >= 2 && *block <= lfs->cfg->block_count);
9971001

9981002
err = lfs_bd_erase(lfs, *block);
9991003
if (err) {

0 commit comments

Comments
 (0)