fix: allow CACHE_SIZE=1 and preserve newlines in interpolated vars - #461
Draft
toddr-bot wants to merge 2 commits into
Draft
fix: allow CACHE_SIZE=1 and preserve newlines in interpolated vars#461toddr-bot wants to merge 2 commits into
toddr-bot wants to merge 2 commits into
Conversation
The _store() method crashed when evicting from a single-slot cache because it unconditionally dereferenced $slot->[PREV] to disconnect the tail node. When HEAD == TAIL (only one slot), PREV is undef. The existing workaround clamped CACHE_SIZE=1 to 2 in _init() with the comment "it breaks things and the additional checking isn't worth it." Fix the root cause instead: - Guard the PREV dereference with a truthy check - Prevent self-referencing when HEAD == TAIL by comparing $head != $slot before linking - Remove the CACHE_SIZE=1 clamp from _init() Add t/provider_cache.t covering CACHE_SIZE 0, 1, 2, negative values, eviction cycling, and content verification through Template.pm.
In interpolate_text(), the line counter for ${variable} directives
used tr/\n/ / which both counts newlines AND replaces them with
spaces in $dir. The adjacent lines for plain text (557) and
unmatched $ references (568) correctly use tr/\n// (count only).
The replacement corrupted the stored directive text — multiline
${variable} references had their newlines converted to spaces in
the token array, affecting DEBUG output that displays original
template source.
Change tr/\n/ / to tr/\n// to match the pattern used everywhere
else in split_text() and interpolate_text().
Add t/interp_newline.t verifying newline preservation, line number
tracking, and end-to-end INTERPOLATE mode rendering.
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.
What
Two independent bug fixes in Provider and Parser, with tests.
Why
Provider (CACHE_SIZE=1): The
_store()linked list eviction code crashes whenHEAD == TAIL(single-slot cache) because it unconditionally dereferences$slot->[PREV], which isundeffor the only node. The existing workaround silently clamped CACHE_SIZE=1 to 2 in_init(). This fixes the root cause and removes the clamp, allowing a legitimate configuration.Parser (interpolate_text): Line 563 used
tr/\n/ /instead oftr/\n//— a typo that replaces newlines with spaces in the stored$dirtext for${variable}references. The two adjacent counting lines (557, 568) and all lines insplit_text()correctly usetr/\n//(count only, no modification). The corruption affects DEBUG output that displays original template source.How
$slot->[PREV]before dereferencing; compare$head != $slotto prevent self-referencing when reusing the sole cache slot. Remove the$size == 1clamp from_init().tr/\n/ /totr/\n//on one line.Testing
t/provider_cache.t: CACHE_SIZE 0, 1, 2, negative values, eviction cycling, reload, alternation, and content verification throughTemplate.pm.t/interp_newline.t: Verifies newline preservation in${...}tokens, line number tracking, and end-to-end INTERPOLATE mode.Quality Report
Changes: 4 files changed, 234 insertions(+), 9 deletions(-)
Code scan: clean
Tests: passed (OK)
Branch hygiene: clean
Generated by Kōan