Bug #80701 [NEW]: NaN equals NaN in arrays, if two arrays have the same identity
| From: | riikka dot kalliomaki at gmail dot com | Date: | Wed, 03 Feb 2021 09:47:15 +0000 |
| Subject: | Bug #80701 [NEW]: NaN equals NaN in arrays, if two arrays have the same identity | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-231898@lists.php.net to get a copy of this message | ||
From: riikka dot kalliomaki at gmail dot com
Operating system:
PHP version: 8.0.1
Package: Arrays related
Bug Type: Bug
Bug description:NaN equals NaN in arrays, if two arrays have the same identity
Description:
------------
TL;DR; "$a = $b = [NAN]; [NAN] !== [NAN] && $a === $b".
PHP arrays don't officially have an identity. However, for optimization
PHP uses copy on write for arrays so two arrays can have the same
"identity" when, for example, an array is assigned to another variable.
For optimization, when using the strict comparison operator "===", PHP
checks if the identity of two arrays is same to quickly return true in
those cases. I mean, one would expect that two arrays that point to the
same thing would be strictly equal.
However, this assumption is not true for NaN, i.e. [NAN] !== [NAN]. But,
if you first assign the [NAN] to a variable, it can equal itself, if two
arrays have the same identity.
Based on https://3v4l.org/gHjG0, this seems have been
introduced in
5.3.15 / 5.4.5
I would consider this behavior to be a bug, but I'm not sure whether it
is worth fixing since that could have non negligible performance impact.
I did not run into this bug in actual code and simply realized it
through personal musing. Thus, at least I don't personally consider
fixing it important.
Test script:
---------------
<?php
$NaN = sqrt(-1);
var_dump($NaN === $NaN);
var_dump(array($NaN) === array($NaN));
$a = array($NaN);
$b = $a;
var_dump($a === $b);
reset($b);
var_dump($a === $b);
Expected result:
----------------
bool(false)
bool(false)
bool(false)
bool(false)
Actual result:
--------------
bool(false)
bool(false)
bool(true)
bool(false)
--
Edit bug report at https://bugs.php.net/bug.php?id=80701&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80701&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80701&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80701&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80701&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80701&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80701&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80701&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80701&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80701&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80701&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80701&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80701&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80701&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80701&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80701&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80701&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80701&r=mysqlcfg