From 27c5a5c4f3dc14bce36cec10fc3242f43e9c73e8 Mon Sep 17 00:00:00 2001 From: Nicholaos Mouzourakis Date: Sun, 23 Mar 2025 18:12:06 -0400 Subject: [PATCH] Fixing LUA MemTextViewer calls. --- arm9/source/lua/gm9ui.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/arm9/source/lua/gm9ui.c b/arm9/source/lua/gm9ui.c index e3acba1..2028136 100644 --- a/arm9/source/lua/gm9ui.c +++ b/arm9/source/lua/gm9ui.c @@ -206,12 +206,16 @@ static int ui_show_text_viewer(lua_State* L) { // validate text ourselves so we can return a better error // MemTextViewer calls ShowPrompt if it's bad, and i don't want that + + if (len >= STD_BUFFER_SIZE) { + return luaL_error(L, "text is too large"); + } if (!(ValidateText(text, len))) { return luaL_error(L, "text validation failed"); } - if (!(MemTextViewer(text, len, 1, false))) { + if (!(MemTextViewer(text, len, 1, false, 0, NULL))) { return luaL_error(L, "failed to run MemTextViewer"); } @@ -225,6 +229,11 @@ static int ui_show_file_text_viewer(lua_State* L) { // validate text ourselves so we can return a better error // MemTextViewer calls ShowPrompt if it's bad, and i don't want that // and FileTextViewer calls the above function + + size_t fileSize = FileGetSize(path); + if (fileSize >= STD_BUFFER_SIZE) { + return luaL_error(L, "text file is too large"); + } char* text = malloc(STD_BUFFER_SIZE); if (!text) { @@ -233,16 +242,14 @@ static int ui_show_file_text_viewer(lua_State* L) { // TODO: replace this with something that can detect file read errors and actual 0-length files size_t flen = FileGetData(path, text, STD_BUFFER_SIZE - 1, 0); - text[flen] = '\0'; - u32 len = (ptrdiff_t)memchr(text, '\0', flen + 1) - (ptrdiff_t)text; - - if (!(ValidateText(text, len))) { + + if (!(ValidateText(text, flen))) { free(text); return luaL_error(L, "text validation failed"); } - if (!(MemTextViewer(text, len, 1, false))) { + if (!(MemTextViewer(text, flen, 1, false, STD_BUFFER_SIZE - 1, path))) { free(text); return luaL_error(L, "failed to run MemTextViewer"); }