Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix GH-13203 file_put_contents fail on strings over 4GB on Windows #13205

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions ext/standard/tests/file/file_put_contents_5gb.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
Test file_put_contents() function with 5GB string
--SKIPIF--
<?php
if (getenv('SKIP_SLOW_TESTS')) {
die('skip slow test');
}

function get_system_memory(): int|float|false
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Windows-based memory check
@exec('wmic OS get FreePhysicalMemory', $output);
if (isset($output[1])) {
return (int)trim($output[1]);
}
} else {
// Unix/Linux-based memory check
$memInfo = @file_get_contents("/proc/meminfo");
if ($memInfo) {
preg_match('/MemFree:\s+(\d+) kB/', $memInfo, $matches);
return $matches[1] * 1024; // Convert to bytes
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false;
}
if (get_system_memory() < 10 * 1024 * 1024 * 1024) {
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
die('skip Reason: Insufficient RAM (less than 10GB)');
}
$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin";
$tmpfileh = fopen($tmpfile, "wb");
if ($tmpfileh === false) {
die('skip Reason: Unable to create temporary file');
}
fclose($tmpfileh);
unlink($tmpfile);
if (disk_free_space(dirname($tmpfile)) < 10 * 1024 * 1024 * 1024) {
die('skip Reason: Insufficient disk space (less than 10GB)');
}
?>
--INI--
memory_limit=6G
--CLEAN--
<?php
@unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin");
?>
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
--FILE--
<?php

$tmpfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "test_file_put_contents_5gb.bin";
$large_string = str_repeat('a', 5 * 1024 * 1024 * 1024);

$result = file_put_contents($tmpfile, $large_string);
if ($result !== strlen($large_string)) {
echo "Could only write $result bytes of " . strlen($large_string) . " bytes.";
var_dump(error_get_last());
} else {
echo "File written successfully.";
}
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
?>
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
--EXPECT--
File written successfully.
7 changes: 2 additions & 5 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# include "win32/time.h"
# include "win32/ioutil.h"
# include "win32/readdir.h"
# include <limits.h>
#endif

#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC)
Expand Down Expand Up @@ -353,11 +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;
if (ZEND_SIZE_T_UINT_OVFL(count)) {
count = UINT_MAX;
}
bytes_written = _write(data->fd, buf, (unsigned int)count);
ssize_t bytes_written = _write(data->fd, buf, (unsigned int)(count > INT_MAX ? INT_MAX : count));
#else
ssize_t bytes_written = write(data->fd, buf, count);
#endif
Expand Down