Skip to content

Stdlib: random_bytes() — enum case length operand must TypeError not silent coerce (php-src-strict) #6160

Description

@PurHur

Category

stdlib (php-src-strict)

Problem

random_bytes() accepts backed enum case operands and coerces via the backing integer. Zend/php-src rejects enum cases with TypeError (ext/standard/random.c / Z_PARAM_LONG).

This is distinct from #4626 (numeric-string + LogicException vs TypeError for arrays); enum cases are a separate guard path via EnumCaseSupport / VmString::coerceStringBuiltinArg() patterns (#5780).

php-src reference

Repro (failure today)

<?php
enum E: int { case A = 1; }
try {
    random_bytes(E::A);
    echo "accepted\n";
} catch (TypeError $e) {
    echo "TypeError\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro_random_bytes_enum.php
php bin/vm.php repro_random_bytes_enum.php
'
Runtime Output
Zend PHP 8.x TypeError
bin/vm.php accepted (silent coerce to length 1)

Scope (this repo)

Module Path
VM ext/standard/random_bytes.php — reject TYPE_ENUM_CASE before int coercion
Shared lib/VM/EnumCaseSupport.php, lib/VM/VmString.php guards
JIT/AOT ext/standard/JitRandomBytes.php — mirror VM TypeError
Tests test/compliance/cases/stdlib/random_bytes_enum_typeerror.phpt

PHP-in-PHP: no new C in runtime/; reuse existing JitStringBuiltinArg / enum-case probes.

Done when

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter random_bytes_enum'

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 apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions