From 3802a303c1c3a064023df8a21295400161e76134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20W=C3=B3jcik?= Date: Fri, 12 Apr 2024 15:17:45 +0200 Subject: [PATCH 1/4] FIX file_get_contents() on Windows fails with "errno=22 Invalid argument" --- main/streams/plain_wrapper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index c77bd1e2415a8..4acaab7e4e3d1 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 From 9ef8bbea49f415d83d4da1c620d043c9c9479e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20W=C3=B3jcik?= Date: Fri, 12 Apr 2024 16:03:11 +0200 Subject: [PATCH 2/4] Simplify buffer size for write also. --- main/streams/plain_wrapper.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index 4acaab7e4e3d1..683a060644709 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -353,11 +353,8 @@ static ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t coun assert(data != NULL); if (data->fd >= 0) { -#ifdef PHP_WIN32 - 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 + ssize_t bytes_written = write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count)); + if (bytes_written < 0) { if (PHP_IS_TRANSIENT_ERROR(errno)) { return 0; From 0b8154df6771f3052fdfe9d50ea8128200764fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20W=C3=B3jcik?= Date: Fri, 12 Apr 2024 16:07:59 +0200 Subject: [PATCH 3/4] Revert oversimplification --- main/streams/plain_wrapper.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index 683a060644709..c92b76d196da0 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -353,8 +353,11 @@ static ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t coun assert(data != NULL); if (data->fd >= 0) { - ssize_t bytes_written = write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count)); - +#ifdef PHP_WIN32 + ssize_t bytes_written = _write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count)); +#else + ssize_t bytes_written = write(data->fd, buf, count); +#endif if (bytes_written < 0) { if (PHP_IS_TRANSIENT_ERROR(errno)) { return 0; From 4d0be19688bac7b336e2f615783a07b151e7a3ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20W=C3=B3jcik?= Date: Fri, 12 Apr 2024 17:31:23 +0200 Subject: [PATCH 4/4] Add test for reading big files by file_get_contents. --- ...e_get_contents_file_put_contents_5gb.phpt} | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) rename ext/standard/tests/file/{file_put_contents_5gb.phpt => file_get_contents_file_put_contents_5gb.phpt} (60%) 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-- --CLEAN-- --EXPECT-- File written successfully. +File read successfully.