]> granicus.if.org Git - php/commitdiff
Add fread(length) method
authordatibbaw <datibbaw@php.net>
Wed, 12 Feb 2014 06:06:29 +0000 (14:06 +0800)
committerTjerk Meesters <datibbaw@php.net>
Fri, 7 Mar 2014 10:50:33 +0000 (18:50 +0800)
Fixed off-by-one write bug

Added test

ext/spl/spl_directory.c
ext/spl/tests/bug65545.phpt [new file with mode: 0644]

index 6e53c7a9470a7c9e845d53f3578c032e4daaa9db..668977134e1c553fc6a044c3e1968df1fb269585 100644 (file)
@@ -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 (file)
index 0000000..b43f10f
--- /dev/null
@@ -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)