Skip to content

Stdlib: file_get_contents() offset/length args return false — must read slice (ext/standard/file.c) #9308

Description

@PurHur

Category

stdlib · php-src-strict

Problem

file_get_contents($path, false, null, $offset, $length) must return a substring of the file bytes starting at $offset for at most $length bytes. Zend returns the slice on readable local files; this compiler returns false for the 5-argument form even when the path is valid and readable.

Single-argument and no-offset forms work; failure is specific to $offset + $length operands.

php-src reference

Repro (failure today — verified 2026-06-19)

test/repro/maintainer_file_get_contents_offset.php (or inline):

<?php
declare(strict_types=1);
$slice = file_get_contents(__FILE__, false, null, 0, 10);
var_export($slice);
echo "\n";
$mid = file_get_contents(__FILE__, false, null, 5, 5);
var_export($mid);
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/maintainer_file_get_contents_offset.php
php bin/vm.php test/repro/maintainer_file_get_contents_offset.php
'
Probe Zend PHP 8.2 VM today
file_get_contents(__FILE__, false, null, 0, 10) first 10 bytes (e.g. '<?php\nvar_') false ❌
file_get_contents(__FILE__, false, null, 5, 5) 5-byte mid slice false ❌
file_get_contents(__FILE__) full file OK

Scope (PHP-in-PHP first)

Path Work
ext/standard/file_get_contents.php Honor $offset + $length in PHP read helper
ext/standard/VmFs.php / stream read Seek + bounded read; reject only on real I/O failure
lib/JIT/Builtin/FileGetContents.php Mirror slice semantics or defer with matrix note
Tests test/compliance/cases/stdlib/file_get_contents_offset_length.phpt

Forbidden: new slice logic in runtime/*.c — port php-src behavior into ext/standard.

Done when (php-src-strict)

  • Both repro slices match Zend byte-for-byte on VM
  • $offset beyond file length → false / empty string per Zend (document which)
  • file_get_contents() with stream context + offset still works
  • JIT/AOT match or capability matrix updated
  • ./script/ci-fast.sh --filter file_get_contents_offset green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter file_get_contents_offset 2>/dev/null || true'

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions