Take the State of PHP 2026 Survey →

Random Functions throwing Exceptions View as Markdown

Implemented (PHP 7.0)
Authors
Anthony Ferrara ircmaxell@php.net, Aaron Piotrowski aaron@icicle.io
Date
2015-08-22
Version
1.0
Source
wiki.php.net/rfc/random-function-exceptions

Vote

Should this RFC be accepted for 7.0 closed
Yes
28
No
2
30 voters
aharvey Yes 2015-08-31 18:04
bwoebi Yes 2015-08-30 10:25
derick Yes 2015-08-30 16:46
galvao Yes 2015-08-30 15:40
gasolwu Yes 2015-08-31 07:24
hywan Yes 2015-08-30 10:58
ircmaxell Yes 2015-08-30 06:52
kalle No 2015-09-04 17:56
krakjoe Yes 2015-08-30 06:58
lstrojny Yes 2015-08-31 20:05
marcio Yes 2015-09-02 16:47
mariano Yes 2015-08-30 17:59
mbeccati Yes 2015-08-31 08:08
mrook Yes 2015-08-30 08:04
nikic Yes 2015-08-30 10:56
ocramius Yes 2015-08-30 17:59
oliviergarcia Yes 2015-09-03 20:28
pajoye Yes 2015-09-01 04:54
pauloelr Yes 2015-08-30 15:42
peehaa Yes 2015-08-30 14:21
pollita Yes 2015-08-30 17:39
rdohms Yes 2015-08-30 17:58
sammyk Yes 2015-08-30 07:36
stelianm Yes 2015-08-31 08:45
svpernova09 Yes 2015-08-31 13:41
till Yes 2015-08-30 09:03
tpunt Yes 2015-08-30 17:52
trowski Yes 2015-08-30 19:12
tyrael No 2015-09-02 19:50
zimt Yes 2015-09-01 08:22

Discussions on the mailing list

Core functions throwing exceptions in PHP7
by Sammy Kaye Powers · 57 messages
Recap - Core functions throwing exceptions in PHP7
by Scott Arciszewski · 27 messages
[RFC] [Discuss] Random Functions Throwing Exceptions in PHP 7.0.0
by Anthony Ferrara · 2 messages
[RFC] [Discuss] Random Functions Throwing Exceptions in PHP 7.0.0
by Thomas Bley · 5 messages
[RFC] [Discuss] Random Functions Throwing Exceptions in PHP 7.0.0
by Thomas Bley · 2 messages
[RFC] [Vote] Random Functions Throwing Exceptions in PHP 7.0.0
by Anthony Ferrara · 3 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

Concern has been raised over the way failure is indicated by several functions in the core that are often used for security purposes. These functions may fail open, allowing execution to continue, potentially compromising the security of an application. Failure of these functions is only indicating failure through a return value that may be inappropriate cast and used or requiring further function calls to check for failure. It is far too easy for an uninformed programmer to be unaware of the potential for failure and the consequences of neglecting to check for failure, so it is imperative that these functions fail closed, so execution cannot continue if the error is ignored.

The following functions are addressed by this RFC:

  • random_int()
  • random_bytes()

An example of a common use of these functions is:

example.php

<?php
$password = "";
for ($i = 0; $i < 10; $i++) {
    $password .= chr(random_int(33, 124));
}
?>

If random_int isn't able to capture enough entropy, it will error. In that case, $password would be 10 null bytes. Without careful error checking this could cause severe security issues. This is not acceptable and this RFC proposes to solve this by making all errors in these functions into exceptions.

Proposal

To fail closed while allowing the error to be handled, it is this RFCs proposal that the functions above throw exceptions upon failure. The specific types of exceptions and conditions from which they are thrown are outlined below.

random_int()

  • Throws TypeError if zend_parse_parameters fails.
  • Throws Exception if generating sufficiently random data fails.
  • Throws Error if $min > $max.

random_bytes()

  • Throws TypeError if zend_parse_parameters fails.
  • Throws Exception if generating sufficiently random data fails.
  • Throws Error if $length <= 0.

Backward Incompatible Changes

random_int() and random_bytes() are new functions to PHP 7, so changes to these functions do not have any backwards compatibility issues.

Proposed PHP Version

PHP 7.0.0

Future Scope

This RFC explicitly does not address how exceptions should be thrown from other core functions. This question will be addressed in a separate RFC. Focus was given to the particular functions in this RFC due to their importance to security applications.

Proposed Voting Choices

  • Throw exceptions

Vote

This vote will close on 07:00 UTC on Sunday 06-09-2015

Vote: Should this RFC be accepted for 7.0 — Yes: 28 · No: 2 (closed)

Patches and Tests

Below are implementations of random_int() and random_bytes() which throw exceptions, though not exactly what is outlined above. The pulls will be updated to throw the exceptions described above before merging.

References