Skip to content

Refuse dfs files shorter than 4 bytes before the native open - #87

Merged
ryan-kipawa merged 1 commit into
masterfrom
fix/85
Nov 11, 2026
Merged

ryan-kipawa merged 1 commit into
masterfrom
fix/85

Conversation

@ryan-kipawa

Copy link
Copy Markdown
Collaborator

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.Open has nothing to close. DfsFile.Open now 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 / dfsFileAppend

The 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 through DfsFile.Open, so all three modes are covered.

Evidence

test_failed_open_of_short_file_leaves_no_file_open counts 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:

  • Before: the 6 cases with 0 or 3 bytes fail; one file is left open each time (assert 19 == 18).
    After: all 9 pass.
  • The issue's script (600 opens of an empty file), run on Linux after the fix: every open gives 2001, it never changes to 2003, and the file can be deleted.

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

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>
@ryan-kipawa
ryan-kipawa merged commit d2048e6 into master Nov 11, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opening an empty (0-byte) dfs file leaves the file open; after ~509 such opens no file can be opened (error 2003)

1 participant