Skip to content

Fix rbf create cleanup cluster - #111

Merged
DrPitre merged 2 commits into
nitros9project:mainfrom
tlindner:fix-rbf-create-cleanup-cluster
Nov 9, 2026
Merged

DrPitre merged 2 commits into
nitros9project:mainfrom
tlindner:fix-rbf-create-cleanup-cluster

Conversation

@tlindner

@tlindner tlindner commented Nov 9, 2026

Copy link
Copy Markdown
Member

Summary

Fixes four related librbf bugs that could corrupt an OS-9 disk image or return data from the wrong file. Each fix comes with a unit test that fails without it.

1. A failed create freed the wrong cluster (librbfopen.c)

_os9_create takes a cluster for the new file's descriptor, then adds the directory entry. If adding the entry failed (for example, the directory had to grow on a full disk), the cleanup called _os9_delbit(bitmap, newLSN, 1). That passed a sector number where the bitmap expects a cluster number. On disks with more than one sector per cluster, this freed an unrelated cluster that was in use and leaked the descriptor's cluster. The other two cleanup paths already divided by spc; this one now does too.

2. Reads could run past the end of the segment list (librbfread.c, librbfreadln.c)

  • The loop that finds the starting segment used || instead of &&. It skipped over the empty entry that ends the list, so if a descriptor claimed more bytes than its segments hold, a read could return another file's data.
  • When the list ended early, _os9_read and _os9_readln reported the size that was asked for instead of the number of bytes actually read.
  • The checks after that loop read segptr[NUM_SEGS], one entry past the array, before testing the index. They now test the index first. _os9_write had the same order and is fixed too.
  • _os9_readln now sets the size to 0 on that error, as _os9_read already did.

3. Clusters could be allocated past the end of the disk (librbfbitmap.c, librbfopen.c, librbfwrite.c)

_os9_getfreebit and _os9_getSASSegment searched the bitmap up to dd_tot, which counts sectors, not clusters. The check in _os9_extendSegList for growing a file into the next cluster had no upper limit. On a disk with more than one sector per cluster, or where the bytes after dd_map in the map's sector are zero, these could hand out clusters that don't exist. A new helper, _os9_total_clusters(), returns the number of whole clusters on the disk, capped at dd_map * 8. All three searches are now limited by it.

Tests (unittest/librbftest.c)

  • test_os9_failed_create_frees_its_cluster: fills the root directory of a disk with 2 sectors per cluster and leaves exactly one free cluster. The create must fail with EOS_DF and leave the bitmap unchanged.
  • test_os9_read_stops_at_segment_list_end: a descriptor's size claims more than its segment list holds, and a leftover entry after the end points at another file. _os9_read and _os9_readln must return only the bytes the list holds, then return an error and read nothing more.
  • test_os9_no_allocation_past_last_cluster: the map is full and its padding is zeroed. Creating a file, growing an empty file, and growing a file that ends at the disk's last cluster must each fail with EOS_DF, without growing the image or changing the map.

I checked that each test fails without its fix. All 15 unit test suites pass with make -C build/unix.

When _os9_create had allocated a file descriptor but then could not
write the directory entry (for example, the directory needed to grow on
a full disk), it released the descriptor with
_os9_delbit(bitmap, newLSN, 1). The bitmap holds clusters, not sectors,
so on a disk with more than one sector per cluster this cleared the bit
for an unrelated in-use cluster and leaked the descriptor's cluster.
The other two failure paths already divide by spc.

Add a test that fills the root directory of a two-sector-cluster disk,
leaves one free cluster, and checks that the failed create leaves the
allocation map unchanged.
Reads:
- The search for the starting segment in _os9_read and _os9_readln used
  || where it needed &&, so it walked past the empty entry that ends the
  segment list and could read another file's data when a descriptor
  claimed more bytes than its list holds.
- Both reported the requested size rather than what was read when the
  list ended early.
- The checks after the search read segptr[NUM_SEGS] before testing the
  index; check the index first (also in _os9_write).
- _os9_readln now sets the size to 0 on that error, like _os9_read.

Allocation:
- _os9_getfreebit and _os9_getSASSegment searched up to dd_tot, a sector
  count, and the contiguous-extension check in _os9_extendSegList did
  not bound the next cluster at all.  With more than one sector per
  cluster, or with zeros after dd_map in the map's sector, they could
  hand out clusters past the end of the disk.  Add _os9_total_clusters()
  (whole clusters, at most dd_map * 8) and bound all three by it.

Tests: test_os9_read_stops_at_segment_list_end and
test_os9_no_allocation_past_last_cluster.
@DrPitre
DrPitre merged commit a279beb into nitros9project:main Nov 9, 2026
8 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.

2 participants