port UBFIXes from emerald

This commit is contained in:
Kurausukun
2021-06-26 04:04:22 -04:00
parent 8116790c08
commit 88bd618496
8 changed files with 65 additions and 7 deletions
+3
View File
@@ -41,6 +41,9 @@
#ifndef BUGFIX
#define BUGFIX
#endif // BUGFIX
#ifndef UBFIX
#define UBFIX
#endif // UBFIX
#endif // MODERN
#endif // GUARD_CONFIG_H
+5
View File
@@ -1362,7 +1362,12 @@ static void Cmd_if_status_not_in_party(void)
// everytime the status is found, the AI's logic jumps further and further past its intended destination. this results in a broken AI macro and is probably why it is unused.
if (species != SPECIES_NONE && species != SPECIES_EGG && hp != 0 && status == statusToCompareTo)
{
sAIScriptPtr += 10; // doesnt return?
#ifdef UBFIX
return;
#endif
}
}
sAIScriptPtr = T1_READ_PTR(sAIScriptPtr + 6);
}
+5
View File
@@ -459,12 +459,17 @@ void InitEasyChatPhrases(void)
gSaveBlock1Ptr->mail[i].words[j] = EC_WORD_UNDEFINED;
}
#ifndef UBFIX
// BUG: This is supposed to clear 64 bits, but this loop is clearing 64 bytes.
// However, this bug has no resulting effect on gameplay because only the
// Mauville old man data is corrupted, which is initialized directly after
// this function is called when starting a new game.
for (i = 0; i < 64; i++)
gSaveBlock1Ptr->additionalPhrases[i] = 0;
#else
for (i = 0; i < NELEMS(gSaveBlock1Ptr->additionalPhrases); i++)
gSaveBlock1Ptr->additionalPhrases[i] = 0;
#endif
}
void EC_ResetMEventProfileMaybe(void)
+7 -1
View File
@@ -2561,7 +2561,13 @@ const u8 *GetObjectEventScriptPointerByObjectEventId(u8 objectEventId)
static u16 GetObjectEventFlagIdByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup)
{
return GetObjectEventTemplateByLocalIdAndMap(localId, mapNum, mapGroup)->flagId;
struct ObjectEventTemplate *obj = GetObjectEventTemplateByLocalIdAndMap(localId, mapNum, mapGroup);
#ifdef UBFIX
// BUG: The function may return NULL, and attempting to read from NULL may freeze the game using modern compilers.
if (obj == NULL)
return 0;
#endif // UBFIX
return obj->flagId;
}
static u16 GetObjectEventFlagIdByObjectEventId(u8 objectEventId)
+10 -2
View File
@@ -3666,8 +3666,16 @@ static void Task_MoveDeoxysRock_Step(u8 taskId)
case 0:
data[4] = sprite->pos1.x << 4;
data[5] = sprite->pos1.y << 4;
data[6] = ((data[2] << 4) - data[4]) / data[8];
data[7] = ((data[3] << 4) - data[5]) / data[8];
// UB: Possible divide by zero
#ifdef UBFIX
#define DIVISOR (data[8] ? data[8] : 1);
#else
#define DIVISOR (data[8])
#endif
data[6] = ((data[2] << 4) - data[4]) / DIVISOR;
data[7] = ((data[3] << 4) - data[5]) / DIVISOR;
data[0]++;
// fallthrough
case 1:
+25 -3
View File
@@ -528,10 +528,14 @@ static bool32 SavedMapViewIsEmpty(void)
u16 i;
u32 marker = 0;
#ifndef UBFIX
// BUG: This loop extends past the bounds of the mapView array. Its size is only 0x100.
for (i = 0; i < 0x200; i++)
marker |= gSaveBlock2Ptr->mapView[i];
#else
for (i = 0; i < NELEMS(gSaveBlock2Ptr->mapView); i++)
marker |= gSaveBlock2Ptr->mapView[i];
#endif
if (marker == 0)
return TRUE;
else
@@ -746,14 +750,32 @@ struct MapConnection *sub_8059600(u8 direction, s32 x, s32 y)
{
s32 count;
struct MapConnection *connection;
const struct MapConnections *connections = gMapHeader.connections;
s32 i;
count = gMapHeader.connections->count;
connection = gMapHeader.connections->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_8059658(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_8059658(direction, x, y, connection) == TRUE)
return connection;
}
#endif
return NULL;
}
+6 -1
View File
@@ -473,8 +473,13 @@ static void sub_80957C8(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++;
}
+4
View File
@@ -1628,7 +1628,11 @@ static bool32 IsPartnerActivityAcceptable(u32 activity, u32 group)
if (group == 0xFF)
return TRUE;
#ifndef UBFIX
if (group <= NELEMS(sAcceptedActivityIds)) // UB: <= may access data outside the array
#else
if (group < NELEMS(sAcceptedActivityIds))
#endif
{
const u8 *bytes = sAcceptedActivityIds[group];