Skip to content

Stdlib: unpack() $offset — enum case operand must TypeError not coerce backing int (ext/standard/pack.c) #8866

Description

@PurHur

Category

stdlibphp-src-strict

Problem

unpack($format, $data, $offset) argument #3 must be int. Passing a backed enum case must TypeError (… must be of type int, E given). VM today coerces the backing scalar (e.g. E::A with backing 0 → offset 0) and proceeds.

php-src reference

Repro

<?php
enum E: int { case A = 0; }
$data = "\x00\x00\x00\x01";
try {
    var_export(unpack('i', $data, E::A));
} catch (Throwable $e) {
    echo get_class($e) . ': ' . $e->getMessage() . "\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php -r "enum E: int { case A = 0; } try { var_export(unpack(\"i\", \"\\x00\\x00\\x00\\x01\", E::A)); } catch (Throwable \$e) { echo get_class(\$e).\": \".\$e->getMessage(); }"'
# Today: array(1 => …) — WRONG (backing 0 used as offset)
# Target: TypeError: unpack(): Argument #3 ($offset) must be of type int, E given

Scope (this repo)

Piece Path
VM builtin ext/standard/unpack.php$offset = VmMath::parseIntBuiltinArg(...)
Int guards ext/standard/VmMath.phprejectEnumCaseIntBuiltinArg() / enumCaseEntryForVariable()
Engine ext/standard/UnpackEngine.php (unchanged once offset typed)
JIT ext/standard/JitUnpack.php (mirror guard)

Root cause likely: enum case not detected before parseLongBuiltinArgCore() backing coercion.

Done when

  • Repro throws TypeError on VM
  • unpack('i', $data, 0) unchanged
  • test/compliance/cases/stdlib/unpack_offset_enum_typeerror.phpt green
  • JIT agrees when third arg is present

Verify

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

Links

#1492 · #6078 · #6213 · #6267 · #5780

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