Skip to content

Stdlib: array_rand() — numeric-string $num must coerce to int (ext/standard/array.c parity) #4320

Description

@PurHur

Category

stdlib · runtime

Summary

array_rand() argument #2 ($num) must accept numeric strings (e.g. "1") with Zend int coercion. VM/JIT require native TYPE_INTEGER and throw LogicException.

Problem

Second parameter $num is not coerced from numeric strings before validation, so valid Zend calls fail at runtime.

php-src reference

Repro

HARNESS_DOCKER_RUN_OPTS='--memory=8g --cpus=2' ./script/docker-exec.sh -- bash -lc '
  source script/php-env.sh
  php -r "var_dump(array_rand([\"a\",\"b\"], \"1\"));"
  php bin/vm.php -r "var_dump(array_rand([\"a\",\"b\"], \"1\"));"
'
Mode array_rand(['a','b'], '1')
Zend int(0) or int(1) (valid key)
VM LogicException — argument #2 must be an integer

Scope (this repo)

Area Files
VM ext/standard/array_rand.php, ext/standard/VmArray.php (arrayRandPacked())
JIT ext/standard/JitArrayRand.php
Tests test/compliance/cases/stdlib/array_rand_num_numeric_string.phpt

Done when

  • Repro passes on VM and JIT/AOT with int key in range
  • Non-numeric strings still throw Zend TypeError / ValueError per php-src
  • Patterns aligned with array_chunk / array_slice numeric-string issues

Implementation notes

  • Use shared int coercion helper (see closed numeric-string issues in ext/standard/).
  • Reuse test/repro-maintainer/array_rand_validation.php where applicable.

Links

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