diff --git a/src/game/behaviors/texscroll.inc.c b/src/game/behaviors/texscroll.inc.c index c2fc62bba..6a1256fdc 100644 --- a/src/game/behaviors/texscroll.inc.c +++ b/src/game/behaviors/texscroll.inc.c @@ -70,13 +70,9 @@ static inline void shift_UV_NORMAL(struct ScrollTarget *scroll, u16 vertcount, s verts[0]->n.flag++; } else { if (bhv < SCROLL_UV_X) { - for (i = 0; i < vertcount; i++) { - scroll->prevF32[i] = scroll->interpF32[i]; - } + memcpy(scroll->prevF32, scroll->interpF32, vertcount * sizeof(f32)); } else { - for (i = 0; i < vertcount; i++) { - scroll->prevS16[i] = scroll->interpS16[i]; - } + memcpy(scroll->prevS16, scroll->interpS16, vertcount * sizeof(s16)); } } } @@ -135,6 +131,12 @@ void uv_update_scroll(void) { if (bhv < SCROLL_UV_X) { scroll->interpF32 = malloc(scroll->size * sizeof(f32)); scroll->prevF32 = malloc(scroll->size * sizeof(f32)); + if (!scroll->interpF32 || !scroll->prevF32) { + free(scroll->interpF32); + free(scroll->prevF32); + scroll->interpF32 = scroll->prevF32 = NULL; + return; + } u8 bhvIndex = MIN(bhv, 2); for (u32 k = 0; k < scroll->size; k++) { @@ -143,6 +145,12 @@ void uv_update_scroll(void) { } else { scroll->interpS16 = malloc(scroll->size * sizeof(s16)); scroll->prevS16 = malloc(scroll->size * sizeof(s16)); + if (!scroll->interpS16 || !scroll->prevS16) { + free(scroll->interpS16); + free(scroll->prevS16); + scroll->interpS16 = scroll->prevS16 = NULL; + return; + } u8 bhvIndex = MIN(bhv-SCROLL_UV_X, 1); for (u32 k = 0; k < scroll->size; k++) { diff --git a/src/game/scroll_targets.c b/src/game/scroll_targets.c index 13d06cefc..a2af8bc19 100644 --- a/src/game/scroll_targets.c +++ b/src/game/scroll_targets.c @@ -13,19 +13,16 @@ struct ScrollTarget *get_scroll_targets(u32 id, u16 size, u16 offset) { if (scroll) { // If we need to, realloc the block of vertices - if ((!scroll->hasOffset && offset > 0) || size < scroll->size) { - if (scroll->hasOffset) { return NULL; } + if (!scroll->hasOffset && (offset > 0 || size < scroll->size)) { if (size > scroll->size) { size = scroll->size; } // Don't use an invalid size - if (size + offset >= scroll->size) { return NULL; } // If the offset is invalid, Abort. - scroll->hasOffset = true; + if (offset > 0 && size + offset >= scroll->size) { return NULL; } // If the offset is invalid, Abort. Vtx* *newVtx = malloc(size * sizeof(Vtx*)); if (!newVtx) { return NULL; } - for (u32 i = 0; i < size; i++) { - newVtx[i] = scroll->vertices[i + offset]; - } + memcpy(newVtx, scroll->vertices + offset, size * sizeof(Vtx*)); free(scroll->vertices); scroll->vertices = newVtx; scroll->size = size; + scroll->hasOffset = true; } return scroll; @@ -86,8 +83,9 @@ void add_vtx_scroll_target(u32 id, Vtx *vtx, u32 size, bool hasOffset) { if (!newArray) { newArray = malloc(newSize); if (!newArray) { return; } - memcpy(newArray, scroll->vertices, oldSize); - memset(newArray + scroll->size, 0, size * sizeof(Vtx*)); + if (scroll->vertices && oldSize > 0) { + memcpy(newArray, scroll->vertices, oldSize); + } free(scroll->vertices); } diff --git a/src/pc/lua/smlua_functions.c b/src/pc/lua/smlua_functions.c index 4054a3ebd..ba79220a8 100644 --- a/src/pc/lua/smlua_functions.c +++ b/src/pc/lua/smlua_functions.c @@ -815,6 +815,11 @@ int smlua_func_log_to_console(lua_State* L) { //////////////////// int smlua_func_add_scroll_target(lua_State* L) { + if (gLuaLoadingMod == NULL) { + LOG_LUA_LINE("add_scroll_target() can only be called on load."); + return 0; + } + // add_scroll_target used to require offset and size of the vertex buffer to be used if (!smlua_functions_valid_param_range(L, 2, 4)) { return 0; } int paramCount = lua_gettop(L);