Skip to content

Commit 207dab5

Browse files
committed
Fix #71882: Negative ftruncate() on php://memory exhausts memory
We must not pass negative sizes to a size_t parameter.
1 parent c7b4cd1 commit 207dab5

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ PHP NEWS
4848
. Fixed bug #72278 (getimagesize returning FALSE on valid jpg). (cmb)
4949
. Fixed bug #65550 (get_browser() incorrectly parses entries with "+" sign).
5050
(cmb)
51+
. Fixed bug #71882 (Negative ftruncate() on php://memory exhausts memory).
52+
(cmb)
5153

5254
- XML:
5355
. Fixed bug #72085 (SEGV on unknown address zif_xml_parse). (cmb)

ext/standard/file.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,11 @@ PHP_NAMED_FUNCTION(php_if_ftruncate)
15121512
RETURN_FALSE;
15131513
}
15141514

1515+
if (size < 0) {
1516+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative size is not supported");
1517+
RETURN_FALSE;
1518+
}
1519+
15151520
PHP_STREAM_TO_ZVAL(stream, &fp);
15161521

15171522
if (!php_stream_truncate_supported(stream)) {

ext/standard/tests/file/bug71882.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Bug #71882 (Negative ftruncate() on php://memory exhausts memory)
3+
--FILE--
4+
<?php
5+
$fd = fopen("php://memory", "w+");
6+
ftruncate($fd, -1);
7+
?>
8+
==DONE==
9+
--EXPECTF--
10+
Warning: ftruncate(): Negative size is not supported in %s%ebug71882.php on line %d
11+
==DONE==

0 commit comments

Comments
 (0)