Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return FALSE on seek past EOF #7324

Merged
merged 3 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libraries/LittleFS/src/LittleFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,16 @@ class LittleFSFileImpl : public FileImpl
if (mode == SeekEnd) {
offset = -offset; // TODO - this seems like its plain wrong vs. POSIX
}
auto lastPos = position();
int rc = lfs_file_seek(_fs->getFS(), _getFD(), offset, (int)mode); // NB. SeekMode === LFS_SEEK_TYPES
if (rc < 0) {
DEBUGV("lfs_file_seek rc=%d\n", rc);
return false;
}
if (position() > size()) {
seek(lastPos, SeekSet); // Pretend the seek() never happened
return false;
}
return true;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/host/fs/test_fs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ TEST_CASE(TESTPRE "open(w+) truncates file", TESTPAT)
REQUIRE( t == "");
}

TEST_CASE(TESTPRE "peek() returns -1 on EOF", TESTPAT)
{
FS_MOCK_DECLARE(64, 8, 512, "");
REQUIRE(FSTYPE.begin());
createFile("/file1", "some text");
auto f = FSTYPE.open("/file1", "r+");
REQUIRE(f.seek(8));
REQUIRE(f.peek() == 't');
REQUIRE(f.read() == 't');
REQUIRE(f.peek() == -1);
REQUIRE(f.read() == -1);
f.close();
}

TEST_CASE(TESTPRE "seek() pase EOF returns error (#7323)", TESTPAT)
{
FS_MOCK_DECLARE(64, 8, 512, "");
REQUIRE(FSTYPE.begin());
createFile("/file1", "some text");
auto f = FSTYPE.open("/file1", "r+");
REQUIRE_FALSE(f.seek(10));
f.close();
}

#ifdef FS_HAS_DIRS

#if FSTYPE != SDFS
Expand Down