-
Notifications
You must be signed in to change notification settings - Fork 102
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
Seeking before beginning of file should fail #157
Conversation
When seeking in a file to a location before the beginning of the file, the operation seems to succeed. While if i try to do the same with a "real" file then the seek command will return an error. So i changed the seek method to return false if the offset is less than 0. This seems to solve my problem and behaves in the same way as seeking in a real file. However I do not know if there are situations where seeking before the beginning of the file could be a valid use case. testing with fseek on unix fs always returns -1 when seeking past the beginning of the file. Added five test assertions to validate this behavior.
Running the test:
Outputs: So it seems seeking past the end of the file is OK, but before the beginning is not. Maybe someone can test on different filesystem. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for detecting this and the PR! See my inline comments.
if ($this->offset<0) { | ||
$this->offset = $old_offset; | ||
return false; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it should be the other way around. Instead of changing the internal offset value and reset it if it's not valid, the new value should be calculated, then checked if it is valid, and only when it is valid the internal offset value should be changed to the new value. This way data corruption can be prevented in case something stupid happens, and it also makes it more failure proof against future changes.
Also, let's stick to camelCase for variable names as all other variable names are already camelCase.
@@ -129,6 +129,8 @@ public function seekEmptyFile() | |||
$this->assertEquals(0, $this->file->getBytesRead()); | |||
$this->assertTrue($this->file->seek(2, SEEK_END)); | |||
$this->assertEquals(2, $this->file->getBytesRead()); | |||
$this->assertFalse($this->file->seek(-1, SEEK_SET),'Seek before beginning of file'); | |||
$this->assertEquals(2, $this->file->getBytesRead()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the existing test cases don't follow the "roughly one assertion per test" rule, but I really think new tests should do this. Otherwise it is harder to see what broke when a test fails. In this case two new test methods should be added, one for testing against the empty file and one against a non-empty file.
Implemented pull request feedback: - Refactor seek so internal offset is not modified when the seek fails. - Moved "seek before beginning" assertions into their own test methods.
I changed the code according to your review comments (I hope). I don't know how the review process on GIT works and if I have to re-request a review somewhere, or is it automatic? |
Thank you, just merged it. |
When seeking in a file to a location before the beginning of the file,
the operation seems to succeed. While if i try to do the same with a
"real" file then the seek command will return an error.
So i changed the seek method to return false if the offset is less
than 0. This seems to solve my problem and behaves in the same
way as seeking in a real file. However I do not know if there are
situations where seeking before the beginning of the file could be
a valid use case. testing with fseek on unix fs always returns -1
when seeking past the beginning of the file. Added five test
assertions to validate this behavior.