aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto I <roberto@inf.puc-rio.br>2026-04-07 13:42:34 -0300
committerRoberto I <roberto@inf.puc-rio.br>2026-04-07 13:42:34 -0300
commit29cf284089d543408d726440a3f1acaecdf73636 (patch)
treee18363e7d53bb64d6f82943556ff0c3110778837
parentc037162a1a657088d722f550e287015525bb2259 (diff)
downloadlua-29cf284089d543408d726440a3f1acaecdf73636.tar.gz
lua-29cf284089d543408d726440a3f1acaecdf73636.tar.bz2
lua-29cf284089d543408d726440a3f1acaecdf73636.zip
Avoid macros luaL_loadbuffer and luaL_loadfile
Use luaL_loadbufferx and luaL_loadfilex instead, being explicit about whether to accept binary chunks.
-rw-r--r--lauxlib.c2
-rw-r--r--lbaselib.c8
-rw-r--r--ldblib.c2
-rw-r--r--loadlib.c2
-rw-r--r--ltests.c6
-rw-r--r--lua.c12
6 files changed, 17 insertions, 15 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 7cf90cb7..af44418a 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -874,7 +874,7 @@ LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
874 874
875 875
876LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { 876LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
877 return luaL_loadbuffer(L, s, strlen(s), s); 877 return luaL_loadbufferx(L, s, strlen(s), s, "t");
878} 878}
879 879
880/* }====================================================== */ 880/* }====================================================== */
diff --git a/lbaselib.c b/lbaselib.c
index 891bb90f..47f7bdec 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -340,9 +340,11 @@ static int load_aux (lua_State *L, int status, int envidx) {
340 340
341 341
342static const char *getMode (lua_State *L, int idx) { 342static const char *getMode (lua_State *L, int idx) {
343 const char *mode = luaL_optstring(L, idx, "bt"); 343 const char *mode = luaL_optstring(L, idx, NULL);
344 if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ 344 if (mode != NULL && strchr(mode, 'B') != NULL) {
345 /* Lua code cannot use fixed buffers */
345 luaL_argerror(L, idx, "invalid mode"); 346 luaL_argerror(L, idx, "invalid mode");
347 }
346 return mode; 348 return mode;
347} 349}
348 350
@@ -425,7 +427,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
425static int luaB_dofile (lua_State *L) { 427static int luaB_dofile (lua_State *L) {
426 const char *fname = luaL_optstring(L, 1, NULL); 428 const char *fname = luaL_optstring(L, 1, NULL);
427 lua_settop(L, 1); 429 lua_settop(L, 1);
428 if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) 430 if (l_unlikely(luaL_loadfilex(L, fname, "bt") != LUA_OK))
429 return lua_error(L); 431 return lua_error(L);
430 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); 432 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
431 return dofilecont(L, 0, 0); 433 return dofilecont(L, 0, 0);
diff --git a/ldblib.c b/ldblib.c
index c7b74812..051327cc 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -427,7 +427,7 @@ static int db_debug (lua_State *L) {
427 if (fgets(buffer, sizeof(buffer), stdin) == NULL || 427 if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
428 strcmp(buffer, "cont\n") == 0) 428 strcmp(buffer, "cont\n") == 0)
429 return 0; 429 return 0;
430 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 430 if (luaL_loadbufferx(L, buffer, strlen(buffer), "=(debug command)", "t") ||
431 lua_pcall(L, 0, 0, 0)) 431 lua_pcall(L, 0, 0, 0))
432 lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); 432 lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
433 lua_settop(L, 0); /* remove eventual returns */ 433 lua_settop(L, 0); /* remove eventual returns */
diff --git a/loadlib.c b/loadlib.c
index 8d2e68e2..ef09c9a5 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -541,7 +541,7 @@ static int searcher_Lua (lua_State *L) {
541 const char *name = luaL_checkstring(L, 1); 541 const char *name = luaL_checkstring(L, 1);
542 filename = findfile(L, name, "path", LUA_LSUBSEP); 542 filename = findfile(L, name, "path", LUA_LSUBSEP);
543 if (filename == NULL) return 1; /* module not found in this path */ 543 if (filename == NULL) return 1; /* module not found in this path */
544 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); 544 return checkload(L, (luaL_loadfilex(L, filename, "bt") == LUA_OK), filename);
545} 545}
546 546
547 547
diff --git a/ltests.c b/ltests.c
index ce2b20ca..6ae5f472 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1302,7 +1302,7 @@ static int doonnewstack (lua_State *L) {
1302 lua_State *L1 = lua_newthread(L); 1302 lua_State *L1 = lua_newthread(L);
1303 size_t l; 1303 size_t l;
1304 const char *s = luaL_checklstring(L, 1, &l); 1304 const char *s = luaL_checklstring(L, 1, &l);
1305 int status = luaL_loadbuffer(L1, s, l, s); 1305 int status = luaL_loadbufferx(L1, s, l, s, "t");
1306 if (status == LUA_OK) 1306 if (status == LUA_OK)
1307 status = lua_pcall(L1, 0, 0, 0); 1307 status = lua_pcall(L1, 0, 0, 0);
1308 lua_pushinteger(L, status); 1308 lua_pushinteger(L, status);
@@ -1382,7 +1382,7 @@ static int doremote (lua_State *L) {
1382 const char *code = luaL_checklstring(L, 2, &lcode); 1382 const char *code = luaL_checklstring(L, 2, &lcode);
1383 int status; 1383 int status;
1384 lua_settop(L1, 0); 1384 lua_settop(L1, 0);
1385 status = luaL_loadbuffer(L1, code, lcode, code); 1385 status = luaL_loadbufferx(L1, code, lcode, code, "t");
1386 if (status == LUA_OK) 1386 if (status == LUA_OK)
1387 status = lua_pcall(L1, 0, LUA_MULTRET, 0); 1387 status = lua_pcall(L1, 0, LUA_MULTRET, 0);
1388 if (status != LUA_OK) { 1388 if (status != LUA_OK) {
@@ -1738,7 +1738,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1738 lua_pushinteger(L1, luaL_len(L1, getindex)); 1738 lua_pushinteger(L1, luaL_len(L1, getindex));
1739 } 1739 }
1740 else if EQ("loadfile") { 1740 else if EQ("loadfile") {
1741 luaL_loadfile(L1, luaL_checkstring(L1, getnum)); 1741 luaL_loadfilex(L1, luaL_checkstring(L1, getnum), "t");
1742 } 1742 }
1743 else if EQ("loadstring") { 1743 else if EQ("loadstring") {
1744 size_t slen; 1744 size_t slen;
diff --git a/lua.c b/lua.c
index 3f4fc9f7..3107a674 100644
--- a/lua.c
+++ b/lua.c
@@ -207,12 +207,12 @@ static int dochunk (lua_State *L, int status) {
207 207
208 208
209static int dofile (lua_State *L, const char *name) { 209static int dofile (lua_State *L, const char *name) {
210 return dochunk(L, luaL_loadfile(L, name)); 210 return dochunk(L, luaL_loadfilex(L, name, "bt"));
211} 211}
212 212
213 213
214static int dostring (lua_State *L, const char *s, const char *name) { 214static int dostring (lua_State *L, const char *s, const char *name) {
215 return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); 215 return dochunk(L, luaL_loadbufferx(L, s, strlen(s), name, "t"));
216} 216}
217 217
218 218
@@ -266,7 +266,7 @@ static int handle_script (lua_State *L, char **argv) {
266 const char *fname = argv[0]; 266 const char *fname = argv[0];
267 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) 267 if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
268 fname = NULL; /* stdin */ 268 fname = NULL; /* stdin */
269 status = luaL_loadfile(L, fname); 269 status = luaL_loadfilex(L, fname, "bt");
270 if (status == LUA_OK) { 270 if (status == LUA_OK) {
271 int n = pushargs(L); /* push arguments to script */ 271 int n = pushargs(L); /* push arguments to script */
272 status = docall(L, n, LUA_MULTRET); 272 status = docall(L, n, LUA_MULTRET);
@@ -609,11 +609,11 @@ static int pushline (lua_State *L, int firstline) {
609static int addreturn (lua_State *L) { 609static int addreturn (lua_State *L) {
610 const char *line = lua_tostring(L, -1); /* original line */ 610 const char *line = lua_tostring(L, -1); /* original line */
611 const char *retline = lua_pushfstring(L, "return %s;", line); 611 const char *retline = lua_pushfstring(L, "return %s;", line);
612 int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); 612 int status = luaL_loadbufferx(L, retline, strlen(retline), "=stdin", "t");
613 if (status == LUA_OK) 613 if (status == LUA_OK)
614 lua_remove(L, -2); /* remove modified line */ 614 lua_remove(L, -2); /* remove modified line */
615 else 615 else
616 lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ 616 lua_pop(L, 2); /* pop result from 'luaL_loadbufferx' and modified line */
617 return status; 617 return status;
618} 618}
619 619
@@ -640,7 +640,7 @@ static int multiline (lua_State *L) {
640 const char *line = lua_tolstring(L, 1, &len); /* get first line */ 640 const char *line = lua_tolstring(L, 1, &len); /* get first line */
641 checklocal(line); 641 checklocal(line);
642 for (;;) { /* repeat until gets a complete statement */ 642 for (;;) { /* repeat until gets a complete statement */
643 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ 643 int status = luaL_loadbufferx(L, line, len, "=stdin", "t"); /* try it */
644 if (!incomplete(L, status) || !pushline(L, 0)) 644 if (!incomplete(L, status) || !pushline(L, 0))
645 return status; /* should not or cannot try to add continuation line */ 645 return status; /* should not or cannot try to add continuation line */
646 lua_remove(L, -2); /* remove error message (from incomplete line) */ 646 lua_remove(L, -2); /* remove error message (from incomplete line) */