php / php/doc-en

stream_copy_to_stream() treats $offset of 0 as "current position"

Open
#2,046 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Category: Streams
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Documentation does not mention that $offset of 0 keeps $from's position rather than copying from 0th offset.

<?php
$a = fopen('php://memory', 'r+');
$b = fopen('php://memory', 'r+');
fwrite($a, '0123456789');
fseek($a, 3);
stream_copy_to_stream($a, $b, 2, 0);
rewind($b);
echo stream_get_contents($b);

This code outputs 34 and not 01 as suggested by the documentation.

To me, this looks more like an implementation issue because $offset may depend on external factors such as user's input, and code most often doesn't expect the function's seeking behaviour to change if the input is zero or non-zero.

Related code (note the pos > 0 condition):

https://github.com/php/php-src/blob/11b612af6dbe9fdd60edb58d37e441c97bff79e0/ext/standard/streamsfuncs.c#L481-L509

PHP_FUNCTION(stream_copy_to_stream)
{
	// ...
	if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) {

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the stream_copy_to_stream() documentation and compare its $offset wording with ext/standard/streamsfuncs.c around the linked lines, especially the pos > 0 condition. Run the provided PHP example to confirm the behavior; done means the documentation accurately describes zero-offset behavior, or the issue is routed to the implementation if that behavior is unintended.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.