From 42cf0e3eea0547a3a56fce7b7b4db8bd21f7a984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Krzy=C5=9Bk=C3=B3w?= <46760021+Flower35@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:45:21 +0100 Subject: [PATCH] Fixed smlua_model_util_get_id buffer overflow (#404) - added the missing limit check for `sCustomModelsCount` (caused memory overflow into `bbh_seg7_vertex_`) - changed `LOG_ERROR` to `LOG_LUA_LINE`, so that the mod dev can easily see any missing models (similar to getting warnings about missing audio files with `audio_sample_load`) --- src/pc/lua/utils/smlua_model_utils.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/pc/lua/utils/smlua_model_utils.c b/src/pc/lua/utils/smlua_model_utils.c index c7fbaa25..e8ba5b64 100644 --- a/src/pc/lua/utils/smlua_model_utils.c +++ b/src/pc/lua/utils/smlua_model_utils.c @@ -53,7 +53,7 @@ #include "levels/ttm/header.h" #include "smlua_model_utils.h" -#include "pc/debuglog.h" +#include "pc/lua/smlua.h" struct ModelUtilsInfo { enum ModelExtendedId extId; @@ -463,8 +463,8 @@ struct ModelUtilsInfo sModels[E_MODEL_MAX] = { }; #define MAX_CUSTOM_MODELS 256 -struct ModelUtilsInfo sCustomModels[MAX_CUSTOM_MODELS] = { 0 }; static u16 sCustomModelsCount = 0; +struct ModelUtilsInfo sCustomModels[MAX_CUSTOM_MODELS] = { 0 }; void smlua_model_util_clear(void) { sCustomModelsCount = 0; @@ -497,7 +497,7 @@ enum ModelExtendedId smlua_model_util_get_id(const char* name) { // find geolayout const void* asset = dynos_geolayout_get(name); if (asset == NULL) { - LOG_ERROR("Failed to find model: %s - %u", name, E_MODEL_ERROR_MODEL); + LOG_LUA_LINE("Could not find model: '%s'", name); return E_MODEL_ERROR_MODEL; } @@ -517,6 +517,11 @@ enum ModelExtendedId smlua_model_util_get_id(const char* name) { } } + if (sCustomModelsCount >= MAX_CUSTOM_MODELS) { + LOG_LUA("Failed to get model: '%s' (too many custom models!)", name); + return E_MODEL_ERROR_MODEL; + } + // allocate custom model u16 customIndex = sCustomModelsCount++; struct ModelUtilsInfo* info = &sCustomModels[customIndex];