Merge pull request #1280 from Sierraffinity/master

Add fixes for undefined behavior with modern compiler
This commit is contained in:
GriffinR 2020-12-26 22:08:48 -05:00 committed by GitHub
commit e1fd2f2a89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 4 deletions

View File

@ -2275,7 +2275,18 @@ u8 CameraObjectGetFollowedObjectId(void)
void CameraObjectReset2(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; FindCameraObject()->data[1] = 2;
#endif // UBFIX
} }
u8 CopySprite(struct Sprite *sprite, s16 x, s16 y, u8 subpriority) u8 CopySprite(struct Sprite *sprite, s16 x, s16 y, u8 subpriority)

View File

@ -797,15 +797,33 @@ bool8 CameraMove(int x, int y)
struct MapConnection *sub_8088950(u8 direction, int x, int y) struct MapConnection *sub_8088950(u8 direction, int x, int y)
{ {
int count; int count;
struct MapConnection *connection;
int i; int i;
count = gMapHeader.connections->count; struct MapConnection *connection;
connection = gMapHeader.connections->connections; 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++) for (i = 0; i < count; i++, connection++)
{ {
if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE) if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE)
return connection; return connection;
} }
#endif
return NULL; return NULL;
} }

View File

@ -3037,7 +3037,15 @@ static void SpriteCB_PokedexListMonSprite(struct Sprite *sprite)
u32 var; u32 var;
sprite->pos2.y = gSineTable[(u8)sprite->data[5]] * 76 / 256; 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]; var = 0x10000 / gSineTable[sprite->data[5] + 64];
#endif //UBFIX
if (var > 0xFFFF) if (var > 0xFFFF)
var = 0xFFFF; var = 0xFFFF;
SetOamMatrix(sprite->data[1] + 1, 0x100, 0, 0, var); SetOamMatrix(sprite->data[1] + 1, 0x100, 0, 0, var);

View File

@ -8408,8 +8408,13 @@ static void sub_80D08CC(void)
for (j = sMoveMonsPtr->minRow; j < rowCount; j++) for (j = sMoveMonsPtr->minRow; j < rowCount; j++)
{ {
struct BoxPokemon *boxMon = GetBoxedMonPtr(boxId, boxPosition); struct BoxPokemon *boxMon = GetBoxedMonPtr(boxId, boxPosition);
// UB: possible null dereference
#ifdef UBFIX
if (boxMon != NULL)
sMoveMonsPtr->boxMons[monArrayId] = *boxMon;
#else
sMoveMonsPtr->boxMons[monArrayId] = *boxMon; sMoveMonsPtr->boxMons[monArrayId] = *boxMon;
#endif
monArrayId++; monArrayId++;
boxPosition++; boxPosition++;
} }