From: datibbaw Date: Wed, 12 Feb 2014 06:06:29 +0000 (+0800) Subject: Add fread(length) method X-Git-Tag: php-5.5.11RC1~28 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c673fed3483ce772612d768a0638ea42d8dd7ac;p=php Add fread(length) method Fixed off-by-one write bug Added test --- diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index 6e53c7a947..668977134e 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 0000000000..b43f10f6e6 --- /dev/null +++ b/ext/spl/tests/bug65545.phpt @@ -0,0 +1,23 @@ +--TEST-- +SplFileObject::fread function +--FILE-- +fread(5); +var_dump($data); + +$data = $obj->fread(); +var_dump($data); + +$data = $obj->fread(0); +var_dump($data); + +?> +--EXPECTF-- +string(5) "