com php-src: Add fread(length) method: ext/spl/spl_directory .c ext/spl/tests/bug65545.phpt
| From: | Tjerk Anne Meesters | Date: | Fri, 07 Mar 2014 10:50:33 +0000 |
| Subject: | com php-src: Add fread(length) method: ext/spl/spl_directory .c ext/spl/tests/bug65545.phpt | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-76384@lists.php.net to get a copy of this message | ||
Commit: 3c673fed3483ce772612d768a0638ea42d8dd7ac
Author: datibbaw <datibbaw@php.net> Wed, 12 Feb 2014 14:06:29 +0800
Committer: Tjerk Meesters <datibbaw@php.net> Fri, 7 Mar 2014 18:50:33 +0800
Parents: ecf757007258e0388b1b97694169cfaa2f4724c6
Branches: PHP-5.5 PHP-5.6 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=3c673fed3483ce772612d768a0638ea42d8dd7ac
Log:
Add fread(length) method
Fixed off-by-one write bug
Added test
Changed paths:
M ext/spl/spl_directory.c
A ext/spl/tests/bug65545.phpt
Diff:
diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
index 6e53c7a..6689771 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)
@@ -2947,6 +2968,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()
@@ -2974,6 +2999,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 0000000..b43f10f
--- /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)