Skip to content

Stdlib: hash()/hash_hmac()/md5()/sha1()/crc32() — enum case operands must TypeError (ext/hash, ext/standard) #5780

Description

@PurHur

Problem

Digest builtins must reject enum case operands with TypeError (PHP 8 typed signatures). This build coerces backed enum cases to strings/ints and computes a digest, so hash('sha256', E::A) silently succeeds.

Related but separate:

php-src reference

Repro

<?php
enum E: string { case A = 'x'; }

foreach ([
    ['hash', fn () => hash('sha256', E::A)],
    ['hash_hmac', fn () => hash_hmac('sha256', E::A, 'key')],
    ['md5', fn () => md5(E::A)],
    ['sha1', fn () => sha1(E::A)],
    ['crc32', fn () => crc32(E::A)],
] as [$name, $call]) {
    try {
        $call();
        echo "$name: no throw\n";
    } catch (Throwable $e) {
        echo "$name: ", get_class($e), ': ', $e->getMessage(), "\n";
    }
}
php repro.php
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
Builtin Zend VM (today)
hash('sha256', E::A) TypeError (arg #2 $data) digest hex (coerced)
hash_hmac(..., E::A, 'key') TypeError (arg #2 $data) digest hex
md5(E::A) TypeError (arg #1 $string) md5 hex
sha1(E::A) TypeError sha1 hex
crc32(E::A) TypeError integer

Scope (PHP-in-PHP)

Path Notes
ext/standard/VmHash.php, hash_.php, hash_hmac.php VM strict checks before VmString::coerceOperand
ext/standard/VmString.php or dedicated guard md5/sha1/crc32 if routed through coercion
lib/JIT/ mirror checks on JIT digest paths

Prefer EnumCaseSupport::isEnumCaseVariable() + TypeError templates matching Zend message text.

Done when

  • All repro rows throw TypeError on VM (and JIT/AOT where builtins are lowered).
  • Compliance .phpt under test/compliance/cases/stdlib/.
  • No new C in runtime/ for this check.

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