Skip to content

Commit

Permalink
Fix UB when accessing move result flags (#6030)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex <[email protected]>
  • Loading branch information
cawtds and AlexOn1ine authored Jan 15, 2025
1 parent ed0c9a4 commit 75d5eda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/battle_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ uq4_12_t CalcTypeEffectivenessMultiplier(u32 move, u32 moveType, u32 battlerAtk,
uq4_12_t CalcPartyMonTypeEffectivenessMultiplier(u16 move, u16 speciesDef, u16 abilityDef);
uq4_12_t GetTypeModifier(u32 atkType, u32 defType);
uq4_12_t GetOverworldTypeEffectiveness(struct Pokemon *mon, u8 moveType);
void UpdateMoveResultFlags(uq4_12_t modifier, u16 *resultFlags);
s32 GetStealthHazardDamage(enum TypeSideHazard hazardType, u32 battler);
s32 GetStealthHazardDamageByTypesAndHP(enum TypeSideHazard hazardType, u8 type1, u8 type2, u32 maxHp);
bool32 CanMegaEvolve(u32 battler);
Expand Down
7 changes: 5 additions & 2 deletions src/battle_ai_switch_items.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ static bool32 FindMonWithFlagsAndSuperEffective(u32 battler, u16 flags, u32 perc
for (i = firstId; i < lastId; i++)
{
u16 species, monAbility;
uq4_12_t typeMultiplier;
u16 moveFlags = 0;

if (!IsValidForBattle(&party[i]))
continue;
Expand All @@ -743,8 +745,9 @@ static bool32 FindMonWithFlagsAndSuperEffective(u32 battler, u16 flags, u32 perc

species = GetMonData(&party[i], MON_DATA_SPECIES_OR_EGG);
monAbility = GetMonAbility(&party[i]);
CalcPartyMonTypeEffectivenessMultiplier(gLastLandedMoves[battler], species, monAbility);
if (gBattleStruct->moveResultFlags[BATTLE_OPPOSITE(battler)] & flags)
typeMultiplier = CalcPartyMonTypeEffectivenessMultiplier(gLastLandedMoves[battler], species, monAbility);
UpdateMoveResultFlags(typeMultiplier, &moveFlags);
if (moveFlags & flags)
{
battlerIn1 = gLastHitBy[battler];

Expand Down
19 changes: 9 additions & 10 deletions src/battle_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -10654,27 +10654,27 @@ static inline void TryNoticeIllusionInTypeEffectiveness(u32 move, u32 moveType,
RecordAbilityBattle(battlerDef, ABILITY_ILLUSION);
}

static void UpdateMoveResultFlags(uq4_12_t modifier, u32 battler)
void UpdateMoveResultFlags(uq4_12_t modifier, u16 *resultFlags)
{
if (modifier == UQ_4_12(0.0))
{
gBattleStruct->moveResultFlags[battler] |= MOVE_RESULT_DOESNT_AFFECT_FOE;
gBattleStruct->moveResultFlags[battler] &= ~(MOVE_RESULT_NOT_VERY_EFFECTIVE | MOVE_RESULT_SUPER_EFFECTIVE);
*resultFlags |= MOVE_RESULT_DOESNT_AFFECT_FOE;
*resultFlags &= ~(MOVE_RESULT_NOT_VERY_EFFECTIVE | MOVE_RESULT_SUPER_EFFECTIVE);
gBattleStruct->blunderPolicy = FALSE; // Don't activate if missed
}
else if (modifier == UQ_4_12(1.0))
{
gBattleStruct->moveResultFlags[battler] &= ~(MOVE_RESULT_NOT_VERY_EFFECTIVE | MOVE_RESULT_SUPER_EFFECTIVE | MOVE_RESULT_DOESNT_AFFECT_FOE);
*resultFlags &= ~(MOVE_RESULT_NOT_VERY_EFFECTIVE | MOVE_RESULT_SUPER_EFFECTIVE | MOVE_RESULT_DOESNT_AFFECT_FOE);
}
else if (modifier > UQ_4_12(1.0))
{
gBattleStruct->moveResultFlags[battler] |= MOVE_RESULT_SUPER_EFFECTIVE;
gBattleStruct->moveResultFlags[battler] &= ~(MOVE_RESULT_NOT_VERY_EFFECTIVE | MOVE_RESULT_DOESNT_AFFECT_FOE);
*resultFlags |= MOVE_RESULT_SUPER_EFFECTIVE;
*resultFlags &= ~(MOVE_RESULT_NOT_VERY_EFFECTIVE | MOVE_RESULT_DOESNT_AFFECT_FOE);
}
else //if (modifier < UQ_4_12(1.0))
{
gBattleStruct->moveResultFlags[battler] |= MOVE_RESULT_NOT_VERY_EFFECTIVE;
gBattleStruct->moveResultFlags[battler] &= ~(MOVE_RESULT_SUPER_EFFECTIVE | MOVE_RESULT_DOESNT_AFFECT_FOE);
*resultFlags |= MOVE_RESULT_NOT_VERY_EFFECTIVE;
*resultFlags &= ~(MOVE_RESULT_SUPER_EFFECTIVE | MOVE_RESULT_DOESNT_AFFECT_FOE);
}
}

Expand Down Expand Up @@ -10760,7 +10760,7 @@ uq4_12_t CalcTypeEffectivenessMultiplier(u32 move, u32 moveType, u32 battlerAtk,
}

if (recordAbilities)
UpdateMoveResultFlags(modifier, battlerDef);
UpdateMoveResultFlags(modifier, &gBattleStruct->moveResultFlags[battlerDef]);
return modifier;
}

Expand All @@ -10781,7 +10781,6 @@ uq4_12_t CalcPartyMonTypeEffectivenessMultiplier(u16 move, u16 speciesDef, u16 a
modifier = UQ_4_12(0.0);
}

UpdateMoveResultFlags(modifier, speciesDef);
return modifier;
}

Expand Down

0 comments on commit 75d5eda

Please sign in to comment.