rfc:zpp_fail_on_overflow

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:zpp_fail_on_overflow [2014/09/23 00:38] – Infinity clarification ajfrfc:zpp_fail_on_overflow [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== PHP RFC: ZPP Failure on Overflow ====== ====== PHP RFC: ZPP Failure on Overflow ======
-  * Version: 0.1 +  * Version: 0.1.1 
-  * Date: 2014-09-22+  * Date: 2014-09-22, Last Updated 2014-12-02
   * Author: Andrea Faulds, ajf@ajf.me   * Author: Andrea Faulds, ajf@ajf.me
-  * Status: Under Discussion+  * Status: Implemented (PHP 7.0)
   * First Published at: http://wiki.php.net/rfc/zpp_fail_on_overflow   * First Published at: http://wiki.php.net/rfc/zpp_fail_on_overflow
  
 ===== Introduction ===== ===== Introduction =====
  
-PHP is a weakly-typed language, and so implicitly converts between integers and floats when passed to internal functions. Currently, when a float that is beyond the range of an integer (outside [PHP_INT_MIN, PHP_INT_MAX]) is passed to an internal function expecting an integer argument, it will silently truncate (e.g. ''3221225470.5'' becomes ''-1073741826'' on 32-bit platforms), causing a loss of magnitude and sign information, though technically preserving the "lower bits". This happens without warning, is unintuitive, and can lead to subtle bugs.+PHP is a weakly-typed language, and so implicitly converts between integers and floats when such values are passed to internal functions. Currently, when a float that is beyond the range of an integer (outside [PHP_INT_MIN, PHP_INT_MAX]) is passed to an internal function expecting an integer argument, it will silently truncate (e.g. ''3221225470.5'' becomes ''-1073741826'' on 32-bit platforms), causing a loss of magnitude and sign information, though technically preserving the "lower bits". This mangling of input happens without warning, is unintuitive, and can lead to subtle bugs.
  
 ===== Proposal ===== ===== Proposal =====
  
-''zend_parse_parameters'' and its fast macro counterparts are modified to fail (usually causing the function to bail out with an ''E_WARNING'' and return ''NULL'') if a float that is outside of the range of an integer, or is NaN, is passed for the ''l'' (''Z_PARAM_LONG'') parameter type. Functions which use the ''L'' type or the ''strict'' mode in the macros, which caps at PHP_INT_MIN or PHP_INT_MAX depending on sign (also known as saturation), will continue to simply cap and not fail where the float is out of bounds, but will now fail if NaN is passed. Note that Infinity is considered to be outside the range of an integer, as it compares much like a normal floating-point value.+''zend_parse_parameters'' and its fast macro counterparts are modified to fail (which usually causes the PHP function that invoked it to bail out with an ''E_WARNING'' and return ''NULL'') if a float that is outside of the range of an integer, or is NaN, is passed for the ''l'' (''Z_PARAM_LONG'') parameter type. Functions which use the ''L'' type (aka ''strict'' mode in the macros) will retain their previous behaviour of saturating (capping at ''PHP_INT_MAX''/''MIN''without erroring when a float is out of bounds, but will now fail if NaN is passed.
  
-This proposal would complement the draft [[rfc:bigint|Big Integer Support RFC]], as it would be desirable to have functions which only accept platform-native integers (32-bit or 64-bit) error instead of silently truncate when a bigint is passed.+The special floating-point values ''INF'' and ''-INF'' fail the check like any other number that is too large. 
 + 
 +This proposal would complement the [[rfc:bigint|Big Integer Support RFC]], as it would be desirable to have functions which only accept platform-native integers (32-bit or 64-bit) error instead of silently truncating when a bigint that is too large is passed. This is particularly important given that the bigint RFC strives for there to be no user-visible difference between the internal `IS_LONG` and `IS_BIGINT` types. Integers being silently mangled when passed to other functions can only cause bugs and programmer frustration. Thus it is quite important that something is done about this before bigints can be implement.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-This is an inherently backwards-incompatible change. However, it is behaviour that is dangerous, and this will only affect edge cases. In the unusual case where the truncation/NaN-tolerant behaviour is desired, an explicit ''(int)'' cast can be used.+This is an inherently backwards-incompatible change. However, the previous behaviour was dangerous, and this will only affect uncommon edge cases. In the unusual event that the truncation/NaN-tolerant behaviour is desired by the programmer, an explicit ''(int)'' cast can be used.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 28: Line 30:
 This does not affect other implicit integer casts, such as those done with array keys or by the bitwise operators. This does not affect other implicit integer casts, such as those done with array keys or by the bitwise operators.
    
-===== Proposed Voting Choices =====+===== Vote =====
  
-This is arguably not a language change. However, as it breaks backwards-compatibility, and because some may argue it //is// a language change, a 2/3 majority will be required. The vote will be a straight Yes/No vote.+This is arguably not a language change. However, as it breaks backwards-compatibility, and because some may argue it //is// a language change, a 2/3 majority will be required. The vote is a straight Yes/No vote.
  
-===== Patches and Tests =====+Voting opened on 2014-12-02 and ended on 2014-12-12.
  
-There is a work-in-progress patch/pull request here: https://github.com/php/php-src/pull/835+<doodle title="Accept the ZPP Failure on Overflow RFC and merge into master?" auth="ajf" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
-As far as I am concerned, the implementation itself is doneHowever there are numerous tests broken by the change which need updating, and hence the patch must be considered work-in-progress.+===== Patches and Tests ===== 
 + 
 +There is a working pull request containing a patch here: https://github.com/php/php-src/pull/835
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  + 
-  - the version(s) it was merged to +This was merged into master here: https://github.com/php/php-src/commit/0ea0b591d79ae0ee18d33533a5c701330836ff6b 
-  a link to the git commit(s) + 
-  - a link to the PHP manual entry for the feature+It will form part of PHP 7.
  
 ===== References ===== ===== References =====
Line 54: Line 61:
 ===== Changelog ===== ===== Changelog =====
  
 +  * v0.1.1 - Some cleanup and clarity
   * v0.1 - Initial version   * v0.1 - Initial version
rfc/zpp_fail_on_overflow.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1