fix scrolling texture interp integer overflow (#72)

This commit is contained in:
Isaac0-dev 2024-06-22 11:06:29 +10:00 committed by GitHub
parent b959ba90d9
commit 570afe946e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 5 deletions

View file

@ -154,13 +154,13 @@ void uv_update_scroll(void) {
// Prepare for interpolation // Prepare for interpolation
if (bhv < SCROLL_UV_X) { if (bhv < SCROLL_UV_X) {
u8 bhvIndex = MIN(bhv, 2); u8 bhvIndex = MIN(bhv, 2);
for (u16 k = 0; k < scroll->size; k++) { for (u16 i = 0; i < scroll->size; i++) {
scroll->prevF32[k] = verts[k]->n.ob[bhvIndex]; scroll->prevF32[i] = verts[i]->n.ob[bhvIndex];
} }
} else { } else {
u8 bhvIndex = MIN(bhv-SCROLL_UV_X, 1); u8 bhvIndex = MIN(bhv-SCROLL_UV_X, 1);
for (u16 k = 0; k < scroll->size; k++) { for (u16 i = 0; i < scroll->size; i++) {
scroll->prevS16[k] = verts[k]->n.tc[bhvIndex]; scroll->prevS16[i] = verts[i]->n.tc[bhvIndex];
} }
} }
scroll->needInterp = true; scroll->needInterp = true;

View file

@ -1,4 +1,5 @@
#include "scroll_targets.h" #include "scroll_targets.h"
#include "pc/lua/utils/smlua_math_utils.h"
static struct ScrollTarget *sScrollTargets = NULL; static struct ScrollTarget *sScrollTargets = NULL;
@ -21,6 +22,7 @@ struct ScrollTarget *get_scroll_targets(u32 id, u16 size, u16 offset) {
// If we need to, realloc the block of vertices // If we need to, realloc the block of vertices
if ((!scroll->hasOffset && offset > 0) || size < scroll->size) { if ((!scroll->hasOffset && offset > 0) || size < scroll->size) {
if (scroll->hasOffset) { return NULL; }
if (size > scroll->size) { size = scroll->size; } // Don't use an invalid size if (size > scroll->size) { size = scroll->size; } // Don't use an invalid size
scroll->hasOffset = true; scroll->hasOffset = true;
Vtx* *newVtx = calloc(size, sizeof(Vtx*)); Vtx* *newVtx = calloc(size, sizeof(Vtx*));
@ -137,6 +139,8 @@ void patch_scroll_targets_before(void) {
} }
} }
#define SHORT_RANGE 32767
void patch_scroll_targets_interpolated(f32 delta) { void patch_scroll_targets_interpolated(f32 delta) {
f32 antiDelta = 1.0f - delta; f32 antiDelta = 1.0f - delta;
struct ScrollTarget *scroll = sScrollTargets; struct ScrollTarget *scroll = sScrollTargets;
@ -152,7 +156,7 @@ void patch_scroll_targets_interpolated(f32 delta) {
} else { } else {
u8 bhvIndex = MIN(scroll->bhv-SCROLL_UV_X, 1); u8 bhvIndex = MIN(scroll->bhv-SCROLL_UV_X, 1);
for (u16 k = 0; k < scroll->size; k++) { for (u16 k = 0; k < scroll->size; k++) {
verts[k]->n.tc[bhvIndex] = (int) scroll->prevS16[k] * antiDelta + scroll->interpS16[k] * delta; verts[k]->n.tc[bhvIndex] = clampf(scroll->prevS16[k] * antiDelta + scroll->interpS16[k] * delta, -SHORT_RANGE, SHORT_RANGE);
} }
} }
} }