This causes ReadAt to return a nil error when ReadAt reads less than the length of the give []byte, while the contract requires it to return a non-nil error.
A secondary bug, consequence of the problem above, is that using the fstest.TestFS(...) func of the standard library fails if the tested files have any content. This happens because it delegates testing ReadAt of the given fs.FS of the test to iotest.TestReader(...), which, in turn, makes an assertion on the condition described above.
In practical terms, I found this issue when I introduced afero and specificaly the MemMapFs to make some assertions on code I was writing that's supposed to generate and write files, and used fstest.TestFS(...) to check for the presence of the expected files I expected my code to generate.
It's an easy fix. Will soon send a PR with tests for both *mem.File and NewIOFS (i.e., use MemMapFs), and the fix.
The current implementation of
*mem.File
violates the contract defined by the Standard Library'sio.ReaderAt
interface.Specifically, according to the docs for
io.ReaderAt
:The current implementation simply delegates
ReadAt
toRead
:https://github.com/spf13/afero/blob/100c9a6d7b9b70ddb10dc728b9b22d472e1f7f71/mem/file.go#L208-L214
This causes ReadAt to return a nil error when
ReadAt
reads less than the length of the give[]byte
, while the contract requires it to return a non-nil error.A secondary bug, consequence of the problem above, is that using the
fstest.TestFS(...)
func of the standard library fails if the tested files have any content. This happens because it delegates testingReadAt
of the givenfs.FS
of the test toiotest.TestReader(...)
, which, in turn, makes an assertion on the condition described above.In practical terms, I found this issue when I introduced
afero
and specificaly theMemMapFs
to make some assertions on code I was writing that's supposed to generate and write files, and usedfstest.TestFS(...)
to check for the presence of the expected files I expected my code to generate.It's an easy fix. Will soon send a PR with tests for both
*mem.File
andNewIOFS
(i.e.,use MemMapFs
), and the fix.