Skip to content

Stdlib: fputcsv() — enclosure/escape edge cases + ValueError parity (ext/standard/file.c) #4530

Description

@PurHur

Category

stdlib

Problem

fputcsv() is implemented for basic cases but does not match Zend for empty enclosure (ValueError) and empty escape character (PHP 8.4+ rules), multi-byte separators, or TypeError vs LogicException on bad handle types.

VM path: ext/standard/fputcsv.php + VmFs; defaults separator=',', enclosure='"', escape='\\' with no validation.

php-src reference

  • php/php-src ext/standard/file.c — PHP_FUNCTION(fputcsv) (_php_fputcsv)
  • PHP 8.4: empty $escape allowed only when $enclosure is non-empty one-byte string; otherwise ValueError

Repro

Save as test/repro-maintainer/fputcsv_escape.php:

<?php
$fp = fopen('php://memory', 'r+');
foreach ([
    ['escape' => ''],
    ['enclosure' => ''],
] as $case) {
    try {
        fputcsv($fp, ['a'], ',', $case['enclosure'] ?? '"', $case['escape'] ?? '\\');
        echo "no throw\n";
    } catch (Throwable $e) {
        echo get_class($e), ': ', $e->getMessage(), "\n";
    }
}
php test/repro-maintainer/fputcsv_escape.php
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php test/repro-maintainer/fputcsv_escape.php'

Expected on PHP 8.4+ (Zend, document exact message from php -v in Docker)

Case Zend
escape='' (PHP 8.4+) ValueError — escape must be empty only with valid enclosure per RFC
enclosure='' ValueError — enclosure must be exactly one character

Record actual message strings in the compliance PHPT header when implementing.

Actual (this compiler)

Often no throw or LogicException on bad handle type instead of TypeError.

Scope

  • ext/standard/fputcsv.php, ext/standard/VmFs.php
  • lib/JIT/Builtin/ fputcsv lowering if present
  • test/compliance/cases/stdlib/fputcsv_escape.phpt, fputcsv_bad_handle.phpt

Done when

  • Repro exception class + message match Zend for empty escape/enclosure on VM
  • Normal 5-arg calls still write RFC4180-style CSV like Zend
  • ./script/ci-fast.sh --filter fputcsv green

Links

Activity

  1. added
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 2, 2026
  2. PurHur commented on Jun 9, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Compiler STD Lib — starting this run

  3. PurHur commented on Jun 9, 2026

    @PurHur
    OwnerAuthor

    Merged via #7755.

    Verification

    ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter fputcsv'
    # OK (11 tests, 13 assertions)

    VM + JIT compliance PHPTs green; AOT covered via compile.php -l on test/fixtures/aot/compile-only/fputcsv_escape.php (full AOT link blocked by pre-existing LLVM verify failures in harness).

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 machineimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions