Repository navigation
Refuse dfs files shorter than 4 bytes before the native open - #87
Merged
Merged
Conversation
The native dfsFileRead, dfsFileEdit and dfsFileAppend leave the file open when they can not read the first 4-byte tag (Error code 2001), and return null pointers, so DfsFile.Open has nothing to close. Each such open used up a file handle until no file could be opened (2003). DfsFile.Open now raises the same error for these files before calling the native library. Fixes #85 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #85. The native reader leaves the file open when it can't read the first 4-byte tag (Error code 2001). It also returns null pointers, so
DfsFile.Openhas nothing to close.DfsFile.Opennow refuses these files before calling the native library and raises the same error users get today:DfsFile.Open(filename, mode) if not a file: raise FileNotFoundError if Edit/Append and read-only: raise "File is readonly …" + if size < 4 bytes: raise "Could not load file … (Error code 2001)" dfsFileRead / dfsFileEdit / dfsFileAppendThe issue suggested refusing only 0-byte files, but files of 1–3 bytes leak the same way. Files of 4 bytes or more fail with other codes (2014, 2017, 1, 2019) and leak nothing. All open paths (
DfsFileFactory,DfsuFile,Dfs123File) go throughDfsFile.Open, so all three modes are covered.Evidence
test_failed_open_of_short_file_leaves_no_file_opencounts open file descriptors before and after a failed open, for each(size, code)in(0, 2001), (3, 2001), (4, 2014)and each of Read, Edit and Append:assert 19 == 18).After: all 9 pass.
Not covered: the test runs on Linux only (it counts
/proc/self/fd), and the fix was verified only on Linux. The issue was reported on Windows. The check runs before any native call, so it should behave the same there, but someone should run the issue's script once on Windows before closing #85.Merge Danger
Door: two-way
Blast Radius: small
A file shorter than 4 bytes can never be a valid dfs file, so the only change users see is that the failed open no longer leaves the file open. The error message and code are unchanged.
🤖 Generated with Claude Code