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

Various fixes for makefile and cleaning up errors reported by new sanitizer #310

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
136 changes: 81 additions & 55 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# vim: set ts=2 sw=2 :

#
# General Compiler Settings
#
Expand All @@ -8,7 +10,7 @@ CC=gcc

# general compiler settings
ifeq ($(M32),1)
FLAGS+= -m32
FLAGS+= -m32
endif
FLAGS+= -std=gnu99 -pipe -fcommon
CFLAGS=$(FLAGS) -Wall -Wextra
Expand All @@ -24,127 +26,151 @@ PROFILE=1
# use this if you want to build everything statically
STATIC=0

# Mudflap is a pointer use checking library. For more info:
# http://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging
MUDFLAP=0

ifeq ($(MUDFLAP),1)
ifeq ($(DEBUG),1)
FLAGS+= -fmudflap
LIB+= -lmudflap
else
X:=$(error Mudflap enabled without debug mode - probably not what you meant)
endif
# https://github.com/google/sanitizers
SANITIZE=0
ifeq ($(SANITIZE),1)
ifeq ($(DEBUG),1)
FLAGS+= -fsanitize=address
FLAGS+= -fsanitize-address-use-after-scope
FLAGS+= -fsanitize=leak

FLAGS+= -fsanitize=pointer-compare
FLAGS+= -fsanitize=pointer-subtract
FLAGS+= -fsanitize=bounds
FLAGS+= -fsanitize=bounds-strict
FLAGS+= -fsanitize=alignment

FLAGS+= -fsanitize=undefined
FLAGS+= -fsanitize=integer-divide-by-zero
FLAGS+= -fsanitize=float-divide-by-zero
FLAGS+= -fsanitize=float-cast-overflow
FLAGS+= -fsanitize=signed-integer-overflow
FLAGS+= -fsanitize=pointer-overflow

FLAGS+= -fsanitize=nonnull-attribute
FLAGS+= -fsanitize=returns-nonnull-attribute
else
X:=$(error sanitize enabled without debug mode - probably not what you wanted)
endif
endif

# stack-smash protection against buffer overflows and corrupt pointers
# (enabled by default on many systems today)
ifeq ($(SSP),1)
FLAGS+= -fstack-protector-all
CFLAGS+= -D_FORTIFY_SOURCE=2
FLAGS+= -fstack-protector-all
CFLAGS+= -D_FORTIFY_SOURCE=2
endif

ifeq ($(DEBUG),1)
FLAGS+= -g
FLAGS+= -g
else
CFLAGS+=-O3 -Winit-self
LDFLAGS+=-s
CFLAGS+=-O3 -Winit-self
LDFLAGS+=-s
endif

ifeq ($(PROFILE),1)
ifneq ($(DEBUG),1)
# Debug symbols needed for profiling to be useful
FLAGS+= -g
endif
FLAGS+= -pg
ifneq ($(DEBUG),1)
# Debug symbols needed for profiling to be useful
FLAGS+= -g
endif
FLAGS+= -pg
endif

#
# ProjectX Specific
#

# some systems use lua5.1
LUA=$(shell pkg-config lua && echo lua || echo lua5.1)
LUA=$(shell pkg-config --exists lua && echo lua || echo lua5.1)
MACOSX=$(shell uname -a | grep -qi darwin && echo 1 || echo 0)

# which version of sdl do you want to ask pkgconfig for ?
SDL=1
ifeq ($(SDL),1)
SDL_=sdl
SDL_=sdl
else
SDL_=sdl$(SDL)
SDL_=sdl$(SDL)
endif

# a full list of libs pulled in by pkg-config
PKG_CONFIG_DEPS= $(SDL_) $(LUA) $(LUA)-socket libenet libpng zlib openal

# throw an error if any required deps are missing
ifneq ($(MAKECMDGOALS),clean)
$(if $(shell pkg-config --exists $(PKG_CONFIG_DEPS) || echo fail), \
$(error "some pkg-config libraries are missing (did you mean ./libs/make.sh): $(PKG_CONFIG_DEPS)"))
endif

# which version of GL do you want to use ?
GL=1

$(if $(shell test "$(GL)" -ge 3 -a "$(SDL)" -lt 2 && echo fail), \
$(error "GL >= 3 only supported with SDL >= 2"))
$(error "GL >= 3 only supported with SDL >= 2"))

# library headers
CFLAGS+= `pkg-config --cflags $(SDL_) $(LUA) $(LUA)-socket libenet libpng zlib openal`
CFLAGS+= `pkg-config --cflags $(PKG_CONFIG_DEPS)`
ifeq ($(MACOSX),1)
CFLAGS += -DMACOSX
CFLAGS += -DMACOSX
endif

# libraries
ifeq ($(STATIC),1)
ifeq ($(MACOSX),1)
$(error MacOSX does not support static builds)
endif
LIB+= -Wl,-dn
PKG_CFG_OPTS= --static
ifeq ($(MACOSX),1)
$(error MacOSX does not support static builds)
endif
LIB+= -Wl,-dn
PKG_CFG_OPTS= --static
endif
LIB+= `pkg-config $(PKG_CFG_OPTS) --libs $(LUA) $(LUA)-socket libenet libpng zlib openal` -lm
LIB+= `pkg-config $(PKG_CFG_OPTS) --libs $(PKG_CONFIG_DEPS)` -lm
ifeq ($(STATIC),1)
LIB+= -Wl,-dy
LIB+= -Wl,-dy
endif

# dynamic only libraries
LIB+= `pkg-config --libs $(SDL_)`
ifeq ($(MINGW),1)
CFLAGS += -DMINGW
LIB += -L./mingw/bin
LIB += -lglu32 -lopengl32
LIB += -lsocket -lws2_32 -lwsock32 -lwinmm -lOpenAL32
CFLAGS += -DMINGW
LIB += -L./mingw/bin
LIB += -lglu32 -lopengl32
LIB += -lsocket -lws2_32 -lwsock32 -lwinmm -lOpenAL32
else ifeq ($(MACOSX),1)
# TODO - Support targeting X11 (need to compile sdl properly)
#LIB += -L/usr/X11/lib/ -lGL -lGLU
LIB += -framework OpenGL # OpenGL bundle on OSX.
LIB += -framework Cocoa # Used to target Quartz by SDL_.
# TODO - Support targeting X11 (need to compile sdl properly)
#LIB += -L/usr/X11/lib/ -lGL -lGLU
LIB += -framework OpenGL # OpenGL bundle on OSX.
LIB += -framework Cocoa # Used to target Quartz by SDL_.
else
LIB += -lGL -lGLU
LIB += -lGL -lGLU
endif
ifneq ($(MINGW),1)
# apparently on some systems -ldl is explicitly required
# perhaps this is part of the default libs on others...?
LIB+= -ldl
# apparently on some systems -ldl is explicitly required
# perhaps this is part of the default libs on others...?
LIB+= -ldl
endif

# ProjectX-specific includes
CFLAGS += -I.

# ProjectX-specific macros
ifeq ($(DXMOUSE),1)
CFLAGS += -DDXMOUSE -Idinput
LIB += -Ldinput -ldinput -ldxguid
CFLAGS += -DDXMOUSE -Idinput
LIB += -Ldinput -ldinput -ldxguid
endif
ifeq ($(RENDER_DISABLED),1)
CFLAGS+= -DRENDER_DISABLED
CFLAGS+= -DRENDER_DISABLED
else
CFLAGS+= -DGL=$(GL)
CFLAGS+= -DGL=$(GL)
endif
CFLAGS+= -DNET_ENET_2 -DBSP -DLUA_USE_APICHECK -DTEXTURE_PNG -DSOUND_SUPPORT -DSOUND_OPENAL
ifeq ($(DEBUG),1)
CFLAGS+= -DDEBUG_ON -DDEBUG_COMP -DDEBUG_SPOTFX_SOUND -DDEBUG_VIEWPORT
CFLAGS+= -DDEBUG_ON -DDEBUG_COMP -DDEBUG_SPOTFX_SOUND -DDEBUG_VIEWPORT
endif

ifeq ($(BOT),1)
CFLAGS+= -DLUA_BOT
CFLAGS+= -DLUA_BOT
endif

ifeq ($(INPUT_DISABLED),1)
CFLAGS+= -DINPUT_DISABLED
CFLAGS+= -DINPUT_DISABLED
endif


Expand Down Expand Up @@ -182,7 +208,7 @@ check:
@echo
@echo "DEBUG = $(DEBUG)"
@echo "PROFILE = $(PROFILE)"
@echo "MUDFLAP = $(MUDFLAP)"
@echo "SANITIZE = $(SANITIZE)"
@echo "STATIC = $(STATIC)"
@echo "PKG_CFG_OPTS = $(PKG_CFG_OPTS)"
@echo "MINGW = $(MINGW)"
Expand Down
7 changes: 2 additions & 5 deletions bgobjects.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,9 @@ void InitBGObjects( void )
BGObjects[ i ].Index = i;
BGObjects[ i ].NextUsed = NULL;
BGObjects[ i ].PrevUsed = NULL;
BGObjects[ i ].NextFree = &BGObjects[ i + 1 ];
BGObjects[ i ].PrevFree = &BGObjects[ i - 1 ];
BGObjects[ i ].NextFree = i==(MAXBGOBJECTS-1) ? NULL : &BGObjects[ i + 1 ];
BGObjects[ i ].PrevFree = i==0 ? NULL : &BGObjects[ i - 1 ];
}

BGObjects[ 0 ].PrevFree = NULL;
BGObjects[ MAXBGOBJECTS - 1 ].NextFree = NULL;
}

/*===================================================================
Expand Down
6 changes: 3 additions & 3 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ read_headlights( FILE *f, USERCONFIG *u, char *last_token )
static int
read_general_macro( FILE *f, char *config_macro, char *last_token, TEXT *macro )
{
char tempstr[ MAXTEXTMSG ];
char tempstr[ MAXTEXTMSG+1 ];
static char fmt[80];

sprintf( fmt, " %%%d[^\n]", MAXTEXTMSG );
Expand Down Expand Up @@ -716,7 +716,7 @@ read_mouse_sensitivity( FILE *f, USERCONFIG *u, char *last_token )
static int
read_name( FILE *f, USERCONFIG *u, char *last_token )
{
char namebuf[80];
char namebuf[81];
int ok;

ok = fscanf( f, " %80s", namebuf );
Expand Down Expand Up @@ -1301,7 +1301,7 @@ read_config( USERCONFIG *u, char *cfg_name )
{

FILE *f;
char token[80];
char token[81];
int j;

// all the config settings
Expand Down
7 changes: 2 additions & 5 deletions enemies.c
Original file line number Diff line number Diff line change
Expand Up @@ -3921,17 +3921,14 @@ void InitEnemies( void )
Enemies[ i ].Used = false;
Enemies[ i ].NextUsed = NULL;
Enemies[ i ].PrevUsed = NULL;
Enemies[ i ].NextFree = &Enemies[ i + 1 ];
Enemies[ i ].PrevFree = &Enemies[ i - 1 ];
Enemies[ i ].NextFree = i==(MAXENEMIES-1) ? NULL : &Enemies[ i + 1 ];
Enemies[ i ].PrevFree = i==0 ? NULL : &Enemies[ i - 1 ];

Enemies[ i ].NextInGroup = NULL;
Enemies[ i ].PrevInGroup = NULL;
Enemies[ i ].Index = i;
Enemies[ i ].Object.Type = OBJECT_TYPE_ENEMY;
}

Enemies[ 0 ].PrevFree = NULL;
Enemies[ MAXENEMIES - 1 ].NextFree = NULL;
}

/*===================================================================
Expand Down
4 changes: 4 additions & 0 deletions models.c
Original file line number Diff line number Diff line change
Expand Up @@ -5017,6 +5017,10 @@ void CreateOrbitPulsar( u_int16_t Ship )
int16_t Count;
u_int16_t Model;

// avoid division by zero
if(Ships[Ship].NumMultiples == 0)
return;

Rot = 0.0F;
Inc = ( 360.0F / Ships[ Ship ].NumMultiples );

Expand Down
2 changes: 1 addition & 1 deletion multiplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void StartAHostSession ( MENUITEM * Item )

MaxKills = MaxKillsSlider.value;

RandomStartPosModify = (u_int16_t) ( ms * 71.42857143 );
RandomStartPosModify = (u_int16_t) ( ms * 71 );

local_port = atoi(local_port_str.text);
SetGamePrefs();
Expand Down
2 changes: 1 addition & 1 deletion new3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef DWORD COLOR; // bgra
===================================================================*/

// bjd - taken from d3dtypes.h
#define RGBA_MAKE(r, g, b, a) ((COLOR) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)))
#define RGBA_MAKE(r, g, b, a) ((COLOR) (((COLOR)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)))
#define RGB_MAKE(r, g, b) ((COLOR) (((r) << 16) | ((g) << 8) | (b)))

// COLOR is packed bgra
Expand Down
2 changes: 1 addition & 1 deletion oct2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ bool InitLevels( char * levels_list )

// scan each level name into ShortLevelNames[j]
j=0;
while ( j < MAXLEVELS && fscanf( f, " %s", ShortLevelNames[ j ] ) == 1 )
while ( j < MAXLEVELS && fscanf( f, " %32s", ShortLevelNames[ j ] ) == 1 )
{


Expand Down
7 changes: 5 additions & 2 deletions primary.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,11 @@ void RestoreWeapons( void )
PrimaryWeaponsGot[ SUSS_GUN ] = 0;
PrimaryWeaponsGot[ LASER ] = 0;

Ships[ WhoIAm ].Primary = PULSAR;
Ships[ WhoIAm ].Secondary = MUGMISSILE;
if(WhoIAm != UNASSIGNED_SHIP)
{
Ships[ WhoIAm ].Primary = PULSAR;
Ships[ WhoIAm ].Secondary = MUGMISSILE;
}
}

/*===================================================================
Expand Down
7 changes: 2 additions & 5 deletions spotfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,12 @@ void SetupSpotFX( void )

SpotFX[ i ].NextUsed = NULL;
SpotFX[ i ].PrevUsed = NULL;
SpotFX[ i ].NextFree = &SpotFX[ i + 1 ];
SpotFX[ i ].PrevFree = &SpotFX[ i - 1 ];
SpotFX[ i ].NextFree = i==(MAXSPOTFX-1) ? NULL : &SpotFX[ i + 1 ];
SpotFX[ i ].PrevFree = i==0 ? NULL : &SpotFX[ i - 1 ];
SpotFX[ i ].NextInGroup = NULL;
SpotFX[ i ].PrevInGroup = NULL;
SpotFX[ i ].Index = i;
}

SpotFX[ 0 ].PrevFree = NULL;
SpotFX[ MAXSPOTFX - 1 ].NextFree = NULL;
}

/*===================================================================
Expand Down
10 changes: 9 additions & 1 deletion text.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,16 @@ void PrintScoreSort( void )
{
for(i=0; i<MAX_PLAYERS; i++)
{
tempwidth = (u_int16_t) log10(abs(GetKills(i)))+1;
int kills = GetKills(i);

// get display width of a number
// protecting against log10(0) throwing `inf`
tempwidth = (u_int16_t) (kills == 0 ? 1 : log10(abs(kills))+1);

// add space for negative sign?
if(GetKills(i) < 0) tempwidth++;

// keep track of widest score?
if(tempwidth > scorewidth) scorewidth = tempwidth;
}
}
Expand Down
4 changes: 2 additions & 2 deletions xmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void * X_malloc( size_t size, char *in_file, int in_line )
BlockInLine[i] = in_line;
MemUsed += size;

memset(Pnt,0,sizeof(Pnt)); // this protects whole program against dirty memory
memset(Pnt,0,size); // this protects whole program against dirty memory

return Pnt;

Expand Down Expand Up @@ -137,7 +137,7 @@ void * X_calloc( size_t num,size_t size, char *in_file, int in_line )
BlockInLine[i] = in_line;
MemUsed += num * size;

memset(Pnt,0,sizeof(Pnt)); // this protects whole program against dirty memory
memset(Pnt,0,size); // this protects whole program against dirty memory

return Pnt;

Expand Down