Skip to content

Commit 030f5ab

Browse files
committed
Fix GH-18753: file_get_contents() and file_put_contents() fail with data >=2GB on macOS & BSD
1 parent 1863014 commit 030f5ab

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

ext/standard/tests/file/gh18753.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GH-18753 (file_get_contents() and file_put_contents() fail with data >=2GB on macOS & BSD)
3+
--CREDITS--
4+
Gregory House <[email protected]>
5+
--INI--
6+
memory_limit=-1
7+
--SKIPIF--
8+
<?php
9+
if (PHP_OS_FAMILY !== 'Darwin' && PHP_OS_FAMILY !== 'BSD') die('skip is not macOS or BSD');
10+
if (PHP_INT_SIZE !== 8) die("skip 64-bit only");
11+
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
12+
?>
13+
--FILE--
14+
<?php
15+
echo "-- file_put_contents() --\n";
16+
echo file_put_contents('bigfile', str_repeat('a', 2 ** 31));
17+
18+
echo "\n-- file_get_contents() --\n";
19+
echo strlen(file_get_contents('bigfile'));
20+
?>
21+
--CLEAN--
22+
<?php
23+
unlink('bigfile');
24+
?>
25+
--EXPECT--
26+
-- file_put_contents() --
27+
2147483648
28+
-- file_get_contents() --
29+
2147483648

main/streams/plain_wrapper.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ extern int php_get_uid_by_name(const char *name, uid_t *uid);
5353
extern int php_get_gid_by_name(const char *name, gid_t *gid);
5454
#endif
5555

56-
#if defined(PHP_WIN32)
56+
#if defined(PHP_WIN32) || defined(__APPLE__) || defined(__MACH__) || defined(BSD) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
5757
# define PLAIN_WRAP_BUF_SIZE(st) ((unsigned int)(st > INT_MAX ? INT_MAX : st))
58+
#else
59+
# define PLAIN_WRAP_BUF_SIZE(st) (st)
60+
#endif
61+
62+
#if defined(PHP_WIN32)
5863
#define fsync _commit
5964
#define fdatasync fsync
6065
#else
61-
# define PLAIN_WRAP_BUF_SIZE(st) (st)
6266
# if !defined(HAVE_FDATASYNC)
6367
# define fdatasync fsync
6468
# elif defined(__APPLE__)
@@ -357,7 +361,7 @@ static ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t coun
357361
#ifdef PHP_WIN32
358362
bytes_written = _write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
359363
#else
360-
bytes_written = write(data->fd, buf, count);
364+
bytes_written = write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
361365
#endif
362366
if (bytes_written < 0) {
363367
if (PHP_IS_TRANSIENT_ERROR(errno)) {

0 commit comments

Comments
 (0)