Skip to content

Commit

Permalink
Fixed null dereferencing errors with -fanalyzer on modern (#3165)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsparagusEduardo authored Jul 31, 2023
2 parents f0d03dd + 5a6532d commit 5481681
Show file tree
Hide file tree
Showing 6 changed files with 586 additions and 561 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ LIBPATH := -L ../../tools/agbcc/lib
LIB := $(LIBPATH) -lgcc -lc -L../../libagbsyscall -lagbsyscall
else
CC1 = $(shell $(PATH_MODERNCC) --print-prog-name=cc1) -quiet
override CFLAGS += -mthumb -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -fno-toplevel-reorder -Wno-pointer-to-int-cast
override CFLAGS += -mthumb -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -fno-toplevel-reorder -Wno-pointer-to-int-cast -std=gnu17 -fanalyzer
ROM := $(MODERN_ROM_NAME)
OBJ_DIR := $(MODERN_OBJ_DIR_NAME)
LIBPATH := -L "$(dir $(shell $(PATH_MODERNCC) -mthumb -print-file-name=libgcc.a))" -L "$(dir $(shell $(PATH_MODERNCC) -mthumb -print-file-name=libnosys.a))" -L "$(dir $(shell $(PATH_MODERNCC) -mthumb -print-file-name=libc.a))"
Expand Down
2 changes: 1 addition & 1 deletion include/overworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void LoadObjEventTemplatesFromHeader(void);
void LoadSaveblockObjEventScripts(void);
void SetObjEventTemplateCoords(u8 localId, s16 x, s16 y);
void SetObjEventTemplateMovementType(u8 localId, u8 movementType);
const struct MapLayout *GetMapLayout(void);
const struct MapLayout *GetMapLayout(u16 mapLayoutId);
void ApplyCurrentWarp(void);
struct MapHeader const *const Overworld_GetMapHeaderByGroupAndId(u16 mapGroup, u16 mapNum);
struct MapHeader const *const GetDestinationWarpMapHeader(void);
Expand Down
7 changes: 5 additions & 2 deletions src/battle_gfx_sfx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,11 @@ void AllocateMonSpritesGfx(void)

for (j = 0; j < 4; j++)
{
gMonSpritesGfxPtr->frameImages[i][j].data = gMonSpritesGfxPtr->sprites.ptr[i] + (j * MON_PIC_SIZE);
gMonSpritesGfxPtr->frameImages[i][j].size = MON_PIC_SIZE;
if (gMonSpritesGfxPtr->sprites.ptr[i])
{
gMonSpritesGfxPtr->frameImages[i][j].data = gMonSpritesGfxPtr->sprites.ptr[i] + (j * MON_PIC_SIZE);
gMonSpritesGfxPtr->frameImages[i][j].size = MON_PIC_SIZE;
}
}

gMonSpritesGfxPtr->templates[i].images = gMonSpritesGfxPtr->frameImages[i];
Expand Down
31 changes: 21 additions & 10 deletions src/fieldmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ bool32 CanCameraMoveInDirection(int direction)

static void SetPositionFromConnection(const struct MapConnection *connection, int direction, int x, int y)
{
struct MapHeader const *mapHeader;
mapHeader = GetMapHeaderFromConnection(connection);
struct MapHeader const *mapHeader = GetMapHeaderFromConnection(connection);

switch (direction)
{
case CONNECTION_EAST:
Expand All @@ -641,6 +641,9 @@ static void SetPositionFromConnection(const struct MapConnection *connection, in
gSaveBlock1Ptr->pos.x -= connection->offset;
gSaveBlock1Ptr->pos.y = mapHeader->mapLayout->height;
break;
default:
DebugPrintfLevel(MGBA_LOG_WARN, "SetPositionFromConnection was passed an invalid direction (%d)!", direction);
break;
}
}

Expand All @@ -663,14 +666,22 @@ bool8 CameraMove(int x, int y)
old_x = gSaveBlock1Ptr->pos.x;
old_y = gSaveBlock1Ptr->pos.y;
connection = GetIncomingConnection(direction, gSaveBlock1Ptr->pos.x, gSaveBlock1Ptr->pos.y);
SetPositionFromConnection(connection, direction, x, y);
LoadMapFromCameraTransition(connection->mapGroup, connection->mapNum);
gCamera.active = TRUE;
gCamera.x = old_x - gSaveBlock1Ptr->pos.x;
gCamera.y = old_y - gSaveBlock1Ptr->pos.y;
gSaveBlock1Ptr->pos.x += x;
gSaveBlock1Ptr->pos.y += y;
MoveMapViewToBackup(direction);
if (connection)
{
SetPositionFromConnection(connection, direction, x, y);
LoadMapFromCameraTransition(connection->mapGroup, connection->mapNum);
gCamera.active = TRUE;
gCamera.x = old_x - gSaveBlock1Ptr->pos.x;
gCamera.y = old_y - gSaveBlock1Ptr->pos.y;
gSaveBlock1Ptr->pos.x += x;
gSaveBlock1Ptr->pos.y += y;
MoveMapViewToBackup(direction);
}
else
{
DebugPrintfLevel(MGBA_LOG_WARN, "GetIncomingConnection returned an invalid connection inside CameraMove!");
}

}
return gCamera.active;
}
Expand Down
13 changes: 5 additions & 8 deletions src/overworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,9 @@ static void InitMapView(void)
InitTilesetAnimations();
}

const struct MapLayout *GetMapLayout(void)
const struct MapLayout *GetMapLayout(u16 mapLayoutId)
{
u16 mapLayoutId = gSaveBlock1Ptr->mapLayoutId;
if (mapLayoutId)
return gMapLayouts[mapLayoutId - 1];
return NULL;
return gMapLayouts[mapLayoutId - 1];
}

void ApplyCurrentWarp(void)
Expand Down Expand Up @@ -618,13 +615,13 @@ static void LoadCurrentMapData(void)
sLastMapSectionId = gMapHeader.regionMapSectionId;
gMapHeader = *Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum);
gSaveBlock1Ptr->mapLayoutId = gMapHeader.mapLayoutId;
gMapHeader.mapLayout = GetMapLayout();
gMapHeader.mapLayout = GetMapLayout(gMapHeader.mapLayoutId);
}

static void LoadSaveblockMapHeader(void)
{
gMapHeader = *Overworld_GetMapHeaderByGroupAndId(gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum);
gMapHeader.mapLayout = GetMapLayout();
gMapHeader.mapLayout = GetMapLayout(gMapHeader.mapLayoutId);
}

static void SetPlayerCoordsFromWarp(void)
Expand Down Expand Up @@ -1020,7 +1017,7 @@ u8 GetFlashLevel(void)
void SetCurrentMapLayout(u16 mapLayoutId)
{
gSaveBlock1Ptr->mapLayoutId = mapLayoutId;
gMapHeader.mapLayout = GetMapLayout();
gMapHeader.mapLayout = GetMapLayout(mapLayoutId);
}

void SetObjectEventLoadFlag(u8 flag)
Expand Down
Loading

0 comments on commit 5481681

Please sign in to comment.