Avoid macros luaL_loadbuffer and luaL_loadfile

Use luaL_loadbufferx and luaL_loadfilex instead, being explicit about
whether to accept binary chunks.
This commit is contained in:
Roberto I 2026-04-07 13:42:34 -03:00
parent c037162a1a
commit 29cf284089
6 changed files with 17 additions and 15 deletions

View file

@ -874,7 +874,7 @@ LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
return luaL_loadbuffer(L, s, strlen(s), s); return luaL_loadbufferx(L, s, strlen(s), s, "t");
} }
/* }====================================================== */ /* }====================================================== */

View file

@ -340,9 +340,11 @@ static int load_aux (lua_State *L, int status, int envidx) {
static const char *getMode (lua_State *L, int idx) { static const char *getMode (lua_State *L, int idx) {
const char *mode = luaL_optstring(L, idx, "bt"); const char *mode = luaL_optstring(L, idx, NULL);
if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ if (mode != NULL && strchr(mode, 'B') != NULL) {
/* Lua code cannot use fixed buffers */
luaL_argerror(L, idx, "invalid mode"); luaL_argerror(L, idx, "invalid mode");
}
return mode; return mode;
} }
@ -425,7 +427,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
static int luaB_dofile (lua_State *L) { static int luaB_dofile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL); const char *fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1); lua_settop(L, 1);
if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) if (l_unlikely(luaL_loadfilex(L, fname, "bt") != LUA_OK))
return lua_error(L); return lua_error(L);
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
return dofilecont(L, 0, 0); return dofilecont(L, 0, 0);

View file

@ -427,7 +427,7 @@ static int db_debug (lua_State *L) {
if (fgets(buffer, sizeof(buffer), stdin) == NULL || if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
strcmp(buffer, "cont\n") == 0) strcmp(buffer, "cont\n") == 0)
return 0; return 0;
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || if (luaL_loadbufferx(L, buffer, strlen(buffer), "=(debug command)", "t") ||
lua_pcall(L, 0, 0, 0)) lua_pcall(L, 0, 0, 0))
lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
lua_settop(L, 0); /* remove eventual returns */ lua_settop(L, 0); /* remove eventual returns */

View file

@ -541,7 +541,7 @@ static int searcher_Lua (lua_State *L) {
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
filename = findfile(L, name, "path", LUA_LSUBSEP); filename = findfile(L, name, "path", LUA_LSUBSEP);
if (filename == NULL) return 1; /* module not found in this path */ if (filename == NULL) return 1; /* module not found in this path */
return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); return checkload(L, (luaL_loadfilex(L, filename, "bt") == LUA_OK), filename);
} }

View file

@ -1302,7 +1302,7 @@ static int doonnewstack (lua_State *L) {
lua_State *L1 = lua_newthread(L); lua_State *L1 = lua_newthread(L);
size_t l; size_t l;
const char *s = luaL_checklstring(L, 1, &l); const char *s = luaL_checklstring(L, 1, &l);
int status = luaL_loadbuffer(L1, s, l, s); int status = luaL_loadbufferx(L1, s, l, s, "t");
if (status == LUA_OK) if (status == LUA_OK)
status = lua_pcall(L1, 0, 0, 0); status = lua_pcall(L1, 0, 0, 0);
lua_pushinteger(L, status); lua_pushinteger(L, status);
@ -1382,7 +1382,7 @@ static int doremote (lua_State *L) {
const char *code = luaL_checklstring(L, 2, &lcode); const char *code = luaL_checklstring(L, 2, &lcode);
int status; int status;
lua_settop(L1, 0); lua_settop(L1, 0);
status = luaL_loadbuffer(L1, code, lcode, code); status = luaL_loadbufferx(L1, code, lcode, code, "t");
if (status == LUA_OK) if (status == LUA_OK)
status = lua_pcall(L1, 0, LUA_MULTRET, 0); status = lua_pcall(L1, 0, LUA_MULTRET, 0);
if (status != LUA_OK) { if (status != LUA_OK) {
@ -1738,7 +1738,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
lua_pushinteger(L1, luaL_len(L1, getindex)); lua_pushinteger(L1, luaL_len(L1, getindex));
} }
else if EQ("loadfile") { else if EQ("loadfile") {
luaL_loadfile(L1, luaL_checkstring(L1, getnum)); luaL_loadfilex(L1, luaL_checkstring(L1, getnum), "t");
} }
else if EQ("loadstring") { else if EQ("loadstring") {
size_t slen; size_t slen;

12
lua.c
View file

@ -207,12 +207,12 @@ static int dochunk (lua_State *L, int status) {
static int dofile (lua_State *L, const char *name) { static int dofile (lua_State *L, const char *name) {
return dochunk(L, luaL_loadfile(L, name)); return dochunk(L, luaL_loadfilex(L, name, "bt"));
} }
static int dostring (lua_State *L, const char *s, const char *name) { static int dostring (lua_State *L, const char *s, const char *name) {
return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); return dochunk(L, luaL_loadbufferx(L, s, strlen(s), name, "t"));
} }
@ -266,7 +266,7 @@ static int handle_script (lua_State *L, char **argv) {
const char *fname = argv[0]; const char *fname = argv[0];
if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
fname = NULL; /* stdin */ fname = NULL; /* stdin */
status = luaL_loadfile(L, fname); status = luaL_loadfilex(L, fname, "bt");
if (status == LUA_OK) { if (status == LUA_OK) {
int n = pushargs(L); /* push arguments to script */ int n = pushargs(L); /* push arguments to script */
status = docall(L, n, LUA_MULTRET); status = docall(L, n, LUA_MULTRET);
@ -609,11 +609,11 @@ static int pushline (lua_State *L, int firstline) {
static int addreturn (lua_State *L) { static int addreturn (lua_State *L) {
const char *line = lua_tostring(L, -1); /* original line */ const char *line = lua_tostring(L, -1); /* original line */
const char *retline = lua_pushfstring(L, "return %s;", line); const char *retline = lua_pushfstring(L, "return %s;", line);
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); int status = luaL_loadbufferx(L, retline, strlen(retline), "=stdin", "t");
if (status == LUA_OK) if (status == LUA_OK)
lua_remove(L, -2); /* remove modified line */ lua_remove(L, -2); /* remove modified line */
else else
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ lua_pop(L, 2); /* pop result from 'luaL_loadbufferx' and modified line */
return status; return status;
} }
@ -640,7 +640,7 @@ static int multiline (lua_State *L) {
const char *line = lua_tolstring(L, 1, &len); /* get first line */ const char *line = lua_tolstring(L, 1, &len); /* get first line */
checklocal(line); checklocal(line);
for (;;) { /* repeat until gets a complete statement */ for (;;) { /* repeat until gets a complete statement */
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ int status = luaL_loadbufferx(L, line, len, "=stdin", "t"); /* try it */
if (!incomplete(L, status) || !pushline(L, 0)) if (!incomplete(L, status) || !pushline(L, 0))
return status; /* should not or cannot try to add continuation line */ return status; /* should not or cannot try to add continuation line */
lua_remove(L, -2); /* remove error message (from incomplete line) */ lua_remove(L, -2); /* remove error message (from incomplete line) */