@@ -372,14 +372,16 @@ bool ScriptApiSecurity::isSecure(lua_State *L)
372
372
return secure;
373
373
}
374
374
375
-
376
- #define CHECK_FILE_ERR (ret, fp ) \
377
- if (ret) { \
378
- lua_pushfstring (L, " %s: %s" , path, strerror (errno)); \
379
- if (fp) std::fclose (fp); \
380
- return false ; \
375
+ bool ScriptApiSecurity::safeLoadString (lua_State *L, const std::string &code, const char *chunk_name)
376
+ {
377
+ if (code.size () > 0 && code[0 ] == LUA_SIGNATURE[0 ]) {
378
+ lua_pushliteral (L, " Bytecode prohibited when mod security is enabled." );
379
+ return false ;
381
380
}
382
-
381
+ if (luaL_loadbuffer (L, code.data (), code.size (), chunk_name))
382
+ return false ;
383
+ return true ;
384
+ }
383
385
384
386
bool ScriptApiSecurity::safeLoadFile (lua_State *L, const char *path, const char *display_name)
385
387
{
@@ -406,68 +408,49 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path, const char
406
408
int c = std::getc (fp);
407
409
if (c == ' #' ) {
408
410
// Skip the first line
409
- while ((c = std::getc (fp)) != EOF && c != ' \n ' );
410
- if (c == ' \n ' ) c = std::getc (fp);
411
+ while ((c = std::getc (fp)) != EOF && c != ' \n ' ) {}
412
+ if (c == ' \n ' )
413
+ std::getc (fp);
411
414
start = std::ftell (fp);
412
415
}
413
416
414
- if (c == LUA_SIGNATURE[0 ]) {
415
- lua_pushliteral (L, " Bytecode prohibited when mod security is enabled." );
416
- std::fclose (fp);
417
- if (path) {
418
- delete [] chunk_name;
419
- }
420
- return false ;
421
- }
422
-
423
417
// Read the file
424
418
int ret = std::fseek (fp, 0 , SEEK_END);
425
419
if (ret) {
426
420
lua_pushfstring (L, " %s: %s" , path, strerror (errno));
427
- std::fclose (fp);
428
421
if (path) {
422
+ std::fclose (fp);
429
423
delete [] chunk_name;
430
424
}
431
425
return false ;
432
426
}
433
427
434
428
size_t size = std::ftell (fp) - start;
435
- char * code = new char [ size] ;
429
+ std::string code ( size, ' \0 ' ) ;
436
430
ret = std::fseek (fp, start, SEEK_SET);
437
431
if (ret) {
438
432
lua_pushfstring (L, " %s: %s" , path, strerror (errno));
439
- std::fclose (fp);
440
- delete [] code;
441
433
if (path) {
434
+ std::fclose (fp);
442
435
delete [] chunk_name;
443
436
}
444
437
return false ;
445
438
}
446
439
447
- size_t num_read = std::fread (code, 1 , size, fp);
448
- if (path) {
440
+ size_t num_read = std::fread (& code[ 0 ] , 1 , size, fp);
441
+ if (path)
449
442
std::fclose (fp);
450
- }
451
443
if (num_read != size) {
452
444
lua_pushliteral (L, " Error reading file to load." );
453
- delete [] code;
454
- if (path) {
445
+ if (path)
455
446
delete [] chunk_name;
456
- }
457
- return false ;
458
- }
459
-
460
- if (luaL_loadbuffer (L, code, size, chunk_name)) {
461
- delete [] code;
462
447
return false ;
463
448
}
464
449
465
- delete [] code;
466
-
467
- if (path) {
450
+ bool result = safeLoadString (L, code, chunk_name);
451
+ if (path)
468
452
delete [] chunk_name;
469
- }
470
- return true ;
453
+ return result;
471
454
}
472
455
473
456
@@ -628,14 +611,9 @@ int ScriptApiSecurity::sl_g_load(lua_State *L)
628
611
code += std::string (buf, len);
629
612
lua_pop (L, 1 ); // Pop return value
630
613
}
631
- if (code[0 ] == LUA_SIGNATURE[0 ]) {
632
- lua_pushnil (L);
633
- lua_pushliteral (L, " Bytecode prohibited when mod security is enabled." );
634
- return 2 ;
635
- }
636
- if (luaL_loadbuffer (L, code.data (), code.size (), chunk_name)) {
614
+ if (!safeLoadString (L, code, chunk_name)) {
637
615
lua_pushnil (L);
638
- lua_insert (L, lua_gettop (L) - 1 );
616
+ lua_insert (L, - 2 );
639
617
return 2 ;
640
618
}
641
619
return 1 ;
@@ -694,15 +672,11 @@ int ScriptApiSecurity::sl_g_loadstring(lua_State *L)
694
672
695
673
size_t size;
696
674
const char *code = lua_tolstring (L, 1 , &size);
675
+ std::string code_s (code, size);
697
676
698
- if (size > 0 && code[ 0 ] == LUA_SIGNATURE[ 0 ] ) {
677
+ if (! safeLoadString (L, code_s, chunk_name) ) {
699
678
lua_pushnil (L);
700
- lua_pushliteral (L, " Bytecode prohibited when mod security is enabled." );
701
- return 2 ;
702
- }
703
- if (luaL_loadbuffer (L, code, size, chunk_name)) {
704
- lua_pushnil (L);
705
- lua_insert (L, lua_gettop (L) - 1 );
679
+ lua_insert (L, -2 );
706
680
return 2 ;
707
681
}
708
682
return 1 ;
0 commit comments