From f4e3c3d24ca880fe72396b2b0fc1ca3ce506b6ff Mon Sep 17 00:00:00 2001
From: John Hodge <tpg@mutabah.net>
Date: Sun, 15 Jan 2012 18:43:26 +0800
Subject: [PATCH] Fixing bugs exposed by scan-build

---
 BuildConf/host/Makefile.cfg                   |  10 +-
 Kernel/arch/x86/mm_phys.c                     |   4 +-
 Kernel/heap.c                                 |   2 +-
 Modules/IPStack/tcp.c                         |   2 +-
 Usermode/Applications/axwin3_src/Makefile     |   6 +-
 .../Applications/axwin3_src/WM/decorator.c    |   2 +-
 .../axwin3_src/WM/renderers/widget.c          |   4 +-
 .../Applications/axwin3_src/WM/wm_input.c     |   2 +-
 .../axwin3_src/WM/wm_render_text.c            |   4 +-
 Usermode/Applications/ping_src/main.c         | 125 ------------------
 Usermode/Libraries/Makefile.tpl               |   2 +-
 Usermode/Libraries/ld-acess.so_src/Makefile   |   1 +
 12 files changed, 22 insertions(+), 142 deletions(-)

diff --git a/BuildConf/host/Makefile.cfg b/BuildConf/host/Makefile.cfg
index 2f8488ab..057e96e3 100644
--- a/BuildConf/host/Makefile.cfg
+++ b/BuildConf/host/Makefile.cfg
@@ -10,9 +10,13 @@ include $(ACESSDIR)/BuildConf/$(HOST_ARCH)/Makefile.cfg
 
 OBJDUMP := objdump -S
 
-CC := $(SAVED_CC_)
+CC_SUFFIX = 
+
 ifeq ($(HOST_ARCH),x86)
-CC += -m32
+CC_SUFFIX := -m32
+LD_SUFFIX := -melf_i386
 endif
-LD := $(SAVED_LD_)
+
+CC := $(SAVED_CC_) $(CC_SUFFIX)
+LD := $(SAVED_LD_) $(LD_SUFFIX)
 
diff --git a/Kernel/arch/x86/mm_phys.c b/Kernel/arch/x86/mm_phys.c
index 81edcf2c..aa1b2603 100644
--- a/Kernel/arch/x86/mm_phys.c
+++ b/Kernel/arch/x86/mm_phys.c
@@ -135,8 +135,8 @@ tPAddr MM_AllocPhys(void)
 	 int	first, last;
 	for( i = numAddrClasses; i -- > 1; )
 	{
-		first = 1 << (addrClasses[i-1] - 12);
-		last = (1 << (addrClasses[i] - 12)) - 1;
+		first = 1UL << (addrClasses[i-1] - 12);
+		last = (1UL << (addrClasses[i] - 12)) - 1;
 		// Range is above the last free page
 		if( first > giLastPossibleFree )
 			continue;
diff --git a/Kernel/heap.c b/Kernel/heap.c
index 921cc6b9..eefd13cf 100644
--- a/Kernel/heap.c
+++ b/Kernel/heap.c
@@ -68,7 +68,7 @@ void *Heap_Extend(int Bytes)
 	
 	// Bounds Check
 	if( (tVAddr)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
-		Bytes = MM_KHEAP_MAX - (tVAddr)gHeapEnd;
+//		Bytes = MM_KHEAP_MAX - (tVAddr)gHeapEnd;
 		return NULL;
 	}
 	
diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c
index bb71fdbf..e2cc4a8a 100644
--- a/Modules/IPStack/tcp.c
+++ b/Modules/IPStack/tcp.c
@@ -764,7 +764,7 @@ Uint16 TCP_GetUnusedPort()
 
 	// Get Next outbound port
 	ret = giTCP_NextOutPort++;
-	while( gaTCP_PortBitmap[ret/32] & (1 << (ret%32)) )
+	while( gaTCP_PortBitmap[ret/32] & (1UL << (ret%32)) )
 	{
 		ret ++;
 		giTCP_NextOutPort++;
diff --git a/Usermode/Applications/axwin3_src/Makefile b/Usermode/Applications/axwin3_src/Makefile
index caa174d9..2ed5bce2 100644
--- a/Usermode/Applications/axwin3_src/Makefile
+++ b/Usermode/Applications/axwin3_src/Makefile
@@ -9,14 +9,14 @@ SUBMAKE = $(MAKE) --no-print-directory
 
 .PNONY: all install clean $(FILES)
 
-all:
+all: $(FILES)
 	@$(foreach DIR,$(DIRS), echo --- $(NAME)/$(DIR) && $(SUBMAKE) -C $(DIR) $@ &&) true
-install:
+install: $(FILES)
 	@$(xMKDIR) $(DISTROOT)/Apps ; true
 	@$(xMKDIR) $(DISTROOT)/Apps/AxWin ; true
 	@$(xMKDIR) $(DISTROOT)/Apps/3.0 ; true
 	@$(foreach DIR,$(DIRS), echo --- $(NAME)/$(DIR) && $(SUBMAKE) -C $(DIR) $@ &&) true
-	@$(foreach FILE,$(FILES), $(SUBMAKE) $FILE;) true
+#	@$(foreach FILE,$(FILES), $(SUBMAKE) -C $(FILE);) true
 
 clean:
 	@$(foreach DIR,$(DIRS), $(SUBMAKE) -C $(DIR) $@ &&) true
diff --git a/Usermode/Applications/axwin3_src/WM/decorator.c b/Usermode/Applications/axwin3_src/WM/decorator.c
index 937fb18b..6239b769 100644
--- a/Usermode/Applications/axwin3_src/WM/decorator.c
+++ b/Usermode/Applications/axwin3_src/WM/decorator.c
@@ -48,7 +48,7 @@ void Decorator_UpdateBorderSize(tWindow *Window)
 
 void Decorator_Redraw(tWindow *Window)
 {
-	 int	bActive;
+	 int	bActive = 0;
 	 int	text_width, text_height;
 	
 	// TODO: This could possibly be expensive, but is there a better way?
diff --git a/Usermode/Applications/axwin3_src/WM/renderers/widget.c b/Usermode/Applications/axwin3_src/WM/renderers/widget.c
index 586a5069..7668cbd6 100644
--- a/Usermode/Applications/axwin3_src/WM/renderers/widget.c
+++ b/Usermode/Applications/axwin3_src/WM/renderers/widget.c
@@ -392,7 +392,7 @@ tElement *Widget_GetElementByPos(tWidgetWin *Info, int X, int Y)
 	tElement	*ret, *next, *ele;
 	
 	next = &Info->RootElement;
-	while(next)
+	do
 	{
 		ret = next;
 		next = NULL;
@@ -405,7 +405,7 @@ tElement *Widget_GetElementByPos(tWidgetWin *Info, int X, int Y)
 			if(Y >= ele->CachedY + ele->CachedH)	continue;
 			next = ele;
 		}
-	}
+	} while(next);
 	return ret;
 }
 
diff --git a/Usermode/Applications/axwin3_src/WM/wm_input.c b/Usermode/Applications/axwin3_src/WM/wm_input.c
index 1fc49f8e..3220f773 100644
--- a/Usermode/Applications/axwin3_src/WM/wm_input.c
+++ b/Usermode/Applications/axwin3_src/WM/wm_input.c
@@ -22,7 +22,7 @@ tWindow	*gpWM_DownStartWindow[MAX_BUTTONS];
 // === CODE ===
 tWindow *WM_int_GetWindowAtPos(int X, int Y)
 {
-	tWindow	*win, *next_win, *ret;
+	tWindow	*win, *next_win, *ret = NULL;
 	
 	next_win = gpWM_RootWindow;
 
diff --git a/Usermode/Applications/axwin3_src/WM/wm_render_text.c b/Usermode/Applications/axwin3_src/WM/wm_render_text.c
index d189e5e0..8168c5c0 100644
--- a/Usermode/Applications/axwin3_src/WM/wm_render_text.c
+++ b/Usermode/Applications/axwin3_src/WM/wm_render_text.c
@@ -171,7 +171,7 @@ tGlyph *_GetGlyph(tFont *Font, uint32_t Codepoint)
 				 prev = next, next = next->Next
 				 );
 			
-			if( next->Codepoint == Codepoint )
+			if( next && next->Codepoint == Codepoint )
 				return next;
 			
 		}
@@ -185,7 +185,7 @@ tGlyph *_GetGlyph(tFont *Font, uint32_t Codepoint)
 				 prev && prev->Codepoint > Codepoint;
 				 next = prev, prev = prev->Prev
 				 );
-			if( prev->Codepoint == Codepoint )
+			if( prev && prev->Codepoint == Codepoint )
 				return prev;
 		}
 	}
diff --git a/Usermode/Applications/ping_src/main.c b/Usermode/Applications/ping_src/main.c
index 4c8c84de..4be3306c 100644
--- a/Usermode/Applications/ping_src/main.c
+++ b/Usermode/Applications/ping_src/main.c
@@ -14,7 +14,6 @@
 // === PROTOTYPES ===
 void	PrintUsage(char *ProgName);
 void	PrintHelp(char *ProgName);
- int	GetAddress( char *Address, uint8_t *Addr );
 
 // === GLOBALS ===
  int	giNumberOfPings = 1;
@@ -137,127 +136,3 @@ void PrintHelp(char *ProgName)
 	fprintf(stderr, " -h\tPrint this message\n");
 }
 
-/**
- * \brief Read an IPv4 Address
- */
-int GetAddress4(char *String, uint8_t *Addr)
-{
-	 int	i = 0;
-	 int	j;
-	 int	val;
-	
-	for( j = 0; String[i] && j < 4; j ++ )
-	{
-		val = 0;
-		for( ; String[i] && String[i] != '.'; i++ )
-		{
-			if('0' > String[i] || String[i] > '9') {
-				printf("0<c<9 expected, '%c' found\n", String[i]);
-				return 0;
-			}
-			val = val*10 + String[i] - '0';
-		}
-		if(val > 255) {
-			printf("val > 255 (%i)\n", val);
-			return 0;
-		}
-		Addr[j] = val;
-		
-		if(String[i] == '.')
-			i ++;
-	}
-	if( j != 4 ) {
-		printf("4 parts expected, %i found\n", j);
-		return 0;
-	}
-	if(String[i] != '\0') {
-		printf("EOS != '\\0', '%c'\n", String[i]);
-		return 0;
-	}
-	return 1;
-}
-
-/**
- * \brief Read an IPv6 Address
- */
-int GetAddress6(char *String, uint8_t *Addr)
-{
-	 int	i = 0;
-	 int	j, k;
-	 int	val, split = -1, end;
-	uint16_t	hi[8], low[8];
-	
-	for( j = 0; String[i] && j < 8; j ++ )
-	{
-		if(String[i] == ':') {
-			if(split != -1) {
-				printf("Two '::'s\n");
-				return 0;
-			}
-			split = j;
-			i ++;
-			continue;
-		}
-		
-		val = 0;
-		for( k = 0; String[i] && String[i] != ':'; i++, k++ )
-		{
-			val *= 16;
-			if('0' <= String[i] && String[i] <= '9')
-				val += String[i] - '0';
-			else if('A' <= String[i] && String[i] <= 'F')
-				val += String[i] - 'A' + 10;
-			else if('a' <= String[i] && String[i] <= 'f')
-				val += String[i] - 'a' + 10;
-			else {
-				printf("%c unexpected\n", String[i]);
-				return 0;
-			}
-		}
-		
-		if(val > 0xFFFF) {
-			printf("val (0x%x) > 0xFFFF\n", val);
-			return 0;
-		}
-		
-		if(split == -1)
-			hi[j] = val;
-		else
-			low[j-split] = val;
-		
-		if( String[i] == ':' ) {
-			i ++;
-		}
-	}
-	end = j;
-	
-	for( j = 0; j < split; j ++ )
-	{
-		Addr[j*2] = hi[j]>>8;
-		Addr[j*2+1] = hi[j]&0xFF;
-	}
-	for( ; j < 8 - (end - split); j++ )
-	{
-		Addr[j*2] = 0;
-		Addr[j*2+1] = 0;
-	}
-	k = 0;
-	for( ; j < 8; j ++, k++)
-	{
-		Addr[j*2] = hi[k]>>8;
-		Addr[j*2+1] = hi[k]&0xFF;
-	}
-	
-	return 1;
-}
-
-int GetAddress(char *String, uint8_t *Addr)
-{
-	if( GetAddress4(String, Addr) )
-		return 4;
-	
-	if( GetAddress6(String, Addr) )
-		return 6;
-	
-	return 0;
-}
diff --git a/Usermode/Libraries/Makefile.tpl b/Usermode/Libraries/Makefile.tpl
index 57e49be2..e32dd603 100644
--- a/Usermode/Libraries/Makefile.tpl
+++ b/Usermode/Libraries/Makefile.tpl
@@ -19,7 +19,7 @@ DEPFILES := $(addsuffix .dep,$(OBJ))
 all: $(_BIN) $(_XBIN)
 
 clean:
-	$(RM) $(_BIN) $(_XBIN) $(OBJ) $(_BIN).dsm $(DEPFILES)
+	$(RM) $(_BIN) $(_XBIN) $(OBJ) $(_BIN).dsm $(DEPFILES) $(EXTRACLEAN)
 
 install: all
 	@echo [xCP] $(DISTROOT)/Libs/$(BIN)
diff --git a/Usermode/Libraries/ld-acess.so_src/Makefile b/Usermode/Libraries/ld-acess.so_src/Makefile
index 178b75e3..585e7108 100644
--- a/Usermode/Libraries/ld-acess.so_src/Makefile
+++ b/Usermode/Libraries/ld-acess.so_src/Makefile
@@ -8,6 +8,7 @@ OBJ := main.o lib.o loadlib.o export.o elf.o pe.o
 OBJ += arch/$(ARCHDIR).ao
 BIN = ld-acess.so
 EXTRABIN := libld-acess.so
+EXTRACLEAN := $(_OBJPREFIX)_stublib.o
 
 CFLAGS   = -g -Wall -fno-builtin -fno-leading-underscore -fno-stack-protector -fPIC
 CFLAGS  += $(CPPFLAGS)
-- 
GitLab