Skip to content

Fix GH-18753: file_get_contents() and file_put_contents() fail with data >=2GB on macOS & BSD #18755

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

Open
wants to merge 1 commit into
base: PHP-8.3
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions ext/standard/tests/file/gh18753.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
GH-18753 (file_get_contents() and file_put_contents() fail with data >=2GB on macOS & BSD)
--CREDITS--
Gregory House <[email protected]>
--INI--
memory_limit=-1
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== 'Darwin' && PHP_OS_FAMILY !== 'BSD') die('skip is not macOS or BSD');
if (PHP_INT_SIZE !== 8) die("skip 64-bit only");
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
?>
--FILE--
<?php
echo "-- file_put_contents() --\n";
echo file_put_contents('bigfile', str_repeat('a', 2 ** 31));
Copy link
Member

Choose a reason for hiding this comment

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

As it's quite a lot memory, I wonder if we should add some skip if there is not enough system memory. But it needs to work on Mac to be actually useful and not like https://github.com/php/php-src/blob/91becb304286942337ad65ec614cc0d1a3f79cd6/ext/standard/tests/file/file_get_contents_file_put_contents_5gb.phpt which is actually the reason why we didn't notice this (that test is always skipped on OSX and not sure if it works on FreeBSD either.

Copy link
Member

Choose a reason for hiding this comment

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

the name needs to be test specific - something like gh18753.data for example


echo "\n-- file_get_contents() --\n";
echo strlen(file_get_contents('bigfile'));
?>
--CLEAN--
<?php
unlink('bigfile');
?>
--EXPECT--
-- file_put_contents() --
2147483648
-- file_get_contents() --
2147483648
10 changes: 7 additions & 3 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ extern int php_get_uid_by_name(const char *name, uid_t *uid);
extern int php_get_gid_by_name(const char *name, gid_t *gid);
#endif

#if defined(PHP_WIN32)
#if defined(PHP_WIN32) || defined(__APPLE__) || defined(__MACH__) || defined(BSD) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to do a configure-time check for this?

Copy link
Member

Choose a reason for hiding this comment

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

I think it might be just better to invert and allow it only for Linux

Copy link
Member

Choose a reason for hiding this comment

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

I'm not really sure if Solaris can handle it either (can't be bother to check) so safest option should be to just do #ifdef __linux__ and invert the logic...

Copy link
Member

Choose a reason for hiding this comment

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

It would probably be salient to also add a comment explaining why then. (the BSD and Windows cause should explain pretty much all cases).

(FWIW, AIX does seem to allow reads as big as off_t on 64-bit systems...)

# define PLAIN_WRAP_BUF_SIZE(st) ((unsigned int)(st > INT_MAX ? INT_MAX : st))
#else
# define PLAIN_WRAP_BUF_SIZE(st) (st)
#endif

#if defined(PHP_WIN32)
#define fsync _commit
#define fdatasync fsync
#else
# define PLAIN_WRAP_BUF_SIZE(st) (st)
# if !defined(HAVE_FDATASYNC)
# define fdatasync fsync
# elif defined(__APPLE__)
Expand Down Expand Up @@ -357,7 +361,7 @@ static ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t coun
#ifdef PHP_WIN32
bytes_written = _write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
#else
bytes_written = write(data->fd, buf, count);
bytes_written = write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
#endif
if (bytes_written < 0) {
if (PHP_IS_TRANSIENT_ERROR(errno)) {
Expand Down
Loading