From 26dd49aa04a68033d41cf9bc73658087deead51a Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 17 Sep 2017 14:52:25 -0500 Subject: [PATCH] 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. --- .travis.yml | 5 ++++- lfs.c | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0651efc..b6c4f7a 100644 --- a/.travis.yml +++ b/.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 diff --git a/lfs.c b/lfs.c index 463c097..76a1b19 100644 --- a/lfs.c +++ b/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) {