https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114919

Revision: 114919
Author:   tstarling
Date:     2012-04-16 04:32:08 +0000 (Mon, 16 Apr 2012)
Log Message:
-----------
* Renamed exception classes LuaSandboxTimeout and LuaSandboxEmergencyTimeout to 
LuaSandboxTimeoutError and LuaSandboxEmergencyTimeoutError, for consistency
* Actually use LuaSandboxTimeoutError when a timeout is hit, not 
LuaSandboxRuntimeError
* When converting data from Lua to PHP, don't insert integer keys into 
hashtables as strings, insert them as proper integers. This allows the 
soon-to-be-committed Scribunto unit tests, which rely on array-equals, to pass.

Modified Paths:
--------------
    trunk/php/luasandbox/data_conversion.c
    trunk/php/luasandbox/luasandbox.c

Modified: trunk/php/luasandbox/data_conversion.c
===================================================================
--- trunk/php/luasandbox/data_conversion.c      2012-04-16 03:45:29 UTC (rev 
114918)
+++ trunk/php/luasandbox/data_conversion.c      2012-04-16 04:32:08 UTC (rev 
114919)
@@ -7,6 +7,7 @@
 #include <lauxlib.h>
 #include <limits.h>
 #include <float.h>
+#include <math.h>
 
 #include "php.h"
 #include "php_luasandbox.h"
@@ -332,10 +333,12 @@
        const char * str;
        size_t length;
        zval *value;
+       lua_Number n;
+       int top = lua_gettop(L);
 
        // Normalise the input index so that we can push without invalidating 
it.
        if (index < 0) {
-               index += lua_gettop(L) + 1;
+               index += top + 1;
        }
 
        lua_pushnil(L);
@@ -343,13 +346,23 @@
                MAKE_STD_ZVAL(value);
                luasandbox_lua_to_zval(value, L, -1, sandbox_zval, 
recursionGuard TSRMLS_CC);
                
+               if (lua_type(L, -2) == LUA_TNUMBER) {
+                       n = lua_tonumber(L, -2);
+                       if (n == floor(n)) {
+                               // Integer key
+                               zend_hash_index_update(ht, n, (void*)&value, 
sizeof(zval*), NULL);
+                               lua_settop(L, top + 1);
+                               continue;
+                       }
+               }
+
                // Make a copy of the key so that we can call lua_tolstring() 
which is destructive
                lua_pushvalue(L, -2);
                str = lua_tolstring(L, -1, &length);
                zend_hash_update(ht, str, length + 1, (void*)&value, 
sizeof(zval*), NULL);
 
-               // Delete the copy and the value
-               lua_pop(L, 2);
+               // Pop temporary values off the stack
+               lua_settop(L, top + 1);
        }
 }
 /* }}} */

Modified: trunk/php/luasandbox/luasandbox.c
===================================================================
--- trunk/php/luasandbox/luasandbox.c   2012-04-16 03:45:29 UTC (rev 114918)
+++ trunk/php/luasandbox/luasandbox.c   2012-04-16 04:32:08 UTC (rev 114919)
@@ -46,6 +46,7 @@
 static zend_bool luasandbox_instanceof(
        zend_class_entry *child_class, zend_class_entry *parent_class);
 
+extern char luasandbox_timeout_message[];
 
 zend_class_entry *luasandbox_ce;
 zend_class_entry *luasandboxerror_ce;
@@ -54,8 +55,8 @@
 zend_class_entry *luasandboxsyntaxerror_ce;
 zend_class_entry *luasandboxmemoryerror_ce;
 zend_class_entry *luasandboxerrorerror_ce;
-zend_class_entry *luasandboxtimeout_ce;
-zend_class_entry *luasandboxemergencytimeout_ce;
+zend_class_entry *luasandboxtimeouterror_ce;
+zend_class_entry *luasandboxemergencytimeouterror_ce;
 zend_class_entry *luasandboxplaceholder_ce;
 zend_class_entry *luasandboxfunction_ce;
 
@@ -197,12 +198,12 @@
        luasandboxerrorerror_ce = zend_register_internal_class_ex(
                        &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
 
-       INIT_CLASS_ENTRY(ce, "LuaSandboxTimeout", luasandbox_empty_methods);
-       luasandboxtimeout_ce = zend_register_internal_class_ex(
+       INIT_CLASS_ENTRY(ce, "LuaSandboxTimeoutError", 
luasandbox_empty_methods);
+       luasandboxtimeouterror_ce = zend_register_internal_class_ex(
                        &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
 
-       INIT_CLASS_ENTRY(ce, "LuaSandboxEmergencyTimeout", 
luasandbox_empty_methods);
-       luasandboxemergencytimeout_ce = zend_register_internal_class_ex(
+       INIT_CLASS_ENTRY(ce, "LuaSandboxEmergencyTimeoutError", 
luasandbox_empty_methods);
+       luasandboxemergencytimeouterror_ce = zend_register_internal_class_ex(
                &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
 
        zend_declare_class_constant_long(luasandboxerror_ce, 
@@ -551,7 +552,7 @@
        lua_close(sandbox->state);
        sandbox->state = NULL;
        sandbox->emergency_timed_out = 0;
-       zend_throw_exception(luasandboxemergencytimeout_ce, 
+       zend_throw_exception(luasandboxemergencytimeouterror_ce, 
                "The maximum execution time was exceeded "
                "and the current Lua statement failed to return, leading to "
                "destruction of the Lua state", LUA_ERRRUN);
@@ -568,11 +569,21 @@
 {
        const char * errorMsg = luasandbox_error_to_string(L, -1);
        zend_class_entry * ce;
-       lua_pop(L, 1);
        if (!EG(exception)) {
+               if (luasandbox_is_fatal(L, -1) && !strcmp(errorMsg, 
luasandbox_timeout_message)) {
+                       ce = luasandboxtimeouterror_ce;
+               }
                switch (status) {
                        case LUA_ERRRUN:
-                               ce = luasandboxruntimeerror_ce;
+                               if (luasandbox_is_fatal(L, -1)) {
+                                       if (!strcmp(errorMsg, 
luasandbox_timeout_message)) {
+                                               ce = luasandboxtimeouterror_ce;
+                                       } else {
+                                               ce = luasandboxfatalerror_ce;
+                                       }
+                               } else {
+                                       ce = luasandboxruntimeerror_ce;
+                               }
                                break;
                        case LUA_ERRSYNTAX:
                                ce = luasandboxsyntaxerror_ce;
@@ -586,6 +597,7 @@
                }
                zend_throw_exception(ce, (char*)errorMsg, status);
        }
+       lua_pop(L, 1);
 }
 /* }}} */
 


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to