[php-src] PHP-8.2: Fix file_get_contents() on Windows fails with "errno=22 Invalid argument"

From: Date: Sun, 14 Apr 2024 13:26:41 +0000
Subject: [php-src] PHP-8.2: Fix file_get_contents() on Windows fails with "errno=22 Invalid argument"
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-128098@lists.php.net to get a copy of this message
Author: Damian Wójcik (DnwK98) Committer: Jakub Zelenka (bukka) Date: 2024-04-14T14:26:10+01:00 Commit: https://github.com/php/php-src/commit/8421cfda61cffc45efad2d6156f5042930aaa93e Raw diff: https://github.com/php/php-src/commit/8421cfda61cffc45efad2d6156f5042930aaa93e.diff Fix file_get_contents() on Windows fails with "errno=22 Invalid argument" Closes GH-13948 Changed paths: A ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt D ext/standard/tests/file/file_put_contents_5gb.phpt M NEWS M main/streams/plain_wrapper.c Diff: diff --git a/NEWS b/NEWS index 0b50a7af1ea27..b462b9b8344e0 100644 --- a/NEWS +++ b/NEWS @@ -41,6 +41,10 @@ PHP NEWS . Fixed bug GH-13891 (memleak and segfault when using ini_set with session.trans_sid_hosts). (nielsdos, kamil-tekiela) +- Standard: + . Fixed file_get_contents() on Windows fails with "errno=22 Invalid + argument". (Damian Wójcik) + - Streams: . Fixed bug GH-13264 (Part 1 - Memory leak on stream filter failure). (Jakub Zelenka) diff --git a/ext/standard/tests/file/file_put_contents_5gb.phpt b/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt similarity index 60% rename from ext/standard/tests/file/file_put_contents_5gb.phpt rename to ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt index d552d86279d03..854996a6481e7 100644 --- a/ext/standard/tests/file/file_put_contents_5gb.phpt +++ b/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt @@ -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) { @@ -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'); @@ -45,15 +45,27 @@ 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); @@ -61,7 +73,8 @@ if (file_exists($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. diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index c77bd1e2415a8..c92b76d196da0 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -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)) #define fsync _commit #define fdatasync fsync #else @@ -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

« previous php.cvs (#128098) next »