mirror of
https://gitee.com/openharmony/third_party_littlefs
synced 2025-02-10 17:52:25 +00:00
Fixed positive seek bounds checking
This bug was a result of an annoying corner case around intermingling signed and unsigned offsets. The boundary check that prevents seeking a file to a position before the file was preventing valid seeks with positive offsets. This corner case is a bit more complicated than it looks because the offset is signed, while the size of the file is unsigned. Simply casting both to signed or unsigned offsets won't handle large files.
This commit is contained in:
parent
be22d3449f
commit
aea3d3db46
4
lfs.c
4
lfs.c
@ -1635,13 +1635,13 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
|
||||
if (whence == LFS_SEEK_SET) {
|
||||
file->pos = off;
|
||||
} else if (whence == LFS_SEEK_CUR) {
|
||||
if ((lfs_off_t)-off > file->pos) {
|
||||
if (off < 0 && (lfs_off_t)-off > file->pos) {
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
file->pos = file->pos + off;
|
||||
} else if (whence == LFS_SEEK_END) {
|
||||
if ((lfs_off_t)-off > file->size) {
|
||||
if (off < 0 && (lfs_off_t)-off > file->size) {
|
||||
return LFS_ERR_INVAL;
|
||||
}
|
||||
|
||||
|
@ -133,6 +133,14 @@ tests/test.py << TEST
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
lfs_file_seek(&lfs, &file[0], size, LFS_SEEK_CUR) => 3*size;
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
lfs_file_seek(&lfs, &file[0], pos, LFS_SEEK_SET) => pos;
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
@ -174,6 +182,14 @@ tests/test.py << TEST
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_CUR) => size;
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
lfs_file_seek(&lfs, &file[0], size, LFS_SEEK_CUR) => 3*size;
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
||||
lfs_file_seek(&lfs, &file[0], pos, LFS_SEEK_SET) => pos;
|
||||
lfs_file_read(&lfs, &file[0], buffer, size) => size;
|
||||
memcmp(buffer, "kittycatcat", size) => 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user