diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index ed7b0e586..e16506914 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -602,12 +602,21 @@ struct Mod* get_active_mod(void) { } LuaTable get_mod_files(struct Mod* mod, const char* subDirectory) { - char* normalizedSubDir = malloc(strlen(subDirectory) + 2); - strcpy(normalizedSubDir, subDirectory); + if (!mod || !subDirectory) { + struct lua_State *L = gLuaState; + if (L) { + lua_newtable(L); + return smlua_to_lua_table(L, -1); + } + return 0; + } + + char normalizedSubDir[SYS_MAX_PATH] = { 0 }; + snprintf(normalizedSubDir, SYS_MAX_PATH, "%s", subDirectory); normalize_path(normalizedSubDir); size_t subDirLen = strlen(normalizedSubDir); - if (subDirLen > 0 && normalizedSubDir[subDirLen - 1] != '/') { + if (subDirLen > 0 && subDirLen + 1 < SYS_MAX_PATH && normalizedSubDir[subDirLen - 1] != '/') { strcat(normalizedSubDir, "/"); subDirLen = strlen(normalizedSubDir); } @@ -622,21 +631,21 @@ LuaTable get_mod_files(struct Mod* mod, const char* subDirectory) { int luaTableIndex = 1; for (int i = 0; i < mod->fileCount; i++) { struct ModFile* file = &mod->files[i]; - char* normalizedPath = strdup(file->relativePath); + char normalizedPath[SYS_MAX_PATH] = { 0 }; + if (snprintf(normalizedPath, SYS_MAX_PATH, "%s", file->relativePath) < 0) { + LOG_ERROR("Failed to copy relativePath for normalization: %s", file->relativePath); + continue; + } normalize_path(normalizedPath); if (strncmp(normalizedPath, normalizedSubDir, subDirLen) == 0) { lua_pushstring(L, file->relativePath); lua_rawseti(L, -2, luaTableIndex++); } - - free(normalizedPath); } LUA_STACK_CHECK_END(L); - free(normalizedSubDir); - return smlua_to_lua_table(L, -1); }