PHP_INT_MAX as Array Key causes an Exception

Discussion about the open-source Phalanger [?] project.

PHP_INT_MAX as Array Key causes an Exception

Postby dulfe » November 1st, 2013, 5:11 pm

Hello,

I found a glitch when using Array with a maximum int key. If I try to use PHP_INT_MAX as key value, it produces an "Arithmetic operation resulted in an overflow" exception.

Simple test case:

Code: Select all
var_dump(PHP_INT_MAX);
$xxx[PHP_INT_MAX] = 'XXX';
var_dump($xxx);


That code will produce this exception:

Code: Select all
Arithmetic operation resulted in an overflow.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.OverflowException: Arithmetic operation resulted in an overflow.

Source Error:



Line 17: var_dump(PHP_INT_MAX);
Line 18:
Line 19: $xxx[PHP_INT_MAX] = 'XXX';
Line 20:
Line 21: var_dump($xxx);
 

 Source File:  C:\inetpub\deleteme01\sample.php    Line:  19

Stack Trace:



[OverflowException: Arithmetic operation resulted in an overflow.]
   PHP.Core.PhpHashtable.KeyAdded(Int32 key) +106
   PHP.Core.PhpHashtable.KeyAdded(IntStringKey& key) +111
   PHP.Core.OrderedDictionary._add_or_update_preserve_ref(PhpArray array, IntStringKey& key, Object value) +790
   PHP.Core.PhpArray.SetArrayItem(IntStringKey key, Object value) +139
   PHP.Core.PhpArray.SetArrayItem(Object key, Object value) +233
   PHP.Core.Operators.SetItemEpilogue(Object value, Object key, Object& var) +151
   PHP.Core.Operators.SetItemEpilogue(Object value, Int32 key, Object& var) +1237
   PHP.Core.Operators.SetItem(Object value, Int32 key, Object& var) +356
   <sample.php>.<Script>.<Main>(ScriptContext <context>, Dictionary`2 <locals>, DObject <self>, DTypeDesc <includer>, Boolean <request>) in C:\inetpub\deleteme01\sample.php:19
   PHP.Core.ScriptInfo.<get_Main>b__0(ScriptContext context, Dictionary`2 variables, DObject self, DTypeDesc includer, Boolean isMain) +102
   PHP.Core.ScriptContext.<IncludeScript>b__1(ScriptInfo scriptInfo) +75
   PHP.Core.ScriptContext.GuardedCall(Converter`2 routine, TData data, Boolean allowUserExceptions) +336

[PhpNetInternalException: Guarded call
InnerException: Arithmetic operation resulted in an overflow.
at    at PHP.Core.PhpHashtable.KeyAdded(Int32 key)
   at PHP.Core.PhpHashtable.KeyAdded(IntStringKey& key)
   at PHP.Core.OrderedDictionary._add_or_update_preserve_ref(PhpArray array, IntStringKey& key, Object value)
   at PHP.Core.PhpArray.SetArrayItem(IntStringKey key, Object value)
   at PHP.Core.PhpArray.SetArrayItem(Object key, Object value)
   at PHP.Core.Operators.SetItemEpilogue(Object value, Object key, Object& var)
   at PHP.Core.Operators.SetItemEpilogue(Object value, Int32 key, Object& var)
   at PHP.Core.Operators.SetItem(Object value, Int32 key, Object& var)
   at <sample.php>.<Script>.<Main>(ScriptContext <context>, Dictionary`2 <locals>, DObject <self>, DTypeDesc <includer>, Boolean <request>) in C:\inetpub\deleteme01\sample.php:line 19
   at PHP.Core.ScriptInfo.<get_Main>b__0(ScriptContext context, Dictionary`2 variables, DObject self, DTypeDesc includer, Boolean isMain)
   at PHP.Core.ScriptContext.<IncludeScript>b__1(ScriptInfo scriptInfo)
   at PHP.Core.ScriptContext.GuardedCall[TData,TResult](Converter`2 routine, TData data, Boolean allowUserExceptions)
]
   PHP.Core.ScriptContext.GuardedCall(Converter`2 routine, TData data, Boolean allowUserExceptions) +595
   PHP.Core.ScriptContext.IncludeScript(String relativeSourcePath, ScriptInfo script) +437
   PHP.Core.RequestContext.IncludeScript(String relativeSourcePath, ScriptInfo script) +157
   PHP.Core.RequestHandler.ProcessRequest(HttpContext context) +438
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

 


If I change the index to:

Code: Select all
var_dump(PHP_INT_MAX);
$xxx[PHP_INT_MAX - 1] = 'XXX';
var_dump($xxx);


It will work.

For code that I own, it is easy to fix, but for 3rd party libs or plugins it will case headaches!

Any ideas how to fix that?...

Thank you.
dulfe
 
Posts: 9
Joined: July 2nd, 2013, 6:38 pm

Re: PHP_INT_MAX as Array Key causes an Exception

Postby Jakub Misek » November 3rd, 2013, 3:43 pm

Hi,


thanks for noticing. This is caused by internal implementation of PHP arrays. We'll have to check how it behaves in legacy PHP, since next call
Code: Select all
$xxx[] = something;
should definitely fail.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2085
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: PHP_INT_MAX as Array Key causes an Exception

Postby Jakub Misek » November 3rd, 2013, 3:54 pm

Ok, I can see, PHP can add elements up to MAX_INT (or MAX_LONG), and every next added element is added at index MAX_INT. Phalanger fails, since it seamed as a "more" correct behaviour.

We'll take a look on possible solution for that.

Thanks!
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2085
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 1 guest

cron

User Control Panel

Login

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 89 on October 15th, 2018, 4:06 pm

Users browsing this forum: No registered users and 1 guest
Phalanger