php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71656 Large static array initialization in class is truncated to 0x8000 entries
Submitted: 2016-02-23 21:58 UTC Modified: 2016-02-23 22:32 UTC
From: laurentconstantin at free dot fr Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: 5.6.18 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: laurentconstantin at free dot fr
New email:
PHP Version: OS:

 

 [2016-02-23 21:58 UTC] laurentconstantin at free dot fr
Description:
------------
The test script creates a class containing a large static array initialization like :
  class MyClass {
    public static $arr = array('a0'=>1,'a1'=>1,'a2'=>1,etc.
  }

Then, running the created test script shows that the created array
is smaller than the expected array.

If the number of entries in this array is larger than 32768 (0x8000),
then the lower 32768 entries are lost. So, for example the a0 to a32768
entries are not in the array, and the a32769... entries are in the
array.

PHP was compiled with :
  ./configure --prefix=/usr/local/phpTest
  make
  make install
 


Test script:
---------------
<?php
$numberOfEntries = $argv[1]; // Use for example '35000'.
$code =  "<?php\n";
$code .= "class MyClass {\n";
$code .= "  public static \$arr = array(\n";
for ($i = 0 ; $i < $numberOfEntries ; $i++) {
  if ($i != 0) {
    $code .= ",";
  }
  $code .= "'a$i'=>1";
}
$code .= "  );\n";
$code .= "}\n";
$code .= "print \"Number of entries in the array: \".count(MyClass::\$arr).\"\\n\";\n";
$code .= "?>\n";
file_put_contents('./myclass.php', $code);
/*
  Then run in a shell:
    php bug.php 35000  # This creates the myclass.php file.
    php myclass.php    # This shows the number of entries in the array.
    > Number of entries in the array: 2232 <- it should say 35000 instead.
  Note: 35000-2232 = 32768 = 0x8000.
  So, this is the proof that the array is truncated, and lost its first
  0x8000 entries.
*/
?>

Expected result:
----------------
After running:
  php bug.php 35000

The second script is run:
  php myclass.php

This second script should display:
  Number of entries in the array: 35000
However, it displays:
  Number of entries in the array: 2232

Note: 35000-2232 = 32768 = 0x8000.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-23 22:32 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2016-02-23 22:32 UTC] nikic@php.net
Duplicate of https://bugs.php.net/bug.php?id=68057.

To bug isn't going to be fixed due to ABI compatibility restrictions. You'll have to either avoid directly initializing static properties with large arrays or upgrade to PHP 7.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Dec 08 13:03:41 2021 UTC