Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ext/standard/tests/streams/bug78326.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
memory allocation on stream_get_contents()
--INI--
memory_limit=32M
--FILE--
<?php
$f = tmpfile();
fwrite($f, '.');

$chunks = array();
for ($i = 0; $i < 1000; ++$i) {
rewind($f);
$chunks[] = stream_get_contents($f, 1000000);
}
var_dump(count($chunks));
?>
--EXPECT--
int(1000)
10 changes: 10 additions & 0 deletions ext/standard/tests/streams/bug78326_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
proper string length on stream_get_contents()
--FILE--
<?php
$f = fopen('php://memory', 'rw');
fwrite($f, str_repeat('X', 1000));
fseek($f, 0);
var_dump(strlen(stream_get_contents($f, 1024)));
--EXPECT--
int(1000)
7 changes: 6 additions & 1 deletion main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,8 +1418,13 @@ PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int
ptr += ret;
}
if (len) {
*ptr = '\0';
ZSTR_LEN(result) = len;
ZSTR_VAL(result)[len] = '\0';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use the same logic as in https://github.com/php/php-src/blob/master/main/streams/streams.c#L759 and only truncate if the "wastage" is large enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic thanks! I guess that the potential savings of the truncate are not worth the extra performance costs of the function and memory copy calls if the "waste" is not large enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dropped the ZSTR_LEN(result) = len assignment, which is still needed in case we don't truncate. I'm surprised this doesn't cause test failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic you're right, it didn't I'm afraid, any ideas for a test that maybe we could add?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in any case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?php
$f = fopen('php://memory', 'rw');
fwrite($f, str_repeat('X', 1000));
fseek($f, 0); 
var_dump(strlen(stream_get_contents($f, 1024)));

should show the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic it did, thanks! I added the additional test just to be on the safe side


/* Only truncate if the savings are large enough */
if (len < maxlen / 2) {
result = zend_string_truncate(result, len, persistent);
}
} else {
zend_string_free(result);
result = NULL;
Expand Down