r114919 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114918‎ | r114919 | r114920 >
Date:04:32, 16 April 2012
Author:tstarling
Status:deferred
Tags:
Comment:
* 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 (modified) (history)
  • /trunk/php/luasandbox/luasandbox.c (modified) (history)

Diff [purge]

Index: trunk/php/luasandbox/luasandbox.c
@@ -46,6 +46,7 @@
4747 static zend_bool luasandbox_instanceof(
4848 zend_class_entry *child_class, zend_class_entry *parent_class);
4949
 50+extern char luasandbox_timeout_message[];
5051
5152 zend_class_entry *luasandbox_ce;
5253 zend_class_entry *luasandboxerror_ce;
@@ -54,8 +55,8 @@
5556 zend_class_entry *luasandboxsyntaxerror_ce;
5657 zend_class_entry *luasandboxmemoryerror_ce;
5758 zend_class_entry *luasandboxerrorerror_ce;
58 -zend_class_entry *luasandboxtimeout_ce;
59 -zend_class_entry *luasandboxemergencytimeout_ce;
 59+zend_class_entry *luasandboxtimeouterror_ce;
 60+zend_class_entry *luasandboxemergencytimeouterror_ce;
6061 zend_class_entry *luasandboxplaceholder_ce;
6162 zend_class_entry *luasandboxfunction_ce;
6263
@@ -197,12 +198,12 @@
198199 luasandboxerrorerror_ce = zend_register_internal_class_ex(
199200 &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
200201
201 - INIT_CLASS_ENTRY(ce, "LuaSandboxTimeout", luasandbox_empty_methods);
202 - luasandboxtimeout_ce = zend_register_internal_class_ex(
 202+ INIT_CLASS_ENTRY(ce, "LuaSandboxTimeoutError", luasandbox_empty_methods);
 203+ luasandboxtimeouterror_ce = zend_register_internal_class_ex(
203204 &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
204205
205 - INIT_CLASS_ENTRY(ce, "LuaSandboxEmergencyTimeout", luasandbox_empty_methods);
206 - luasandboxemergencytimeout_ce = zend_register_internal_class_ex(
 206+ INIT_CLASS_ENTRY(ce, "LuaSandboxEmergencyTimeoutError", luasandbox_empty_methods);
 207+ luasandboxemergencytimeouterror_ce = zend_register_internal_class_ex(
207208 &ce, luasandboxfatalerror_ce, NULL TSRMLS_CC);
208209
209210 zend_declare_class_constant_long(luasandboxerror_ce,
@@ -551,7 +552,7 @@
552553 lua_close(sandbox->state);
553554 sandbox->state = NULL;
554555 sandbox->emergency_timed_out = 0;
555 - zend_throw_exception(luasandboxemergencytimeout_ce,
 556+ zend_throw_exception(luasandboxemergencytimeouterror_ce,
556557 "The maximum execution time was exceeded "
557558 "and the current Lua statement failed to return, leading to "
558559 "destruction of the Lua state", LUA_ERRRUN);
@@ -568,11 +569,21 @@
569570 {
570571 const char * errorMsg = luasandbox_error_to_string(L, -1);
571572 zend_class_entry * ce;
572 - lua_pop(L, 1);
573573 if (!EG(exception)) {
 574+ if (luasandbox_is_fatal(L, -1) && !strcmp(errorMsg, luasandbox_timeout_message)) {
 575+ ce = luasandboxtimeouterror_ce;
 576+ }
574577 switch (status) {
575578 case LUA_ERRRUN:
576 - ce = luasandboxruntimeerror_ce;
 579+ if (luasandbox_is_fatal(L, -1)) {
 580+ if (!strcmp(errorMsg, luasandbox_timeout_message)) {
 581+ ce = luasandboxtimeouterror_ce;
 582+ } else {
 583+ ce = luasandboxfatalerror_ce;
 584+ }
 585+ } else {
 586+ ce = luasandboxruntimeerror_ce;
 587+ }
577588 break;
578589 case LUA_ERRSYNTAX:
579590 ce = luasandboxsyntaxerror_ce;
@@ -586,6 +597,7 @@
587598 }
588599 zend_throw_exception(ce, (char*)errorMsg, status);
589600 }
 601+ lua_pop(L, 1);
590602 }
591603 /* }}} */
592604
Index: trunk/php/luasandbox/data_conversion.c
@@ -7,6 +7,7 @@
88 #include <lauxlib.h>
99 #include <limits.h>
1010 #include <float.h>
 11+#include <math.h>
1112
1213 #include "php.h"
1314 #include "php_luasandbox.h"
@@ -332,10 +333,12 @@
333334 const char * str;
334335 size_t length;
335336 zval *value;
 337+ lua_Number n;
 338+ int top = lua_gettop(L);
336339
337340 // Normalise the input index so that we can push without invalidating it.
338341 if (index < 0) {
339 - index += lua_gettop(L) + 1;
 342+ index += top + 1;
340343 }
341344
342345 lua_pushnil(L);
@@ -343,13 +346,23 @@
344347 MAKE_STD_ZVAL(value);
345348 luasandbox_lua_to_zval(value, L, -1, sandbox_zval, recursionGuard TSRMLS_CC);
346349
 350+ if (lua_type(L, -2) == LUA_TNUMBER) {
 351+ n = lua_tonumber(L, -2);
 352+ if (n == floor(n)) {
 353+ // Integer key
 354+ zend_hash_index_update(ht, n, (void*)&value, sizeof(zval*), NULL);
 355+ lua_settop(L, top + 1);
 356+ continue;
 357+ }
 358+ }
 359+
347360 // Make a copy of the key so that we can call lua_tolstring() which is destructive
348361 lua_pushvalue(L, -2);
349362 str = lua_tolstring(L, -1, &length);
350363 zend_hash_update(ht, str, length + 1, (void*)&value, sizeof(zval*), NULL);
351364
352 - // Delete the copy and the value
353 - lua_pop(L, 2);
 365+ // Pop temporary values off the stack
 366+ lua_settop(L, top + 1);
354367 }
355368 }
356369 /* }}} */

Status & tagging log