From 11178a49db67304bb82287e4c93ef4a36cf5edca Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Mon, 27 Jul 2020 12:27:35 +0200 Subject: [PATCH] Fix undefined behaviour in protect animation gPlttBufferFaded[sprite->data[2] + ++i] = gPlttBufferFaded[sprite->data[2] + i + 1] The above does NOT make it certain that i gets incremented before reading from the right side. Changed it, so the function always increments i first, then reads from pal buffer. --- src/battle_anim_effects_1.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/battle_anim_effects_1.c b/src/battle_anim_effects_1.c index 516c58e55..f4769145d 100644 --- a/src/battle_anim_effects_1.c +++ b/src/battle_anim_effects_1.c @@ -3967,24 +3967,25 @@ static void AnimProtect(struct Sprite* sprite) static void AnimProtect_Step(struct Sprite *sprite) { - int a; - int i; + int i, savedPal; sprite->data[5] += 96; sprite->pos2.x = -(sprite->data[5] >> 8); if (++sprite->data[1] > 1) { + int id; sprite->data[1] = 0; - a = gPlttBufferFaded[sprite->data[2] + 1]; + savedPal = gPlttBufferFaded[sprite->data[2] + 1]; i = 0; do { - gPlttBufferFaded[sprite->data[2] + ++i] = gPlttBufferFaded[sprite->data[2] + i + 1]; + id = sprite->data[2] + ++i; + gPlttBufferFaded[id] = gPlttBufferFaded[id + 1]; } while (i < 6); - gPlttBufferFaded[sprite->data[2] + 7] = a; + gPlttBufferFaded[sprite->data[2] + 7] = savedPal; } - if (sprite->data[7] > 6 && sprite->data[0] >0 && ++sprite->data[6] > 1) + if (sprite->data[7] > 6 && sprite->data[0] > 0 && ++sprite->data[6] > 1) { sprite->data[6] = 0; sprite->data[7] -= 1;