Skip to content

Commit 0982020

Browse files
committed
Fixed issue with cold-write after seek to block boundary
This off-by-one error was caused by a slight difference between the pos argument to lfs_index_find and lfs_index_extend. When pos is on a block boundary, lfs_index_extend expects block to point before pos, as it would when writing a file linearly. But when seeking to that pos, the lfs_index_find to warm things up just supplies the block it expects pos to be in. Fixed the off-by-one error and added a test case for several of these cold seek+writes.
1 parent c2283a2 commit 0982020

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lfs.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ static int lfs_index_find(lfs_t *lfs,
975975
return err;
976976
}
977977

978+
assert(head >= 2 && head <= lfs->cfg->block_count);
978979
current -= 1 << skip;
979980
}
980981

@@ -1054,6 +1055,8 @@ static int lfs_index_extend(lfs_t *lfs,
10541055
return err;
10551056
}
10561057
}
1058+
1059+
assert(head >= 2 && head <= lfs->cfg->block_count);
10571060
}
10581061

10591062
*off = 4*skips;
@@ -1428,18 +1431,17 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
14281431
// check if we need a new block
14291432
if (!(file->flags & LFS_F_WRITING) ||
14301433
file->off == lfs->cfg->block_size) {
1431-
if (!(file->flags & LFS_F_WRITING)) {
1434+
if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
14321435
// find out which block we're extending from
14331436
int err = lfs_index_find(lfs, &file->cache, NULL,
14341437
file->head, file->size,
1435-
file->pos, &file->block, &file->off);
1438+
file->pos-1, &file->block, &file->off);
14361439
if (err) {
14371440
return err;
14381441
}
14391442

14401443
// mark cache as dirty since we may have read data into it
14411444
file->cache.block = 0xffffffff;
1442-
file->flags |= LFS_F_WRITING;
14431445
}
14441446

14451447
// extend file with new blocks
@@ -1450,6 +1452,8 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
14501452
if (err) {
14511453
return err;
14521454
}
1455+
1456+
file->flags |= LFS_F_WRITING;
14531457
}
14541458

14551459
// program as much as we can in current block

tests/test_seek.sh

+28
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,33 @@ tests/test.py << TEST
277277
lfs_unmount(&lfs) => 0;
278278
TEST
279279

280+
echo "--- Boundary seek and write ---"
281+
tests/test.py << TEST
282+
lfs_mount(&lfs, &cfg) => 0;
283+
lfs_file_open(&lfs, &file[0], "hello/kitty42", LFS_O_RDWR) => 0;
284+
285+
size = strlen("hedgehoghog");
286+
const lfs_soff_t offsets[] = {512, 1020, 513, 1021, 511, 1019};
287+
288+
for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
289+
lfs_soff_t off = offsets[i];
290+
memcpy(buffer, "hedgehoghog", size);
291+
lfs_file_seek(&lfs, &file[0], off, LFS_SEEK_SET) >= 0 => 1;
292+
lfs_file_write(&lfs, &file[0], buffer, size) => size;
293+
lfs_file_seek(&lfs, &file[0], off, LFS_SEEK_SET) => off+size;
294+
lfs_file_read(&lfs, &file[0], buffer, size) => size;
295+
memcmp(buffer, "hedgehoghog", size) => 0;
296+
297+
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_SET) => off+size;
298+
lfs_file_read(&lfs, &file[0], buffer, size) => size;
299+
memcmp(buffer, "kittycatcat", size) => 0;
300+
301+
lfs_file_sync(&lfs, &file[0]) => 0;
302+
}
303+
304+
lfs_file_close(&lfs, &file[0]) => 0;
305+
lfs_unmount(&lfs) => 0;
306+
TEST
307+
280308
echo "--- Results ---"
281309
tests/stats.py

0 commit comments

Comments
 (0)