Skip to content

fix: prevent infinite loop in Datafile plugin on empty files#390

Draft
toddr-bot wants to merge 1 commit intoabw:masterfrom
toddr-bot:koan.toddr.bot/fix-datafile-infinite-loop
Draft

fix: prevent infinite loop in Datafile plugin on empty files#390
toddr-bot wants to merge 1 commit intoabw:masterfrom
toddr-bot:koan.toddr.bot/fix-datafile-infinite-loop

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

What

Adds an EOF check to the header-reading loop in Template::Plugin::Datafile to prevent infinite loops.

Why

When a datafile is empty or contains only comment lines, the constructor's header-reading while loop spins forever: <FD> returns undef at EOF, ! $line evaluates as true (undef is falsy), and the loop re-enters indefinitely. This hangs any template that tries to load such a file.

How

One-line fix: last unless defined $line; after the filehandle read. When EOF is reached, the loop exits and the existing error check on line 53 correctly returns "first line of file must contain field names."

Testing

  • New t/datafile_edge.t with Test::More covering:
    • Empty file → returns error (not hang)
    • Comment-only file → returns error (not hang)
  • Existing t/datafile.t passes unchanged

🤖 Generated with Claude Code

…iles

The header-reading loop in Datafile.pm's constructor would spin forever
when the file was empty or contained only comment lines. The <FD> read
returns undef at EOF, but the loop condition `! $line` treats undef as
falsy, so it re-enters the loop and reads undef again indefinitely.

Add an early exit when the filehandle returns undef. The existing error
check on line 53 then correctly reports "first line of file must contain
field names."

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