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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Test file_put_contents() function with 5GB string
Test file_put_contents() and file_get_contents() functions with 5GB string
--SKIPIF--
<?php
if (PHP_INT_SIZE < 5) {
Expand Down Expand Up @@ -30,7 +30,7 @@ function get_system_memory(): int|float|false
if (get_system_memory() < 10 * 1024 * 1024 * 1024) {
die('skip Reason: Insufficient RAM (less than 10GB)');
}
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin";
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin";
$tmpfileh = fopen($tmpfile, "wb");
if ($tmpfileh === false) {
die('skip Reason: Unable to create temporary file');
Expand All @@ -45,23 +45,36 @@ if (disk_free_space(dirname($tmpfile)) < 10 * 1024 * 1024 * 1024) {
memory_limit=6G
--FILE--
<?php
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin";
$large_string = str_repeat('a', 5 * 1024 * 1024 * 1024);
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin";
$large_string_len = 5 * 1024 * 1024 * 1024;

$large_string = str_repeat('a', $large_string_len);
$result = file_put_contents($tmpfile, $large_string);
if ($result !== strlen($large_string)) {
echo "Could only write $result bytes of " . strlen($large_string) . " bytes.";
if ($result !== $large_string_len) {
echo "Could only write $result bytes of $large_string_len bytes.";
var_dump(error_get_last());
} else {
echo "File written successfully.";
echo "File written successfully." . PHP_EOL;
}
unset($large_string);

$result_large_string = file_get_contents($tmpfile);
if (strlen($result_large_string) !== $large_string_len) {
echo "Could only read " . strlen($result_large_string) . " bytes of $large_string_len bytes.";
var_dump(error_get_last());
} else {
echo "File read successfully." . PHP_EOL;
}

clearstatcache(true, $tmpfile);
if (file_exists($tmpfile)) {
unlink($tmpfile);
}
?>
--CLEAN--
<?php
@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin");
@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin");
?>
--EXPECT--
File written successfully.
File read successfully.
4 changes: 2 additions & 2 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern int php_get_gid_by_name(const char *name, gid_t *gid);
#endif

#if defined(PHP_WIN32)
# define PLAIN_WRAP_BUF_SIZE(st) (((st) > UINT_MAX) ? UINT_MAX : (unsigned int)(st))
# define PLAIN_WRAP_BUF_SIZE(st) ((unsigned int)(st > INT_MAX ? INT_MAX : st))
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch. I think it is possible to simplify line 356/360, what do you think ?

#define fsync _commit
#define fdatasync fsync
#else
Expand Down Expand Up @@ -354,7 +354,7 @@ static ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t coun

if (data->fd >= 0) {
#ifdef PHP_WIN32
ssize_t bytes_written = _write(data->fd, buf, (unsigned int)(count > INT_MAX ? INT_MAX : count));
ssize_t bytes_written = _write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
#else
ssize_t bytes_written = write(data->fd, buf, count);
#endif
Expand Down