Skip to content

iterator_count() on an SplFileObject runs endlessly #19942

@pilif

Description

@pilif

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions