From 67a656a4df326ebfcbca48c76efdc4fc39554093 Mon Sep 17 00:00:00 2001 From: Sierraffinity Date: Mon, 4 Jan 2021 16:26:28 -0800 Subject: [PATCH] Fix/document possible division by zero in ConvertScaleParam --- gflib/sprite.c | 12 +++++++++++- src/pokedex.c | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gflib/sprite.c b/gflib/sprite.c index c7e3d09a32..e36c53721c 100644 --- a/gflib/sprite.c +++ b/gflib/sprite.c @@ -1319,8 +1319,18 @@ void ApplyAffineAnimFrameRelativeAndUpdateMatrix(u8 matrixNum, struct AffineAnim s16 ConvertScaleParam(s16 scale) { + s16 ret; s32 val = 0x10000; - return val / scale; + // UB: possible division by zero +#ifdef UBFIX + if (scale != 0) + ret = val / scale; + else + ret = 0; +#else + ret = val / scale; +#endif //UBFIX + return ret; } void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd) diff --git a/src/pokedex.c b/src/pokedex.c index 691abd649f..6aa3479940 100644 --- a/src/pokedex.c +++ b/src/pokedex.c @@ -3042,7 +3042,7 @@ static void SpriteCB_PokedexListMonSprite(struct Sprite *sprite) if (gSineTable[sprite->data[5] + 64] != 0) var = 0x10000 / gSineTable[sprite->data[5] + 64]; else - var = 0xFFFF; + var = 0; #else var = 0x10000 / gSineTable[sprite->data[5] + 64]; #endif //UBFIX