phalcon/cphalcon · error · InvalidRandomInput

Input number must be a positive integer

Error message

Input number must be a positive integer

What it means

Thrown by Random::number when its $len argument is <= 0. The method promises a result in the range 0 <= result <= $len, which is impossible for a non-positive upper bound, so the guard rejects the input before calling random_int. It indicates a caller-side logic error (e.g. computing the bound from an empty count or negative size), not a failure of the secure random generator itself.

Solutions

  1. Pass len >= 1 (e.g. 16 for a 4-bit-random range)
  2. Validate user-supplied lengths before calling number()
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Encryption/Security/Random.zep:274 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/098bdbd5c21fc390. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Encryption/Security/Random.zep:274

    }

    /**
     * Generates a random number between 0 and $len
     *
     * Returns an integer: 0 <= result <= $len.
     *
     *```php
     * $random = new \Phalcon\Encryption\Security\Random();
     *
     * echo $random->number(16); // 8
     *```
     * @throws Exception If secure random number generator is not available,
     *                   unexpected partial read or $len <= 0
     */
    public function number(int len) -> int
    {
        if unlikely len <= 0 {
            throw new InvalidRandomInput();
        }

        return random_int(0, len);
    }

    /**
     * Generates a v4 random UUID (Universally Unique IDentifier)
     *
     * The version 4 UUID is purely random (except the version). It does not
     * contain meaningful information such as MAC address, time, etc. See RFC
     * 4122 for details of UUID.
     *
     * Delegates to `Phalcon\Encryption\Security\Uuid::v4()`. For other UUID
     * versions or object-based access use that class directly.
     *
     *```php
     * $random = new \Phalcon\Encryption\Security\Random();
     *

View on GitHub (pinned to b7419de9cd)