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

Fix Trainer Hill OOB array access #5930

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/trainer_hill.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ static const struct TrainerHillChallenge *const sChallengeData[NUM_TRAINER_HILL_
[HILL_MODE_EXPERT] = &sChallenge_Expert,
};

static const struct TrainerHillFloor *const sFloorData[NUM_TRAINER_HILL_MODES] =
{
[HILL_MODE_NORMAL] = &sFloors_Normal[0],
[HILL_MODE_VARIETY] = &sFloors_Variety[0],
[HILL_MODE_UNIQUE] = &sFloors_Unique[0],
[HILL_MODE_EXPERT] = &sFloors_Expert[0],
};

// Unused.
static const u8 *const sFloorStrings[] =
{
Expand Down Expand Up @@ -357,20 +365,14 @@ void FreeTrainerHillBattleStruct(void)
static void SetUpDataStruct(void)
{
#if FREE_TRAINER_HILL == FALSE
if (sHillData == NULL)
{
sHillData = AllocZeroed(sizeof(*sHillData));
sHillData->floorId = gMapHeader.mapLayoutId - LAYOUT_TRAINER_HILL_1F;

// This copy depends on the floor data for each challenge being directly after the
// challenge header data, and for the field 'floors' in sHillData to come directly
// after the field 'challenge'.
// e.g. for HILL_MODE_NORMAL, it will copy sChallenge_Normal to sHillData->challenge and
// it will copy sFloors_Normal to sHillData->floors
CpuCopy32(sChallengeData[gSaveBlock1Ptr->trainerHill.mode], &sHillData->challenge, sizeof(sHillData->challenge) + sizeof(sHillData->floors));
TrainerHillDummy();
}
#endif //FREE_TRAINER_HILL
if (sHillData != NULL) return;

sHillData = AllocZeroed(sizeof(*sHillData));
sHillData->floorId = gMapHeader.mapLayoutId - LAYOUT_TRAINER_HILL_1F;

CpuCopy32(sChallengeData[gSaveBlock1Ptr->trainerHill.mode], &sHillData->challenge, sizeof(sHillData->challenge));
SBird1337 marked this conversation as resolved.
Show resolved Hide resolved
CpuCopy32(sFloorData[gSaveBlock1Ptr->trainerHill.mode], &sHillData->floors, sizeof(sHillData->floors));
#endif // FREE_TRAINER_HILL
}

static void FreeDataStruct(void)
Expand Down
Loading