-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix GH-8561, GH-8562, GH-8563, GH-8564: SplFileObject iterator desync #21679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c4c3bed
6035b0a
0353557
e3c8189
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,5 +18,5 @@ var_dump($s->key()); | |
| var_dump($s->valid()); | ||
| ?> | ||
| --EXPECT-- | ||
| int(14) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The .phpt temp file is exactly 12 physical lines (verified via |
||
| int(12) | ||
| bool(false) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,5 +18,5 @@ var_dump($s->key()); | |
| var_dump($s->valid()); | ||
| ?> | ||
| --EXPECT-- | ||
| int(13) | ||
| int(12) | ||
| bool(false) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,5 @@ $file->rewind(); | |
| var_dump($file->fgetcsv()); | ||
| ?> | ||
| --EXPECT-- | ||
| array(1) { | ||
| [0]=> | ||
| NULL | ||
| } | ||
| bool(false) | ||
| bool(false) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --TEST-- | ||
| GH-8561 (SplFileObject: key() and current() unsynchronized after fgets()) | ||
| --FILE-- | ||
| <?php | ||
| $file = new SplTempFileObject(); | ||
| for ($i = 0; $i < 5; $i++) { | ||
| $file->fwrite("line {$i}" . PHP_EOL); | ||
| } | ||
|
|
||
| // Case 1: rewind + fgets, then key/current | ||
| $file->rewind(); | ||
| $file->fgets(); | ||
| echo "After rewind+fgets: key=" . $file->key() . " current=" . trim($file->current()) . "\n"; | ||
|
|
||
| // Case 2: multiple fgets | ||
| $file->rewind(); | ||
| $file->fgets(); | ||
| $file->fgets(); | ||
| echo "After rewind+fgets+fgets: key=" . $file->key() . " current=" . trim($file->current()) . "\n"; | ||
|
|
||
| // Case 3: current then fgets | ||
| $file->rewind(); | ||
| $file->current(); | ||
| $file->fgets(); | ||
| echo "After current+fgets: key=" . $file->key() . " current=" . trim($file->current()) . "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| After rewind+fgets: key=1 current=line 1 | ||
| After rewind+fgets+fgets: key=2 current=line 2 | ||
| After current+fgets: key=1 current=line 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| GH-8563 (Different results for seek() on SplFileObject and SplTempFileObject) | ||
| --FILE-- | ||
| <?php | ||
| $path = __DIR__ . '/gh8563.txt'; | ||
| $file_01 = new SplFileObject($path, 'w+'); | ||
| $file_02 = new SplTempFileObject(); | ||
|
|
||
| for ($i = 0; $i < 5; $i++) { | ||
| $file_01->fwrite("line {$i}" . PHP_EOL); | ||
| $file_02->fwrite("line {$i}" . PHP_EOL); | ||
| } | ||
|
|
||
| $file_01->rewind(); | ||
| $file_02->rewind(); | ||
|
|
||
| $file_01->seek(10); | ||
| $file_02->seek(10); | ||
|
|
||
| echo 'SplFileObject: ' . $file_01->key() . "\n"; | ||
| echo 'SplTempFileObject: ' . $file_02->key() . "\n"; | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| unlink(__DIR__ . '/gh8563.txt'); | ||
| ?> | ||
| --EXPECT-- | ||
| SplFileObject: 5 | ||
| SplTempFileObject: 5 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --TEST-- | ||
| GH-8564 (SplFileObject: next() moves to nonexistent indexes) | ||
| --FILE-- | ||
| <?php | ||
| $file = new SplTempFileObject(); | ||
| for ($i = 0; $i < 5; $i++) { | ||
| $file->fwrite("line {$i}" . PHP_EOL); | ||
| } | ||
|
|
||
| $file->rewind(); | ||
| for ($i = 0; $i < 10; $file->next(), $i++); | ||
| echo "next() 10x: key=" . $file->key() . " valid=" . var_export($file->valid(), true) . "\n"; | ||
|
|
||
| $file->rewind(); | ||
| $file->seek(10); | ||
| echo "seek(10): key=" . $file->key() . " valid=" . var_export($file->valid(), true) . "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| next() 10x: key=5 valid=false | ||
| seek(10): key=5 valid=false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,9 +44,9 @@ try { | |
| string(14) ""A", "B", "C" | ||
| " | ||
| string(13) ""D", "E", "F"" | ||
| Cannot read from file php://temp | ||
| string(0) "" | ||
| --- Use csv control --- | ||
| string(14) ""A", "B", "C" | ||
| " | ||
| string(13) ""D", "E", "F"" | ||
| Cannot read from file php://temp | ||
| string(0) "" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think part of the motivation for CSV functions is to ignore the final line if it just an EOL before EOF.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning the last read line would mean holding a separate "last line" field on the object across |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --TEST-- | ||
| GH-8562 (SplFileObject::current() returns wrong result after call to next()) | ||
| --FILE-- | ||
| <?php | ||
| $file = new SplTempFileObject(); | ||
| for ($i = 0; $i < 5; $i++) { | ||
| $file->fwrite("line {$i}" . PHP_EOL); | ||
| } | ||
|
|
||
| $file->rewind(); | ||
| $file->next(); | ||
| echo "After rewind+next: key=" . $file->key() . " current=" . trim($file->current()) . "\n"; | ||
|
|
||
| $file->rewind(); | ||
| $file->next(); | ||
| $file->next(); | ||
| echo "After rewind+next+next: key=" . $file->key() . " current=" . trim($file->current()) . "\n"; | ||
|
|
||
| $file->rewind(); | ||
| $file->current(); | ||
| $file->next(); | ||
| echo "After current+next: key=" . $file->key() . " current=" . trim($file->current()) . "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| After rewind+next: key=1 current=line 1 | ||
| After rewind+next+next: key=2 current=line 2 | ||
| After current+next: key=1 current=line 1 |
Uh oh!
There was an error while loading. Please reload this page.