Add an option to disable loadfile APIs

This commit is contained in:
Matt Messier 2023-01-12 08:20:38 -05:00
parent d0e88930dd
commit df1336f86c
5 changed files with 24 additions and 0 deletions

View File

@ -387,6 +387,8 @@ void emit_lib(BuildCtx *ctx)
ok = LJ_HASFFI;
else if (!strcmp(buf, "#if LJ_HASBUFFER\n"))
ok = LJ_HASBUFFER;
else if (!strcmp(buf, "#if LJ_HASLOADFILE\n"))
ok = LJ_HASLOADFILE;
if (!ok) {
int lvl = 1;
while (fgets(buf, sizeof(buf), fp) != NULL) {

View File

@ -62,7 +62,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
LUALIB_API int (luaL_ref) (lua_State *L, int t);
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
#ifndef LUAJIT_DISABLE_LOADFILE
LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
#endif
LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
const char *name);
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
@ -79,8 +81,10 @@ LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
/* From Lua 5.2. */
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname);
LUALIB_API int luaL_execresult(lua_State *L, int stat);
#ifndef LUAJIT_DISABLE_LOADFILE
LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
const char *mode);
#endif
LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
const char *name, const char *mode);
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,

View File

@ -373,6 +373,8 @@ static int load_aux(lua_State *L, int status, int envarg)
}
}
#if LJ_HASLOADFILE
LJLIB_CF(loadfile)
{
GCstr *fname = lj_lib_optstr(L, 1);
@ -384,6 +386,8 @@ LJLIB_CF(loadfile)
return load_aux(L, status, 3);
}
#endif
static const char *reader_func(lua_State *L, void *ud, size_t *size)
{
UNUSED(ud);
@ -439,6 +443,8 @@ LJLIB_CF(loadstring)
return lj_cf_load(L);
}
#if LJ_HASLOADFILE
LJLIB_CF(dofile)
{
GCstr *fname = lj_lib_optstr(L, 1);
@ -450,6 +456,8 @@ LJLIB_CF(dofile)
return (int)(L->top - L->base) - 1;
}
#endif
/* -- Base library: GC control -------------------------------------------- */
LJLIB_CF(gcinfo)

View File

@ -594,6 +594,12 @@
#define LJ_HASPROFILE 0
#endif
#if defined(LUAJIT_DISABLE_LOADFILE)
#define LJ_HASLOADFILE 0
#else
#define LJ_HASLOADFILE 1
#endif
#ifndef LJ_ARCH_HASFPU
#define LJ_ARCH_HASFPU 1
#endif

View File

@ -67,6 +67,8 @@ LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
return lua_loadx(L, reader, data, chunkname, NULL);
}
#if LJ_HASLOADFILE
typedef struct FileReaderCtx {
FILE *fp;
char buf[LUAL_BUFFERSIZE];
@ -119,6 +121,8 @@ LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)
return luaL_loadfilex(L, filename, NULL);
}
#endif
typedef struct StringReaderCtx {
const char *str;
size_t size;