mirror of
https://github.com/php/php-src.git
synced 2024-09-21 18:07:23 +00:00
Fix GH-8273: SplFileObject: key() returns wrong value
This commit is contained in:
parent
4d6965dcec
commit
6186ecd436
3
NEWS
3
NEWS
@ -22,6 +22,7 @@ PHP NEWS
|
|||||||
- SPL:
|
- SPL:
|
||||||
. Fixed bug GH-8366 (ArrayIterator may leak when calling __construct()).
|
. Fixed bug GH-8366 (ArrayIterator may leak when calling __construct()).
|
||||||
(cmb)
|
(cmb)
|
||||||
|
. Fixed bug GH-8273 (SplFileObject: key() returns wrong value). (Girgias)
|
||||||
|
|
||||||
- Streams:
|
- Streams:
|
||||||
. Fixed php://temp does not preserve file-position when switched to temporary
|
. Fixed php://temp does not preserve file-position when switched to temporary
|
||||||
@ -95,7 +96,7 @@ PHP NEWS
|
|||||||
- Reflection:
|
- Reflection:
|
||||||
. Fixed bug GH-8080 (ReflectionClass::getConstants() depends on def. order).
|
. Fixed bug GH-8080 (ReflectionClass::getConstants() depends on def. order).
|
||||||
(cmb)
|
(cmb)
|
||||||
. Fixed bug GH-8421 (Closures should accept attributes with
|
. Fixed bug GH-8421 (Closures should accept attributes with
|
||||||
Attribute::TARGET_FUNCTION). (ollieread)
|
Attribute::TARGET_FUNCTION). (ollieread)
|
||||||
|
|
||||||
- Zlib:
|
- Zlib:
|
||||||
|
@ -1856,11 +1856,10 @@ static int spl_filesystem_object_cast(zend_object *readobj, zval *writeobj, int
|
|||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /* {{{ */
|
static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add) /* {{{ */
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t line_len = 0;
|
size_t line_len = 0;
|
||||||
zend_long line_add = (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) ? 1 : 0;
|
|
||||||
|
|
||||||
spl_filesystem_file_free_line(intern);
|
spl_filesystem_file_free_line(intern);
|
||||||
|
|
||||||
@ -1905,7 +1904,13 @@ static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /
|
|||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value) /* {{{ */
|
static inline zend_result spl_filesystem_file_read(spl_filesystem_object *intern, bool silent)
|
||||||
|
{
|
||||||
|
zend_long line_add = (intern->u.file.current_line) ? 1 : 0;
|
||||||
|
return spl_filesystem_file_read_ex(intern, silent, line_add);
|
||||||
|
}
|
||||||
|
|
||||||
|
static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value) /* {{{ */
|
||||||
{
|
{
|
||||||
int ret = SUCCESS;
|
int ret = SUCCESS;
|
||||||
zval *value;
|
zval *value;
|
||||||
@ -2185,7 +2190,7 @@ PHP_METHOD(SplFileObject, fgets)
|
|||||||
|
|
||||||
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
|
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
|
||||||
|
|
||||||
if (spl_filesystem_file_read(intern, 0) == FAILURE) {
|
if (spl_filesystem_file_read_ex(intern, /* silent */ false, /* line_add */ 1) == FAILURE) {
|
||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
|
RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len);
|
||||||
@ -2225,8 +2230,8 @@ PHP_METHOD(SplFileObject, key)
|
|||||||
RETURN_THROWS();
|
RETURN_THROWS();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do not read the next line to support correct counting with fgetc()
|
/* Do not read the next line to support correct counting with fgetc()
|
||||||
if (!intern->current_line) {
|
if (!intern->u.file.current_line) {
|
||||||
spl_filesystem_file_read_line(ZEND_THIS, intern, 1);
|
spl_filesystem_file_read_line(ZEND_THIS, intern, 1);
|
||||||
} */
|
} */
|
||||||
RETURN_LONG(intern->u.file.current_line_num);
|
RETURN_LONG(intern->u.file.current_line_num);
|
||||||
|
39
ext/spl/tests/SplFileObject_key_fgets_and seek.phpt
Normal file
39
ext/spl/tests/SplFileObject_key_fgets_and seek.phpt
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
--TEST--
|
||||||
|
SplFileObject verify interactions between seeking, getting the key and fgets
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$file = new SplTempFileObject();
|
||||||
|
|
||||||
|
for ($i = 0; $i < 100; $i++) {
|
||||||
|
$file->fwrite("Foo $i\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
$file->seek(50);
|
||||||
|
|
||||||
|
var_dump(
|
||||||
|
['line' => $file->key(), 'contents' => trim($file->fgets())],
|
||||||
|
['line' => $file->key(), 'contents' => trim($file->fgets())],
|
||||||
|
['line' => $file->key(), 'contents' => trim($file->fgets())],
|
||||||
|
);
|
||||||
|
|
||||||
|
?>
|
||||||
|
--EXPECT--
|
||||||
|
array(2) {
|
||||||
|
["line"]=>
|
||||||
|
int(50)
|
||||||
|
["contents"]=>
|
||||||
|
string(6) "Foo 50"
|
||||||
|
}
|
||||||
|
array(2) {
|
||||||
|
["line"]=>
|
||||||
|
int(51)
|
||||||
|
["contents"]=>
|
||||||
|
string(6) "Foo 51"
|
||||||
|
}
|
||||||
|
array(2) {
|
||||||
|
["line"]=>
|
||||||
|
int(52)
|
||||||
|
["contents"]=>
|
||||||
|
string(6) "Foo 52"
|
||||||
|
}
|
24
ext/spl/tests/gh8273.phpt
Normal file
24
ext/spl/tests/gh8273.phpt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
--TEST--
|
||||||
|
GH-8273 (SplFileObject: key() returns wrong value)
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$file = new SplTempFileObject();
|
||||||
|
|
||||||
|
// write to file
|
||||||
|
for ($i = 0; $i < 5; $i++) {
|
||||||
|
$file->fwrite("line {$i}" . PHP_EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// read from file
|
||||||
|
$file->rewind();
|
||||||
|
while ($file->valid()) {
|
||||||
|
echo $file->key(), ': ', $file->fgets();
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
--EXPECT--
|
||||||
|
0: line 0
|
||||||
|
1: line 1
|
||||||
|
2: line 2
|
||||||
|
3: line 3
|
||||||
|
4: line 4
|
Loading…
Reference in New Issue
Block a user