Skip to content

fix: reject cyclic directory graphs before rendering - #100

Merged
blocksmith314 merged 3 commits into
mainfrom
fix/75-directory-graph
Nov 26, 2026
Merged

blocksmith314 merged 3 commits into
mainfrom
fix/75-directory-graph

Conversation

@blocksmith314

@blocksmith314 blocksmith314 commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Validate the directory graph in RestoreBlocksFromPack before block parsing: root (kRootDirId) must exist; every non-root directory has exactly one parent; reject self-edges, root-as-child, unknown parents/children, duplicate edges/parents; BFS checks reachability and cycles.
  • The depth limit of 128 is a rendering limit only. unpack and stat accept deeper valid trees; tree, ls and dump return kCorruption instead of overflowing the recursive renderer.
  • Add an active-set and depth guard to RenderDirListing as defense in depth, so a future caller cannot recurse forever even if the load-time graph check is bypassed.
  • Document the directory-graph invariants and correct the minimum data block size (empty blocks invalid; a one-file block is 137 bytes).
  • No archive-format change.

Note: directory entries' entry_type is not used for validation. ScanDirectory currently re-adds each directory with a default EntryMeta, so existing archives do not reliably store DIRECTORY; membership in dir_entry_records is authoritative.

Tests

  • Release ctest: 201/201 passed
  • ASan ctest: 201/201 passed
  • DirectoryGraph.*: valid root-only/tree, missing root, self-cycle, root-as-child, multiple parents, disconnected cycle, unknown/missing parent/child, duplicate child, deep-chain validation, renderer depth guard, renderer cycle guard.
  • DirectoryGraph.DeepTreeUnpacksButDoesNotRender: packs and unpacks a 130-level tree successfully, then verifies tree rendering returns kCorruption cleanly.
  • MalformedArchive.RejectsCyclicDirectoryGraph: patches the root's child list to form a self-cycle, recomputes the metadata checksum, and expects kCorruption before rendering.

Performance gate

Against origin/main (7e4a23d):

  • pack, 5 runs: -1.9% wall time / +2.8% peak RSS
  • unpack, 10 runs: -2.0% wall time / +3.8% peak RSS
  • same-code A/A control: +3.1% peak RSS, confirming the RSS delta is measurement noise.

No attributable regression.

Closes #75

PackFileMeta::Deserialize accepted child maps without validating the directory graph, and RenderDirListing recursed without a visited set. A crafted archive whose root or a subdirectory lists an ancestor made ls, tree and dump recurse until stack overflow; repeated child edges could also blow up output exponentially.

Validate the graph after metadata deserialization: root presence, one parent per non-root directory, no self/root edges or duplicate parents, reachability and cycle checks via BFS, and a depth limit of 1024. Add an active-set and depth guard to the renderer as defense in depth, and cover both with unit and malformed-archive tests. Empty data blocks are also documented as invalid (the reader already rejects them).

No format change; archive bytes are unchanged.

Closes #75

@blocksmith314 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

LGTM. The graph validation is well structured:

  • root existence, one-parent-per-directory, self/root-as-child, unknown/missing/duplicate edges are rejected before traversal
  • BFS catches disconnected cycles via the reachability check and enforces the depth bound
  • RenderDirListingImpl adds a real active-set/depth guard for callers that bypass load-time validation
  • the entry_type caveat is handled correctly by using dir_entry_records membership

Verified locally: Release ctest 199/199, ASan ctest 199/199, legacy 1.0.0 stat/tree/ls OK, CI 5/5 green.

No blockers.

Minor suggestions:

  • DirectoryGraph.RejectsDepthAboveLimit covers the validator, but there is no renderer-depth test. A RendererRejectsDepth case (a chain above kMaxDirectoryDepth, rendered directly without ValidateDirectoryGraph) would cover the second guard.
  • The cyclic end-to-end test goes through unpack; since all four commands call RestoreBlocksFromPack first, that is reasonable. A tree/ls cyclic case would additionally pin the CLI wiring, but the unit renderer test already covers the defense-in-depth path.
  • The CN doc line adds “空 data block 是非法的” in both the directory-graph and metadata bullets; harmless duplication.
  • The +1.4% pack result was a first-run outlier and pack does not call this code; unpack -2.4% is the relevant number.

DirectoryGraph.RendererRejectsDepth exposed that a 1025-level chain overflows the ASan stack before the depth guard can return. Lower kMaxDirectoryDepth to 128 so both the loader and the recursive renderer stay within realistic stack budgets; real filesystem paths are far shallower.

Add the renderer-depth test and remove the duplicated empty-block note from the metadata bullet.

Refs #75

@blocksmith314 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

The cycle/tree validation is good, but the new depth cap is applied to all readers, including unpack and metadata-only stat, which do not recurse. That rejects valid archives produced by ctar itself.

Reproduced on b2058d9:

# created /tmp/ctar-deep with 130 nested directories and one file
$ ctar pack /tmp/ctar-deep /tmp/ctar-deep.ctar
pack file bytes: 71626 ...
pack exit=0

$ ctar unpack /tmp/ctar-deep.ctar /tmp/ctar-deep-out
failed to unpack archive ...: corrupt archive ...: directory graph: depth exceeds the limit 128
unpack exit=1

tree also fails, but it recurses and is the command the limit is intended to protect. unpack does not recurse and should be able to restore a valid deep tree. Legacy stat is similarly rejected even though it only parses metadata.

Suggested fix

  • Keep cycle/parent validation in ValidateDirectoryGraph() for all readers.
  • Move the depth check to the recursive renderer only, or make it an optional/parameterized check that ls/tree/dump pass and unpack/stat do not.
  • Keep the active-set/depth guard in RenderDirListingImpl — returning kCorruption for a too-deep tree is better than a stack overflow.
  • Adjust or remove DirectoryGraph.RejectsDepthAboveLimit (it currently asserts all readers reject); keep RendererRejectsDepth.
  • Update the docs to say 128 is a renderer limit, not an archive-reader/format invariant. If deep tree rendering should work, an iterative renderer is the real fix.

A regression test that packs ~130 nested directories and then unpacks successfully (while tree may return kCorruption) would pin this.

Verified locally: Release ctest 200/200, ASan ctest 200/200, legacy 1.0.0 stat/tree/ls OK, CI 5/5 green.

The depth cap was enforced by ValidateDirectoryGraph, so unpack and stat rejected valid deep trees produced by ctar even though they do not recurse. Keep the acyclic/single-parent/reachability validation for every reader, but move the depth bound to RenderDirListing only; unpack and stat now accept deep valid trees, while tree/ls/dump return kCorruption instead of overflowing the stack.

Add a regression test that packs and unpacks a tree deeper than the renderer limit and verifies rendering fails cleanly.

Refs #75

@blocksmith314 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 57409f8.

The previous blocker is fixed: the depth limit now belongs only to the recursive renderer.

  • ValidateDirectoryGraph() still checks root/parents/cycles/reachability but no longer rejects deep valid trees
  • unpack and metadata-only stat accept a 130-level tree
  • tree/ls/dump still return kCorruption from the renderer instead of overflowing
  • DeepTreeUnpacksButDoesNotRender covers the end-to-end behavior, and AcceptsDeepChain replaces the old validator-depth assertion
  • docs now describe 128 as a rendering limit, not an archive invariant

Verified locally: Release ctest 201/201, ASan ctest 201/201, legacy 1.0.0 stat/tree/ls OK, CI 5/5 green.

I also reproduced the CLI case: pack a 130-deep tree, unpack succeeds, stat succeeds, tree reports directory tree depth exceeds the limit 128 instead of crashing.

No blockers. The rendering limit is intentionally conservative for valid deep trees, but that is documented and the safer choice for the recursive renderer.

@blocksmith314
blocksmith314 merged commit fd9301b into main Nov 26, 2026
5 checks passed
@blocksmith314
blocksmith314 deleted the fix/75-directory-graph branch November 26, 2026 15:27
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.

[availability] Bug: cyclic directory graph makes ls/tree/dump recurse forever

1 participant