Skip to content

fix: handle negative offset in sysread tied READ method#388

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-sysread-negative-offset
Draft

fix: handle negative offset in sysread tied READ method#388
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-sysread-negative-offset

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

What

Handle negative offsets in the tied READ method, matching the existing WRITE behavior.

Why

sysread($fh, $buf, $len, -N) with a negative offset exceeding the buffer length would crash with Perl's substr error instead of producing the proper "Offset outside string" warning with EINVAL — unlike syswrite which already handled this correctly.

How

Normalize negative offsets to positive (like WRITE already does): offset = buf_len + offset. If still negative after normalization, warn and return undef with EINVAL.

Testing

3 new test subtests in t/sysreadwrite_edge_cases.t:

  • Valid negative offset (-3 on 5-byte buffer) — places read data correctly
  • Out-of-bounds negative offset (-10 on 2-byte buffer) — warns + EINVAL
  • Edge case offset -1 — reads into last position

All existing sysread/syswrite tests continue to pass.

🤖 Generated with Claude Code

The READ method did not normalize negative offsets before using them,
unlike the WRITE method which converts negative offsets to positive
and validates bounds. When sysread was called with a negative offset
exceeding the buffer length (e.g., sysread($fh, $buf, 10, -100) on
a 2-byte buffer), Perl's substr would die instead of producing the
proper "Offset outside string" warning with EINVAL.

Now matches WRITE behavior: negative offsets are converted to positive
(offset = buf_len + offset), and out-of-bounds negatives get a clean
warning + EINVAL return.

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