diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 1c49b26ac7d..1cfce579b02 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -419,6 +419,11 @@ PHP_FUNCTION(stream_get_contents) Z_PARAM_LONG(desiredpos) ZEND_PARSE_PARAMETERS_END(); + if (maxlen < 0 && maxlen != PHP_STREAM_COPY_ALL) { + php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to zero, or -1"); + RETURN_FALSE; + } + php_stream_from_zval(stream, zsrc); if (desiredpos >= 0) { diff --git a/ext/standard/tests/streams/stream_get_contents_negative_length.phpt b/ext/standard/tests/streams/stream_get_contents_negative_length.phpt new file mode 100644 index 00000000000..3d52729a2f2 --- /dev/null +++ b/ext/standard/tests/streams/stream_get_contents_negative_length.phpt @@ -0,0 +1,16 @@ +--TEST-- +stream_get_contents() with negative max length +--FILE-- + +--EXPECTF-- +string(2) "bc" + +Warning: stream_get_contents(): Length must be greater than or equal to zero, or -1 in %s on line %d +bool(false)