php / php/php-src

iterator_count() on an SplFileObject runs endlessly

Open
#19,942 3 comments 0 reactions 0 assignees View on GitHub

@alexandre-daubois is already working on this.

Since Sep 25, 2025.

  • #19957 by @alexandre-daubois — open
Bug Extension: spl Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:


<?php

touch("/tmp/empty");
var_dump(iterator_count(new SplFileObject("/tmp/empty", "r")));

Resulted in this output:

(None. Script runs forever)

But I expected this output instead:

int(0)

It fails on files of any size, but this is the simplest I can reproduce the problem with.

Can reproduce this all the way to PHP 5.6 which is the earliest version I tried

PHP Version
PHP 8.4.12 (cli) (built: Sep  8 2025 21:17:50) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.12, Copyright (c), by Zend Technologies
Operating System

Tested under Linux and macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reported PHP snippet with iterator_count() and SplFileObject on an empty file, then inspect the implementation and existing tests for these APIs. Done means the call terminates and returns int(0) for the empty file while preserving correct counts for non-empty files.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.