Skip to content

Fix librbf raw I/O, segmented line reads, and write-only file deletion - #83

Merged
tlindner merged 1 commit into
nitros9project:mainfrom
tlindner:fix/librbf-regressions
Oct 28, 2026
Merged

tlindner merged 1 commit into
nitros9project:mainfrom
tlindner:fix/librbf-regressions

Conversation

@tlindner

Copy link
Copy Markdown
Member

Summary

This PR fixes four independent bugs in librbf and adds a regression test for
each. The fixes address incorrect byte counts and file positions during reads
and writes, plus a crash when deleting a write-only file.

  1. Limit raw disk reads to the remaining image size
    =================================================

Problem:
_os9_read() checked whether a raw path was already at EOF, but did not limit a
request that crossed the end of the disk. For example, requesting 32 bytes with
only 16 remaining reported 32 bytes read and advanced the tracked file position
16 bytes past EOF. A read starting at EOF also returned EOS_EOF without resetting
the requested byte count to zero.

Fix:
Clamp the raw read size to the number of bytes remaining in the disk image. Set
the returned byte count to zero when the position is at or beyond EOF.

Regression test: test_os9_raw_read_end_of_disk

  • Reads the final bytes of the image to establish expected contents.
  • Requests more bytes than remain and verifies the short-read count and contents.
  • Checks that buffer space beyond the returned data is untouched.
  • Verifies that the position stops exactly at EOF.
  • Confirms that a subsequent read returns EOS_EOF, reports zero bytes, and leaves
    the buffer and position unchanged.
  1. Return the complete line length across allocation segments
    ============================================================

Problem:
When _os9_readln() encountered a carriage return, it set the returned byte count
to the number of bytes read from the current allocation segment. Bytes already
read from earlier segments were omitted. The buffer and file position reflected
the complete line, but the reported length did not.

Fix:
Include bytes accumulated from earlier segments when calculating the returned
line length.

Regression test: test_os9_readln_across_segments

  • Creates a file whose first data segment contains one sector.
  • Creates a second file to occupy the following space, forcing an append to use
    another segment.
  • Verifies that the test file actually has multiple allocation segments.
  • Reads a line containing six bytes from the first segment and three from the
    second.
  • Checks that the returned length is nine bytes, the contents are correct, and
    the position is immediately after the carriage return.
  • Reads and verifies the following line.

Before the fix, this test returned a length of 3 instead of 9.

  1. Delete write-only files without reopening them for reading
    ============================================================

Problem:
_os9_freefile() reopened the target file with FAM_READ to obtain its allocation
information. For a write-only file, the open failed with a permission error, but
the helper continued using the invalid path, causing a crash.

Fix:
Pass the already-open parent path and the target's file-descriptor sector number
to _os9_freefile(). Read the target's descriptor through that path and release
its data allocations using the parent path's bitmap. This removes the dependency
on read permission for the target.

Regression test: test_os9_delete_write_only_file

  • Creates a write-only file and writes data to it.
  • Records its descriptor and data allocations after closing the file.
  • Deletes the file and verifies that it can no longer be opened.
  • Checks the allocation bitmap to confirm that both the descriptor cluster and
    all data clusters were released.

The write-only deletion path crashed before the fix.

  1. Advance the tracked position after raw writes
    ===============================================

Problem:
The raw-path branch of _os9_write() returned the actual number of bytes written
but did not update path->filepos. Consequently, _os9_gs_pos() reported the old
offset, and a following _os9_read() sought back to that stale position.

Fix:
Advance the tracked position by the actual byte count returned by fwrite().

Regression test: test_os9_raw_write_position

  • Writes known data to unused space at the end of the test image.
  • Verifies that the tracked position advances by the number of bytes written.
  • Replaces part of the data and immediately reads from the same path.
  • Checks that the read starts after the replacement data and advances the
    position correctly.
  • Seeks back and verifies the replacement contents.

Validation

  • Each regression was reproduced before its corresponding fix.
  • make -C build/unix completes successfully.
  • All 36 tests across six suites pass, including all four new regression tests.
  • git diff --check passes.
  • No build-system files or compiler/debug flags were changed.

@tlindner
tlindner merged commit d701c6c into nitros9project:main Oct 28, 2026
6 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.

1 participant