diff --git a/src/event_object_movement.c b/src/event_object_movement.c index 1d06ddc84d..12ef4acdc0 100644 --- a/src/event_object_movement.c +++ b/src/event_object_movement.c @@ -2275,7 +2275,18 @@ u8 CameraObjectGetFollowedObjectId(void) void CameraObjectReset2(void) { + // UB: Possible null dereference +#ifdef UBFIX + struct Sprite *cameraObject; + + cameraObject = FindCameraObject(); + if (cameraObject != NULL) + { + cameraObject->data[1] = 2; + } +#else FindCameraObject()->data[1] = 2; +#endif // UBFIX } u8 CopySprite(struct Sprite *sprite, s16 x, s16 y, u8 subpriority) diff --git a/src/fieldmap.c b/src/fieldmap.c index 49337ebbec..296c4edf25 100644 --- a/src/fieldmap.c +++ b/src/fieldmap.c @@ -797,15 +797,33 @@ bool8 CameraMove(int x, int y) struct MapConnection *sub_8088950(u8 direction, int x, int y) { int count; - struct MapConnection *connection; int i; - count = gMapHeader.connections->count; - connection = gMapHeader.connections->connections; + struct MapConnection *connection; + const struct MapConnections *connections = gMapHeader.connections; + // UB: Multiple possible null dereferences +#ifdef UBFIX + if (connections != NULL) + { + count = connections->count; + connection = connections->connections; + if (connection != NULL) + { + for (i = 0; i < count; i++, connection++) + { + if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE) + return connection; + } + } + } +#else + count = connections->count; + connection = connections->connections; for (i = 0; i < count; i++, connection++) { if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE) return connection; } +#endif return NULL; } diff --git a/src/pokedex.c b/src/pokedex.c index 516cef6a72..691abd649f 100644 --- a/src/pokedex.c +++ b/src/pokedex.c @@ -3037,7 +3037,15 @@ static void SpriteCB_PokedexListMonSprite(struct Sprite *sprite) u32 var; sprite->pos2.y = gSineTable[(u8)sprite->data[5]] * 76 / 256; + // UB: possible division by zero +#ifdef UBFIX + if (gSineTable[sprite->data[5] + 64] != 0) + var = 0x10000 / gSineTable[sprite->data[5] + 64]; + else + var = 0xFFFF; +#else var = 0x10000 / gSineTable[sprite->data[5] + 64]; +#endif //UBFIX if (var > 0xFFFF) var = 0xFFFF; SetOamMatrix(sprite->data[1] + 1, 0x100, 0, 0, var); diff --git a/src/pokemon_storage_system.c b/src/pokemon_storage_system.c index db8f3d2bb4..4be1cf729f 100644 --- a/src/pokemon_storage_system.c +++ b/src/pokemon_storage_system.c @@ -8408,8 +8408,13 @@ static void sub_80D08CC(void) for (j = sMoveMonsPtr->minRow; j < rowCount; j++) { struct BoxPokemon *boxMon = GetBoxedMonPtr(boxId, boxPosition); - + // UB: possible null dereference +#ifdef UBFIX + if (boxMon != NULL) + sMoveMonsPtr->boxMons[monArrayId] = *boxMon; +#else sMoveMonsPtr->boxMons[monArrayId] = *boxMon; +#endif monArrayId++; boxPosition++; }