Repository navigation
Fix rbf create cleanup cluster - #111
Merged
DrPitre merged 2 commits intoNov 9, 2026
Merged
Conversation
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.
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 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_createtakes 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 byspc; this one now does too.2. Reads could run past the end of the segment list (
librbfread.c,librbfreadln.c)||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._os9_readand_os9_readlnreported the size that was asked for instead of the number of bytes actually read.segptr[NUM_SEGS], one entry past the array, before testing the index. They now test the index first._os9_writehad the same order and is fixed too._os9_readlnnow sets the size to 0 on that error, as_os9_readalready did.3. Clusters could be allocated past the end of the disk (
librbfbitmap.c,librbfopen.c,librbfwrite.c)_os9_getfreebitand_os9_getSASSegmentsearched the bitmap up todd_tot, which counts sectors, not clusters. The check in_os9_extendSegListfor 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 afterdd_mapin 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 atdd_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 withEOS_DFand 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_readand_os9_readlnmust 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 withEOS_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.