paragonie/random_compat · error · Error

Length must be greater than 0

Error message

Length must be greater than 0

What it means

After confirming $bytes is an integer, random_compat's libsodium-legacy random_bytes() rejects values less than 1 with this Error. Zero or negative lengths are considered invalid input because a random-byte buffer must contain at least one byte, matching PHP 7's built-in behavior.

Solutions

  1. Guard the argument: if ($length < 1) throw/handle before calling random_bytes().
  2. Use max(1, $length) if a minimum of one byte is always acceptable.
  3. Fix the length computation that produces 0 or negative values.
  4. Validate configuration-driven lengths at load time so zero never reaches the call site.

Example fix

// before
$bytes = random_bytes($config['key_length']);
// after
$length = (int) $config['key_length'];
if ($length < 1) {
    throw new InvalidArgumentException('key_length must be >= 1');
}
$bytes = random_bytes($length);
Defensive patterns

Strategy: validation

Validate before calling

if (!is_int($length) || $length < 1) {
    throw new InvalidArgumentException('length must be an integer >= 1');
}

Type guard

null

Try / catch

try {
    $bytes = random_bytes($length);
} catch (Error $e) {
    if (strpos($e->getMessage(), 'Length must be greater than 0') !== false) {
        throw new InvalidArgumentException('length must be >= 1', 0, $e);
    }
    throw $e;
}

Prevention

When it happens

Trigger: Calling random_bytes(0) or random_bytes(-16) (e.g. a computed length from subtraction or an empty config value) triggers the '$bytes < 1' check at lib/random_bytes_libsodium_legacy.php:55.

Common situations: Length computed as strlen($a)-strlen($b) yielding 0, a config value defaulting to 0, or a caller that only guards against negative numbers but not zero.

Related errors


AI-assisted analysis of paragonie/random_compat@b5d188cc9d (2026-09-13). Data as JSON: /api/errors/315eca53e2b1712f. Report an issue: GitHub.

Appendix: source

Thrown at lib/random_bytes_libsodium_legacy.php:55

     * @param int $bytes
     *
     * @throws Exception
     *
     * @return string
     */
    function random_bytes($bytes)
    {
        try {
            /** @var int $bytes */
            $bytes = RandomCompat_intval($bytes);
        } catch (TypeError $ex) {
            throw new TypeError(
                'random_bytes(): $bytes must be an integer'
            );
        }

        if ($bytes < 1) {
            throw new Error(
                'Length must be greater than 0'
            );
        }

        /**
         * @var string
         */
        $buf = '';

        /**
         * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
         * generated in one invocation.
         */
        if ($bytes > 2147483647) {
            for ($i = 0; $i < $bytes; $i += 1073741824) {
                $n = ($bytes - $i) > 1073741824
                    ? 1073741824
                    : $bytes - $i;

View on GitHub (pinned to b5d188cc9d)