Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cmd_averagestats has UB #5000

Closed
Bassoonian opened this issue Jul 19, 2024 · 5 comments · Fixed by #5191
Closed

Cmd_averagestats has UB #5000

Bassoonian opened this issue Jul 19, 2024 · 5 comments · Fixed by #5191
Labels
bug Bug category: battle-mechanic Pertains to battle mechanics status: unconfirmed This bug has not been reproduced yet

Comments

@Bassoonian
Copy link
Collaborator

Description

https://discord.com/channels/419213663107416084/774393519569502268/1263628135417774081

Version

master (default, unreleased bugfixes)

Upcoming/master Version

No response

Discord contact info

No response

@Bassoonian Bassoonian added bug Bug category: battle-mechanic Pertains to battle mechanics status: unconfirmed This bug has not been reproduced yet labels Jul 19, 2024
@u8-Salem
Copy link

u8-Salem commented Jul 19, 2024

luckytyphlosion pointed out their solution would not be compatible with agbcc. I suggest we wait with this one until it is fully deprecated and removed (#4994)

@AlexOn1ine
Copy link
Collaborator

If there is a solution it wouldn't be bad to fix on 1.8

@u8-Salem
Copy link

If there is a solution it wouldn't be bad to fix on 1.8

for which release is agbcc's deprecation sheduled? we should do this fix in the same release

@AlexOn1ine
Copy link
Collaborator

If there is a solution it wouldn't be bad to fix on 1.8

for which release is agbcc's deprecation sheduled? we should do this fix in the same release

it is being disabled in 1.9 but deprecated in 1.10. Though it would be a bugfix for 1.8.6 since this one is released at the same time 1.9 is

@mrgriffin
Copy link
Collaborator

mrgriffin commented Jul 19, 2024

A simple fix would be to write something like:

u16 *GetBattlerStat(struct BattlePokemon *battler, u32 stat)
{
  switch (stat)
  {
  case STAT_ATTACK: return &battler->attack;
  case STAT_DEFENSE: return &battler->defense;
  ...
  default: return NULL;
  }
}

static void Cmd_averagestats(void)
{
  CMD_ARGS(u8 stat);

  u16 *stat1 = GetBattlerStat(&gBattleMons[gBattlerAttacker]), cmd->stat);
  u16 *stat2 = GetBattlerStat(&gBattleMons[gBattlerTarget]), cmd->stat);
  u16 avg = (*stat1 + *stat2) / 2;
  *stat1 = *stat2 = avg;

  gBattlescriptCurrInstr = cmd->nextInstr;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug category: battle-mechanic Pertains to battle mechanics status: unconfirmed This bug has not been reproduced yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants