Skip to content

Commit

Permalink
Fix bugs/warnings from static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
MysterD committed Feb 21, 2022
1 parent 13ecef0 commit 69fb69d
Show file tree
Hide file tree
Showing 42 changed files with 144 additions and 101 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ ifeq ($(WINDOWS_AUTO_BUILDER),1)
EXTRA_INCLUDES := -I ../include/1 -I ../include/2 -I ../include/3 -I ../include/4
EXTRA_CFLAGS := -Wno-expansion-to-defined
else
EXTRA_INCLUDES :=
EXTRA_CFLAGS :=
EXTRA_INCLUDES ?=
EXTRA_CFLAGS ?=
endif

EXTRACT_ASSETS := $(PYTHON) ./extract_assets.py
Expand Down Expand Up @@ -591,7 +591,7 @@ SDL1_USED := 0
SDL2_USED := 0

# suppress warnings
BACKEND_CFLAGS += -Wno-format-truncation
BACKEND_CFLAGS += -Wno-format-truncation -Wno-format-security -Wno-trigraphs
BACKEND_CFLAGS += $(EXTRA_CFLAGS)

# for now, it's either SDL+GL or DXGI+DirectX, so choose based on WAPI
Expand Down Expand Up @@ -652,19 +652,19 @@ ifneq ($(SDL1_USED)$(SDL2_USED),00)
endif

ifeq ($(WINDOWS_BUILD),1)
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -DWINSOCK
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -DWINSOCK
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv -DWINSOCK

ifeq ($(TARGET_BITS), 32)
BACKEND_LDFLAGS += -ldbghelp
endif
else ifeq ($(TARGET_WEB),1)
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -s USE_SDL=2
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -s USE_SDL=2
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv -s USE_SDL=2

# Linux / Other builds below
else
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS)
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra $(VERSION_CFLAGS) $(GRUCODE_CFLAGS)
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv

endif
Expand Down
2 changes: 2 additions & 0 deletions developer/static-analysis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
make clean ; scan-build -disable-checker deadcode.DeadStores make BETTERCAMERA=1 NODRAWINGDISTANCE=1 DEBUG=1 IMMEDIATELOAD=1 DEVELOPMENT=1 STRICT=1 EXTRA_INCLUDES="-IC:/msys64/mingw64/include" -j
10 changes: 9 additions & 1 deletion src/audio/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ void *soundAlloc(struct SoundAllocPool *pool, u32 size) {
#else
u32 alignedSize = ALIGN16(size);

if (pool == NULL || pool->cur == NULL) {
return NULL;
}

u8* start = pool->cur;
if ((start + alignedSize <= pool->size + pool->start)) {
bzero(start, alignedSize);
Expand Down Expand Up @@ -328,7 +332,7 @@ void *alloc_bank_or_seq(struct SoundMultiPool *arg0, s32 arg1, s32 size, s32 arg
u16 secondVal;
#endif
u32 nullID = -1;
u8 *table;
u8 *table = NULL;
u8 isSound;
#ifndef VERSION_EU
u16 firstVal;
Expand All @@ -349,6 +353,10 @@ void *alloc_bank_or_seq(struct SoundMultiPool *arg0, s32 arg1, s32 size, s32 arg
isSound = TRUE;
}

if (table == NULL) {
return NULL;
}

firstVal = (tp->entries[0].id == (s8)nullID ? SOUND_LOAD_STATUS_NOT_LOADED : table[tp->entries[0].id]);
secondVal = (tp->entries[1].id == (s8)nullID ? SOUND_LOAD_STATUS_NOT_LOADED : table[tp->entries[1].id]);

Expand Down
4 changes: 2 additions & 2 deletions src/audio/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,8 @@ u8 get_missing_bank(u32 seqId, s32 *nonNullCount, s32 *nullCount) {
}

struct AudioBank *load_banks_immediate(s32 seqId, u8 *arg1) {
void *ret;
u32 bankId;
void *ret = NULL;
u32 bankId = 0;
u16 offset;
u8 i;

Expand Down
12 changes: 7 additions & 5 deletions src/audio/seqplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ GLOBAL_ASM("asm/non_matchings/seq_channel_layer_process_script_us.s")
#endif

u8 get_instrument(struct SequenceChannel *seqChannel, u8 instId, struct Instrument **instOut, struct AdsrSettings *adsr) {
struct Instrument *inst;
struct Instrument *inst = NULL;
#ifdef VERSION_EU
inst = get_instrument_inner(seqChannel->bankId, instId);
if (inst == NULL)
Expand Down Expand Up @@ -1063,8 +1063,10 @@ u8 get_instrument(struct SequenceChannel *seqChannel, u8 instId, struct Instrume
|| ((uintptr_t) gBankLoadedPool.temporary.pool.start <= (uintptr_t) inst
&& (uintptr_t) inst <= (uintptr_t)(gBankLoadedPool.temporary.pool.start
+ gBankLoadedPool.temporary.pool.size))) {
adsr->envelope = inst->envelope;
adsr->releaseRate = inst->releaseRate;
if (inst != NULL) {
adsr->envelope = inst->envelope;
adsr->releaseRate = inst->releaseRate;
}
*instOut = inst;
instId++;
return instId;
Expand Down Expand Up @@ -1114,7 +1116,7 @@ void sequence_channel_process_script(struct SequenceChannel *seqChannel) {
u8 cmd; // v1, s1
u8 loBits; // t0, a0
s32 offset;
s8 value; // sp53, 4b
s8 value = 0; // sp53, 4b
u8 temp;
s8 tempSigned;
UNUSED u8 temp2;
Expand Down Expand Up @@ -1678,7 +1680,7 @@ void sequence_player_process_sequence(struct SequencePlayer *seqPlayer) {
u8 cmd;
u8 loBits;
u8 temp;
s32 value;
s32 value = 0;
s32 i;
u16 u16v;
u32 u32v;
Expand Down
4 changes: 2 additions & 2 deletions src/audio/synthesis.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,11 @@ u64 *synthesis_process_notes(s16 *aiBuf, s32 bufLen, u64 *cmd) {
UNUSED u8 pad7[0x0c]; // sp100
UNUSED s32 tempBufLen;
#ifdef VERSION_EU
s32 sp130; //sp128, sp104
s32 sp130 = 0; //sp128, sp104
UNUSED u32 pad9;
#else
UNUSED u32 pad9;
s32 sp130; //sp128, sp104
s32 sp130 = 0; //sp128, sp104
#endif
s32 nAdpcmSamplesProcessed; // signed required for US
s32 t0;
Expand Down
32 changes: 16 additions & 16 deletions src/engine/math_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ void *vec3f_copy(Vec3f dest, Vec3f src) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Set vector 'dest' to (x, y, z)
void *vec3f_set(Vec3f dest, f32 x, f32 y, f32 z) {
dest[0] = x;
dest[1] = y;
dest[2] = z;
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Add vector 'a' to 'dest'
void *vec3f_add(Vec3f dest, Vec3f a) {
dest[0] += a[0];
dest[1] += a[1];
dest[2] += a[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Make 'dest' the sum of vectors a and b.
void *vec3f_sum(Vec3f dest, Vec3f a, Vec3f b) {
dest[0] = a[0] + b[0];
dest[1] = a[1] + b[1];
dest[2] = a[2] + b[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Multiply vector 'dest' by a
Expand All @@ -50,55 +50,55 @@ void *vec3f_mul(Vec3f dest, f32 a)
dest[0] *= a;
dest[1] *= a;
dest[2] *= a;
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Copy vector src to dest
void *vec3s_copy(Vec3s dest, Vec3s src) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Set vector 'dest' to (x, y, z)
void *vec3s_set(Vec3s dest, s16 x, s16 y, s16 z) {
dest[0] = x;
dest[1] = y;
dest[2] = z;
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Add vector a to 'dest'
void *vec3s_add(Vec3s dest, Vec3s a) {
dest[0] += a[0];
dest[1] += a[1];
dest[2] += a[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Make 'dest' the sum of vectors a and b.
void *vec3s_sum(Vec3s dest, Vec3s a, Vec3s b) {
dest[0] = a[0] + b[0];
dest[1] = a[1] + b[1];
dest[2] = a[2] + b[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Make 'dest' the difference of vectors a and b.
void *vec3f_dif(Vec3f dest, Vec3f a, Vec3f b) {
dest[0] = a[0] - b[0];
dest[1] = a[1] - b[1];
dest[2] = a[2] - b[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Convert short vector a to float vector 'dest'
void *vec3s_to_vec3f(Vec3f dest, Vec3s a) {
dest[0] = a[0];
dest[1] = a[1];
dest[2] = a[2];
return &dest; //! warning: function returns address of local variable
return dest;
}

/**
Expand All @@ -110,7 +110,7 @@ void *vec3f_to_vec3s(Vec3s dest, Vec3f a) {
dest[0] = a[0] + ((a[0] > 0) ? 0.5f : -0.5f);
dest[1] = a[1] + ((a[1] > 0) ? 0.5f : -0.5f);
dest[2] = a[2] + ((a[2] > 0) ? 0.5f : -0.5f);
return &dest; //! warning: function returns address of local variable
return dest;
}

/**
Expand All @@ -122,15 +122,15 @@ void *find_vector_perpendicular_to_plane(Vec3f dest, Vec3f a, Vec3f b, Vec3f c)
dest[0] = (b[1] - a[1]) * (c[2] - b[2]) - (c[1] - b[1]) * (b[2] - a[2]);
dest[1] = (b[2] - a[2]) * (c[0] - b[0]) - (c[2] - b[2]) * (b[0] - a[0]);
dest[2] = (b[0] - a[0]) * (c[1] - b[1]) - (c[0] - b[0]) * (b[1] - a[1]);
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Make vector 'dest' the cross product of vectors a and b.
void *vec3f_cross(Vec3f dest, Vec3f a, Vec3f b) {
dest[0] = a[1] * b[2] - b[1] * a[2];
dest[1] = a[2] * b[0] - b[2] * a[0];
dest[2] = a[0] * b[1] - b[0] * a[1];
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Scale vector 'dest' so it has length 1
Expand All @@ -141,7 +141,7 @@ void *vec3f_normalize(Vec3f dest) {
dest[0] *= invsqrt;
dest[1] *= invsqrt;
dest[2] *= invsqrt;
return &dest; //! warning: function returns address of local variable
return dest;
}

/// Get length of vector 'a'
Expand Down Expand Up @@ -883,7 +883,7 @@ void anim_spline_init(struct MarioState* m, Vec4s *keyFrames) {
* Returns TRUE when the last point is reached, FALSE otherwise.
*/
s32 anim_spline_poll(struct MarioState* m, Vec3f result) {
Vec4f weights;
Vec4f weights = { 0 };
s32 i;
s32 hasEnded = FALSE;

Expand Down
7 changes: 4 additions & 3 deletions src/engine/surface_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ static void stub_surface_load_1(void) {
* @param vertexIndices Helper which tells positions in vertexData to start reading vertices
*/
static struct Surface *read_surface_data(s16 *vertexData, s16 **vertexIndices) {
if (vertexData == NULL || vertexIndices == NULL || *vertexIndices == NULL) { return NULL; }

struct Surface *surface;
register s32 x1, y1, z1;
register s32 x2, y2, z2;
Expand Down Expand Up @@ -594,9 +596,8 @@ u32 get_area_terrain_size(s16 *data) {
* boxes (water, gas, JRB fog).
*/
void load_area_terrain(s16 index, s16 *data, s8 *surfaceRooms, s16 *macroObjects) {
s16 terrainLoadType;
s16 *vertexData;
UNUSED s32 unused;
s16 terrainLoadType = 0;
s16 *vertexData = NULL;

// Initialize the data for this.
gEnvironmentRegions = NULL;
Expand Down
6 changes: 4 additions & 2 deletions src/game/area.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,15 @@ void unload_mario_area(void) {
}

void change_area(s32 index) {
s32 areaFlags = gCurrentArea->flags;
s32 areaFlags = (gCurrentArea != NULL) ? gCurrentArea->flags : 0;

if (gCurrAreaIndex != index) {
unload_area();
load_area(index);

gCurrentArea->flags = areaFlags;
if (gCurrentArea != NULL) {
gCurrentArea->flags = areaFlags;
}
for (int i = 0; i < MAX_PLAYERS; i++) {
gMarioStates[i].marioObj->oActiveParticleFlags = 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ s32 update_fixed_camera(struct Camera *c, Vec3f focus, UNUSED Vec3f pos) {
f32 focusFloorOff;
f32 goalHeight;
f32 ceilHeight;
f32 heightOffset;
f32 heightOffset = 0;
f32 distCamToFocus;
UNUSED u8 filler2[8];
f32 scaleToMario = 0.5f;
Expand Down Expand Up @@ -4917,7 +4917,7 @@ void play_sound_if_cam_switched_to_lakitu_or_mario(void) {
* Handles input for radial, outwards radial, parallel tracking, and 8 direction mode.
*/
s32 radial_camera_input(struct Camera *c, UNUSED f32 unused) {
s16 dummy;
s16 dummy = 0;

if ((gCameraMovementFlags & CAM_MOVE_ENTERED_ROTATE_SURFACE) || !(gCameraMovementFlags & CAM_MOVE_ROTATE)) {

Expand Down Expand Up @@ -5160,7 +5160,7 @@ s32 determine_dance_cutscene(UNUSED struct Camera *c) {
* @return `pullResult` or `pushResult` depending on Mario's door action
*/
u8 open_door_cutscene(u8 pullResult, u8 pushResult) {
s16 result;
s16 result = 0;

if (sMarioCamState->action == ACT_PULLING_DOOR) {
result = pullResult;
Expand Down Expand Up @@ -11286,7 +11286,7 @@ struct CutsceneSplinePoint sCcmOutsideCreditsSplineFocus[] = {
void play_cutscene(struct Camera *c) {
UNUSED u32 pad[3];
UNUSED s16 unusedYawFocToMario;
s16 cutsceneDuration;
s16 cutsceneDuration = 0;
u8 oldCutscene;

unusedYawFocToMario = sAreaYaw;
Expand Down
8 changes: 4 additions & 4 deletions src/game/envfx_bubbles.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ Gfx *envfx_update_bubble_particles(s32 mode, UNUSED Vec3s marioPos, Vec3s camFro
s32 i;
s16 radius, pitch, yaw;

Vec3s vertex1;
Vec3s vertex2;
Vec3s vertex3;
Vec3s interpolatedVertices[3];
Vec3s vertex1 = { 0 };
Vec3s vertex2 = { 0 };
Vec3s vertex3 = { 0 };
Vec3s interpolatedVertices[3] = { 0 };

static Vec3s prevVertex1;
static Vec3s prevVertex2;
Expand Down
4 changes: 2 additions & 2 deletions src/game/ingame_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1818,12 +1818,12 @@ s8 gDialogCourseActNum = 1;

void render_dialog_entries(void) {
#ifdef VERSION_EU
s8 lowerBound;
s8 lowerBound = 0;
#endif
void **dialogTable;
struct DialogEntry *dialog;
#ifdef VERSION_US
s8 lowerBound;
s8 lowerBound = 0;
#endif
#ifdef VERSION_EU
gInGameLanguage = eu_get_language();
Expand Down
2 changes: 1 addition & 1 deletion src/game/level_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ static void initiate_painting_warp_node(struct WarpNode *pWarpNode) {
* Check is Mario has entered a painting, and if so, initiate a warp.
*/
void initiate_painting_warp(void) {
if (gCurrentArea->paintingWarpNodes != NULL && gMarioState->floor != NULL) {
if (gCurrentArea != NULL && gCurrentArea->paintingWarpNodes != NULL && gMarioState->floor != NULL) {
struct WarpNode *pWarpNode = get_painting_warp_node();

if (pWarpNode != NULL) {
Expand Down
Loading

0 comments on commit 69fb69d

Please sign in to comment.