summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/spl/spl_directory.c26
-rw-r--r--ext/spl/tests/bug65545.phpt23
2 files changed, 49 insertions, 0 deletions
diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
index 5f32feaaf0..fe253aaf39 100644
--- a/ext/spl/spl_directory.c
+++ b/ext/spl/spl_directory.c
@@ -2848,6 +2848,27 @@ SPL_METHOD(SplFileObject, fwrite)
RETURN_LONG(php_stream_write(intern->u.file.stream, str, str_len));
} /* }}} */
+SPL_METHOD(SplFileObject, fread)
+{
+ spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ long length = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE) {
+ return;
+ }
+
+ if (length <= 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
+ RETURN_FALSE;
+ }
+
+ Z_STRVAL_P(return_value) = emalloc(length + 1);
+ Z_STRLEN_P(return_value) = php_stream_read(intern->u.file.stream, Z_STRVAL_P(return_value), length);
+
+ Z_STRVAL_P(return_value)[length] = 0;
+ Z_TYPE_P(return_value) = IS_STRING;
+}
+
/* {{{ proto bool SplFileObject::fstat()
Stat() on a filehandle */
FileFunction(fstat)
@@ -2948,6 +2969,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fwrite, 0, 0, 1)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fread, 0, 0, 1)
+ ZEND_ARG_INFO(0, length)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_ftruncate, 0, 0, 1)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
@@ -2975,6 +3000,7 @@ static const zend_function_entry spl_SplFileObject_functions[] = {
SPL_ME(SplFileObject, fgetss, arginfo_file_object_fgetss, ZEND_ACC_PUBLIC)
SPL_ME(SplFileObject, fscanf, arginfo_file_object_fscanf, ZEND_ACC_PUBLIC)
SPL_ME(SplFileObject, fwrite, arginfo_file_object_fwrite, ZEND_ACC_PUBLIC)
+ SPL_ME(SplFileObject, fread, arginfo_file_object_fread, ZEND_ACC_PUBLIC)
SPL_ME(SplFileObject, fstat, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
SPL_ME(SplFileObject, ftruncate, arginfo_file_object_ftruncate, ZEND_ACC_PUBLIC)
SPL_ME(SplFileObject, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
diff --git a/ext/spl/tests/bug65545.phpt b/ext/spl/tests/bug65545.phpt
new file mode 100644
index 0000000000..b43f10f6e6
--- /dev/null
+++ b/ext/spl/tests/bug65545.phpt
@@ -0,0 +1,23 @@
+--TEST--
+SplFileObject::fread function
+--FILE--
+<?php
+$obj = new SplFileObject(__FILE__, 'r');
+$data = $obj->fread(5);
+var_dump($data);
+
+$data = $obj->fread();
+var_dump($data);
+
+$data = $obj->fread(0);
+var_dump($data);
+
+?>
+--EXPECTF--
+string(5) "<?php"
+
+Warning: SplFileObject::fread() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: SplFileObject::fread(): Length parameter must be greater than 0 in %s on line %d
+bool(false)