Skip to content

fix: honour depth==0 when rdepth exceeds MAX_DEPTH on wide structures#138

Draft
Koan-Bot wants to merge 2 commits into
garu:masterfrom
Koan-Bot:koan.atoomic/fix-depth-zero-rdepth-override
Draft

fix: honour depth==0 when rdepth exceeds MAX_DEPTH on wide structures#138
Koan-Bot wants to merge 2 commits into
garu:masterfrom
Koan-Bot:koan.atoomic/fix-depth-zero-rdepth-override

Conversation

@Koan-Bot

@Koan-Bot Koan-Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

What

Moves the depth == 0 early-return check before the rdepth > MAX_DEPTH iterative fallback in sv_clone().

Why

rdepth increments on every sv_clone() call — including sibling elements in flat arrays/hashes. A container with 4000+ elements pushes rdepth past MAX_DEPTH, causing the iterative fallback to deep-copy elements that should have been shared (depth=0 means "just SvREFCNT_inc").

For example, clone(\@array_of_5000_refs, 1) should shallow-copy the outer array and share every element. But elements past index ~4000 were silently deep-copied, breaking the depth contract. Non-clonable types (coderefs, globs) at high indices also triggered a spurious "depth limit exceeded" warning.

How

The depth == 0 check was at line 515, after the rdepth > MAX_DEPTH block at line 440. Moved it immediately after rdepth++, before any iterative path can intercept.

Testing

New t/28-depth-zero-wide.t (8 tests) covering:

  • Array/hash with 5000 elements cloned at depth=1 — late elements verified as shared
  • Mutation through shared late element visible in original
  • No spurious warnings for coderefs at high indices
  • Full test suite passes

🤖 Generated with Claude Code


Quality Report

Changes: 3 files changed, 90 insertions(+), 3 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

Koan-Bot and others added 2 commits May 6, 2026 06:59
rdepth increments on every sv_clone() call, including sibling elements
in flat arrays and hashes.  A container with more than MAX_DEPTH
elements (4000 on Linux/macOS, 2000 on Windows) pushed rdepth past the
threshold, causing the iterative fallback to deep-copy elements that
the caller requested to be shared (depth=0).

Observable symptoms:
- clone(\@big_array, 1) deep-copied elements past index ~4000 instead
  of sharing them — mutations through the clone were invisible to the
  original.
- Non-clonable types (coderefs, globs) at high indices triggered a
  spurious "depth limit exceeded" warning.

Fix: check depth==0 before the rdepth > MAX_DEPTH block so that the
"share, don't clone" semantic is always honoured regardless of call
count.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
install-with-cpm@v1 regressed on Perl 5.8-5.22 after a recent cpm
update requiring Perl 5.24+.  Bump to @v2 for action-based jobs
and use direct `cpm install` in the Docker-based Linux matrix
(where cpm is pre-installed).

Matches the approach approved by @atoomic in PR garu#120.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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