Browse Source

Eliminate most warnings & fix potential bugs

LouYihua 7 years ago
parent
commit
328f8c52e7
23 changed files with 271 additions and 304 deletions
  1. 2 2
      adplug/dbemuopl.cpp
  2. 4 0
      adplug/dbopl.cpp
  3. 1 1
      adplug/demuopl.cpp
  4. 1 1
      adplug/surroundopl.cpp
  5. 4 16
      audio.c
  6. 2 0
      common.h
  7. 12 5
      font.c
  8. 93 141
      global.c
  9. 1 1
      libmad/music_mad.c
  10. 4 0
      liboggvorbis/src/info.c
  11. 4 0
      liboggvorbis/src/vorbisfile.c
  12. 15 10
      mp3play.c
  13. 16 10
      oggplay.c
  14. 31 35
      palcfg.c
  15. 14 17
      palcommon.c
  16. 0 6
      players.h
  17. 7 11
      rngplay.c
  18. 11 11
      sound.c
  19. 22 7
      text.c
  20. 22 29
      uigame.c
  21. 1 1
      unix/native_midi.cpp
  22. 2 0
      unix/pal_config.h
  23. 2 0
      win32/native_midi.cpp

+ 2 - 2
adplug/dbemuopl.cpp

@@ -46,8 +46,8 @@ static inline uint8_t conver_to_uint8(int32_t sample)
 }
 
 CDBemuopl::CDBemuopl(int rate, bool bit16, bool usestereo)
-	: use16bit(bit16), stereo(usestereo), rate(rate)
-	, maxlen(0), buffer(NULL)
+	: buffer(NULL), rate(rate), maxlen(0)
+	, use16bit(bit16), stereo(usestereo)
 {
 	currType = TYPE_OPL2;
 	chip.Setup(rate);

+ 4 - 0
adplug/dbopl.cpp

@@ -883,6 +883,8 @@ Channel* Channel::BlockTemplate( Chip* chip, Bit32u samples, Bit32s* output ) {
 			return (this + 2);
 		}
 		break;
+	default:
+		break;
 	}
 	//Init the operators with the the current vibrato and tremolo values
 	Op( 0 )->Prepare( chip );
@@ -948,6 +950,8 @@ Channel* Channel::BlockTemplate( Chip* chip, Bit32u samples, Bit32s* output ) {
 			output[ i * 2 + 0 ] += sample & maskLeft;
 			output[ i * 2 + 1 ] += sample & maskRight;
 			break;
+		default:
+			break;
 		}
 	}
 	switch( mode ) {

+ 1 - 1
adplug/demuopl.cpp

@@ -24,7 +24,7 @@
 #include <string.h>
 
 CDemuopl::CDemuopl(int rate, bool bit16, bool usestereo)
-	: use16bit(bit16), stereo(usestereo), rate(rate), chip(adlib_init(rate))
+	: chip(adlib_init(rate)), rate(rate), use16bit(bit16), stereo(usestereo)
 {
 	currType = TYPE_OPL2;
 }

+ 1 - 1
adplug/surroundopl.cpp

@@ -28,7 +28,7 @@
 //#include "debug.h"
 
 CSurroundopl::CSurroundopl(Copl *a, Copl *b, bool use16bit, double opl_freq, double freq_offset)
-	: use16bit(use16bit), bufsize(4096), a(a), b(b), opl_freq(opl_freq), freq_offset(freq_offset)
+	: a(a), b(b), freq_offset(freq_offset), opl_freq(opl_freq), bufsize(4096), use16bit(use16bit)
 {
 	currType = TYPE_OPL2;
 	this->lbuf = new short[this->bufsize];

+ 4 - 16
audio.c

@@ -259,22 +259,16 @@ AUDIO_OpenDevice(
 	   }
 	   break;
    case MUSIC_MP3:
-#if PAL_HAS_MP3
 	   gAudioDevice.pMusPlayer = MP3_Init();
-#else
-	   gAudioDevice.pMusPlayer = NULL;
-#endif
 	   break;
    case MUSIC_OGG:
-#if PAL_HAS_OGG
 	   gAudioDevice.pMusPlayer = OGG_Init();
-#else
-	   gAudioDevice.pMusPlayer = NULL;
-#endif
 	   break;
    case MUSIC_MIDI:
 	   gAudioDevice.pMusPlayer = NULL;
 	   break;
+   default:
+	   break;
    }
 
    //
@@ -309,18 +303,12 @@ AUDIO_OpenDevice(
 	   break;
    }
    case MUSIC_MP3:
-#if PAL_HAS_MP3
 	   gAudioDevice.pCDPlayer = MP3_Init();
-#else
-	   gAudioDevice.pCDPlayer = NULL;
-#endif
 	   break;
    case MUSIC_OGG:
-#if PAL_HAS_OGG
 	   gAudioDevice.pCDPlayer = OGG_Init();
-#else
-	   gAudioDevice.pCDPlayer = NULL;
-#endif
+	   break;
+   default:
 	   break;
    }
 

+ 2 - 0
common.h

@@ -212,4 +212,6 @@ typedef const WCHAR        *LPCWSTR;
 # define PAL_FATAL_OUTPUT(s)
 #endif
 
+#define PAL_fread(buf, elem, num, fp) if (fread((buf), (elem), (num), (fp)) < (num)) return -1
+
 #endif

+ 12 - 5
font.c

@@ -46,7 +46,8 @@ static void PAL_InitEmbeddedFont(void)
 	FILE *fp;
 	char *char_buf;
 	wchar_t *wchar_buf;
-	int nBytes, nChars, i;
+	size_t nBytes;
+	int nChars, i;
 
 	//
 	// Load the wor16.asc file.
@@ -71,7 +72,11 @@ static void PAL_InitEmbeddedFont(void)
 		return;
 	}
 	fseek(fp, 0, SEEK_SET);
-	fread(char_buf, 1, nBytes, fp);
+	if (fread(char_buf, 1, nBytes, fp) < nBytes)
+	{
+		fclose(fp);
+		return;
+	}
 
 	//
 	// Close wor16.asc file.
@@ -106,9 +111,11 @@ static void PAL_InitEmbeddedFont(void)
 	for (i = 0; i < nChars; i++)
 	{
 		wchar_t w = (wchar_buf[i] >= unicode_upper_base) ? (wchar_buf[i] - unicode_upper_base + unicode_lower_top) : wchar_buf[i];
-		fread(unicode_font[w], 30, 1, fp);
-		unicode_font[w][30] = 0;
-		unicode_font[w][31] = 0;
+		if (fread(unicode_font[w], 30, 1, fp) == 1)
+		{
+			unicode_font[w][30] = 0;
+			unicode_font[w][31] = 0;
+		}
 	}
 	free(wchar_buf);
 

+ 93 - 141
global.c

@@ -54,7 +54,7 @@ PAL_IsWINVersion(
 )
 {
 	FILE *fps[] = { UTIL_OpenRequiredFile("abc.mkf"), UTIL_OpenRequiredFile("map.mkf"), gpGlobals->f.fpF, gpGlobals->f.fpFBP, gpGlobals->f.fpFIRE, gpGlobals->f.fpMGO };
-	char *data = NULL;
+	uint8_t *data = NULL;
 	int data_size = 0, dos_score = 0, win_score = 0;
 	BOOL result = FALSE;
 
@@ -72,7 +72,7 @@ PAL_IsWINVersion(
 		// Note that this check is not 100% correct, however in incorrect situations,
 		// the sub-file will be over 784MB if uncompressed, which is highly unlikely.
 		//
-		if (data_size < size) data = (char *)realloc(data, data_size = size);
+		if (data_size < size) data = (uint8_t *)realloc(data, data_size = size);
 		PAL_MKFReadChunk(data, data_size, j, fps[i]);
 		if (data[0] == 'Y' && data[1] == 'J' && data[2] == '_' && data[3] == '1')
 		{
@@ -521,11 +521,47 @@ typedef struct tagSAVEDGAME_WIN
 	EVENTOBJECT      rgEventObject[MAX_EVENT_OBJECTS];
 } SAVEDGAME_WIN, *LPSAVEDGAME_WIN;
 
-static VOID
+static BOOL
 PAL_LoadGame_Common(
-	const LPSAVEDGAME_COMMON s
+	const char         *szFileName,
+	LPSAVEDGAME_COMMON  s,
+	size_t              size
 )
 {
+	//
+	// Try to open the specified file
+	//
+	FILE *fp = fopen(szFileName, "rb");
+	//
+	// Read all data from the file and close.
+	//
+	size_t n = fp ? fread(s, 1, size, fp) : 0;
+
+	if (fp != NULL)
+	{
+		fclose(fp);
+	}
+
+	if (n < size - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS)
+	{
+		return FALSE;
+	}
+
+	//
+	// Adjust endianness
+	//
+	DO_BYTESWAP(&s, size);
+
+	//
+	// Cash amount is in DWORD, so do a wordswap in Big-Endian.
+	//
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+	s->dwCash = ((s->dwCash >> 16) | (s->dwCash << 16));
+#endif
+
+	//
+	// Get common data from the saved game struct.
+	//
 	gpGlobals->viewport = PAL_XY(s->wViewportX, s->wViewportY);
 	gpGlobals->wMaxPartyMemberIndex = s->nPartyMember;
 	gpGlobals->wNumScene = s->wNumScene;
@@ -557,6 +593,12 @@ PAL_LoadGame_Common(
 	memset(gpGlobals->rgPoisonStatus, 0, sizeof(gpGlobals->rgPoisonStatus));
 	memcpy(gpGlobals->rgInventory, s->rgInventory, sizeof(gpGlobals->rgInventory));
 	memcpy(gpGlobals->g.rgScene, s->rgScene, sizeof(gpGlobals->g.rgScene));
+
+	gpGlobals->fEnteringScene = FALSE;
+
+	PAL_CompressInventory();
+
+	return TRUE;
 }
 
 static INT
@@ -578,41 +620,15 @@ PAL_LoadGame_DOS(
 
 --*/
 {
-   FILE                     *fp;
    PAL_LARGE SAVEDGAME_DOS   s;
    int                       i;
 
-   //
-   // Try to open the specified file
-   //
-   fp = fopen(szFileName, "rb");
-   if (fp == NULL)
-   {
-      return -1;
-   }
-
-   //
-   // Read all data from the file and close.
-   //
-   fread(&s, sizeof(SAVEDGAME_DOS), 1, fp);
-   fclose(fp);
-
-   //
-   // Adjust endianness
-   //
-   DO_BYTESWAP(&s, sizeof(SAVEDGAME_DOS));
-
-   //
-   // Cash amount is in DWORD, so do a wordswap in Big-Endian.
-   //
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-   s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
-#endif
-
    //
    // Get all the data from the saved game struct.
    //
-   PAL_LoadGame_Common((LPSAVEDGAME_COMMON)&s);
+   if (!PAL_LoadGame_Common(szFileName, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_DOS)))
+	   return -1;
+
    //
    // Convert the DOS-style data structure to WIN-style data structure
    //
@@ -629,12 +645,7 @@ PAL_LoadGame_DOS(
          gpGlobals->g.rgObject[i].rgwData[6] = 0;
       }
    }
-   memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject,
-      sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
-
-   gpGlobals->fEnteringScene = FALSE;
-
-   PAL_CompressInventory();
+   memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
 
    //
    // Success
@@ -661,47 +672,16 @@ PAL_LoadGame_WIN(
 
 --*/
 {
-   FILE                     *fp;
    PAL_LARGE SAVEDGAME_WIN   s;
 
-   //
-   // Try to open the specified file
-   //
-   fp = fopen(szFileName, "rb");
-   if (fp == NULL)
-   {
-      return -1;
-   }
-
-   //
-   // Read all data from the file and close.
-   //
-   fread(&s, sizeof(SAVEDGAME_WIN), 1, fp);
-   fclose(fp);
-
-   //
-   // Adjust endianness
-   //
-   DO_BYTESWAP(&s, sizeof(SAVEDGAME_WIN));
-
-   //
-   // Cash amount is in DWORD, so do a wordswap in Big-Endian.
-   //
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-   s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
-#endif
-
    //
    // Get all the data from the saved game struct.
    //
-   PAL_LoadGame_Common((LPSAVEDGAME_COMMON)&s);
-   memcpy(gpGlobals->g.rgObject, s.rgObject, sizeof(gpGlobals->g.rgObject));
-   memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject,
-      sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
+   if (!PAL_LoadGame_Common(szFileName, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_WIN)))
+	   return -1;
 
-   gpGlobals->fEnteringScene = FALSE;
-
-   PAL_CompressInventory();
+   memcpy(gpGlobals->g.rgObject, s.rgObject, sizeof(gpGlobals->g.rgObject));
+   memcpy(gpGlobals->g.lprgEventObject, s.rgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
 
    //
    // Success
@@ -719,9 +699,16 @@ PAL_LoadGame(
 
 static VOID
 PAL_SaveGame_Common(
-    const LPSAVEDGAME_COMMON s
+	LPCSTR             szFileName,
+	WORD               wSavedTimes,
+	LPSAVEDGAME_COMMON s,
+	size_t             size
 )
 {
+	FILE *fp;
+	int   i;
+
+	s->wSavedTimes = wSavedTimes;
 	s->wViewportX = PAL_X(gpGlobals->viewport);
 	s->wViewportY = PAL_Y(gpGlobals->viewport);
 	s->nPartyMember = gpGlobals->wMaxPartyMemberIndex;
@@ -751,6 +738,32 @@ PAL_SaveGame_Common(
 	memcpy(s->rgPoisonStatus, gpGlobals->rgPoisonStatus, sizeof(gpGlobals->rgPoisonStatus));
 	memcpy(s->rgInventory, gpGlobals->rgInventory, sizeof(gpGlobals->rgInventory));
 	memcpy(s->rgScene, gpGlobals->g.rgScene, sizeof(gpGlobals->g.rgScene));
+
+	//
+	// Adjust endianness
+	//
+	DO_BYTESWAP(&s, size);
+
+	//
+	// Cash amount is in DWORD, so do a wordswap in Big-Endian.
+	//
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+	s->dwCash = ((s->dwCash >> 16) | (s->dwCash << 16));
+#endif
+
+	//
+	// Try writing to file
+	//
+	if ((fp = fopen(szFileName, "wb")) == NULL)
+	{
+		return;
+	}
+
+	i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
+	i += size - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
+
+	fwrite(&s, i, 1, fp);
+	fclose(fp);
 }
 
 static VOID
@@ -773,14 +786,9 @@ PAL_SaveGame_DOS(
 
 --*/
 {
-   FILE                     *fp;
    PAL_LARGE SAVEDGAME_DOS   s;
    UINT32                    i;
 
-   //
-   // Put all the data to the saved game struct.
-   //
-   PAL_SaveGame_Common((LPSAVEDGAME_COMMON)&s);
    //
    // Convert the WIN-style data structure to DOS-style data structure
    //
@@ -792,37 +800,12 @@ PAL_SaveGame_DOS(
          s.rgObject[i].rgwData[5] = gpGlobals->g.rgObject[i].rgwData[6];     // wFlags
 	  }
    }
-   memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject,
-      sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
-
-   s.wSavedTimes = wSavedTimes;
-
-   //
-   // Adjust endianness
-   //
-   DO_BYTESWAP(&s, sizeof(SAVEDGAME));
+   memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
 
    //
-   // Cash amount is in DWORD, so do a wordswap in Big-Endian.
-   //
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-   s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
-#endif
-
-   //
-   // Try writing to file
+   // Put all the data to the saved game struct.
    //
-   fp = fopen(szFileName, "wb");
-   if (fp == NULL)
-   {
-      return;
-   }
-
-   i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
-   i += sizeof(SAVEDGAME_DOS) - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
-
-   fwrite(&s, i, 1, fp);
-   fclose(fp);
+   PAL_SaveGame_Common(szFileName, wSavedTimes, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_DOS));
 }
 
 static VOID
@@ -845,46 +828,15 @@ PAL_SaveGame_WIN(
 
 --*/
 {
-   FILE                     *fp;
    PAL_LARGE SAVEDGAME_WIN   s;
-   UINT32                    i;
 
    //
    // Put all the data to the saved game struct.
    //
-   PAL_SaveGame_Common((LPSAVEDGAME_COMMON)&s);
    memcpy(s.rgObject, gpGlobals->g.rgObject, sizeof(gpGlobals->g.rgObject));
-   memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject,
-      sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
-
-   s.wSavedTimes = wSavedTimes;
-
-   //
-   // Adjust endianness
-   //
-   DO_BYTESWAP(&s, sizeof(SAVEDGAME));
-
-   //
-   // Cash amount is in DWORD, so do a wordswap in Big-Endian.
-   //
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-   s.dwCash = ((s.dwCash >> 16) | (s.dwCash << 16));
-#endif
-
-   //
-   // Try writing to file
-   //
-   fp = fopen(szFileName, "wb");
-   if (fp == NULL)
-   {
-      return;
-   }
-
-   i = PAL_MKFGetChunkSize(0, gpGlobals->f.fpSSS);
-   i += sizeof(SAVEDGAME_WIN) - sizeof(EVENTOBJECT) * MAX_EVENT_OBJECTS;
+   memcpy(s.rgEventObject, gpGlobals->g.lprgEventObject, sizeof(EVENTOBJECT) * gpGlobals->g.nEventObject);
 
-   fwrite(&s, i, 1, fp);
-   fclose(fp);
+   PAL_SaveGame_Common(szFileName, wSavedTimes, (LPSAVEDGAME_COMMON)&s, sizeof(SAVEDGAME_WIN));
 }
 
 VOID

+ 1 - 1
libmad/music_mad.c

@@ -226,7 +226,7 @@ decode_frame(mad_data *mp3_mad) {
     lo = (int)mp3_mad->frame.header.samplerate < mp3_mad->mixer.freq ? mp3_mad->frame.header.samplerate : mp3_mad->mixer.freq;
 	if (hi != lo) {
       /* Need sample rate conversion, resampler should be used. Try to create resamplers. */
-      if (mp3_mad->resampler[0] = resampler_create()) {
+      if ((mp3_mad->resampler[0] = resampler_create()) != NULL) {
         if (mp3_mad->mixer.channels == 2) {
           if ((mp3_mad->resampler[1] = resampler_create())) {
             resampler_set_quality(mp3_mad->resampler[1], (hi % lo == 0) ? RESAMPLER_QUALITY_MIN : mp3_mad->resampler_quality);

+ 4 - 0
liboggvorbis/src/info.c

@@ -18,6 +18,10 @@
 /* general handling of the header and the vorbis_info structure (and
    substructures) */
 
+#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>

+ 4 - 0
liboggvorbis/src/vorbisfile.c

@@ -15,6 +15,10 @@
 
  ********************************************************************/
 
+#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>

+ 15 - 10
mp3play.c

@@ -23,13 +23,12 @@
 #include "util.h"
 #include "global.h"
 #include "palcfg.h"
-
-#if PAL_HAS_MP3
-
 #include "audio.h"
 #include "players.h"
-#include "resampler.h"
+
+#if PAL_HAS_MP3
 #include "libmad/music_mad.h"
+#include "resampler.h"
 
 typedef struct tagMP3PLAYER
 {
@@ -142,7 +141,7 @@ MP3_Init(
 )
 {
 	LPMP3PLAYER player;
-	if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
+	if ((player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER))) != NULL)
 	{
 		player->FillBuffer = MP3_FillBuffer;
 		player->Play = MP3_Play;
@@ -151,12 +150,18 @@ MP3_Init(
 		player->pMP3 = NULL;
 		player->iMusic = -1;
 		player->fLoop = FALSE;
-		return (LPAUDIOPLAYER)player;
-	}
-	else
-	{
-		return NULL;
 	}
+	return (LPAUDIOPLAYER)player;
+}
+
+#else
+
+LPAUDIOPLAYER
+MP3_Init(
+	VOID
+)
+{
+	return NULL;
 }
 
 #endif

+ 16 - 10
oggplay.c

@@ -29,9 +29,9 @@
 #include "players.h"
 #include "audio.h"
 #include <math.h>
+
 #if PAL_HAS_OGG
 #include <vorbis/vorbisfile.h>
-
 #include "resampler.h"
 
 #define FLAG_OY 0x01
@@ -236,8 +236,9 @@ static BOOL OGG_Rewind(LPOGGPLAYER player)
 																for vd here */
 		player->iStage = STAGE_PAGEOUT;
 		player->iFlags |= FLAG_VD | FLAG_VB;
+		player->fUseResampler = (player->vi.rate != gConfig.iSampleRate);
 
-		if (player->fUseResampler = player->vi.rate != gConfig.iSampleRate) {
+		if (player->fUseResampler) {
 			double factor = (double)player->vi.rate / (double)gConfig.iSampleRate;
 			for (i = 0; i < min(player->vi.channels, 2); i++)
 			{
@@ -477,9 +478,9 @@ OGG_Init(
 )
 {
 	LPOGGPLAYER player;
-	if (player = (LPOGGPLAYER)malloc(sizeof(OGGPLAYER)))
+	if ((player = (LPOGGPLAYER)malloc(sizeof(OGGPLAYER))) != NULL)
 	{
-		memset(player, 0, sizeof(LPOGGPLAYER));
+		memset(player, 0, sizeof(OGGPLAYER));
 
 		player->FillBuffer = OGG_FillBuffer;
 		player->Play = OGG_Play;
@@ -503,13 +504,18 @@ OGG_Init(
 				player->resampler[0] = NULL;
 			}
 		}
-
-		return (LPAUDIOPLAYER)player;
-	}
-	else
-	{
-		return NULL;
 	}
+	return (LPAUDIOPLAYER)player;
+}
+
+#else
+
+LPAUDIOPLAYER
+OGG_Init(
+	VOID
+)
+{
+	return NULL;
 }
 
 #endif

+ 31 - 35
palcfg.c

@@ -33,7 +33,7 @@
 #define PAL_HAS_TOUCH     0
 #endif
 
-#define MAKE_VALUE(defv, minv, maxv) (LPCSTR)(defv), (LPCSTR)(minv), (LPCSTR)(maxv)
+#define MAKE_VALUE(defv, minv, maxv) {(LPCSTR)(defv)}, {(LPCSTR)(minv)}, {(LPCSTR)(maxv)}
 
 static const ConfigItem gConfigItems[PALCFG_ALL_MAX] = {
 	{ PALCFG_FULLSCREEN,        PALCFG_BOOLEAN,  "FullScreen",        10, MAKE_VALUE(FALSE,                         FALSE,                 TRUE) },
@@ -55,14 +55,14 @@ static const ConfigItem gConfigItems[PALCFG_ALL_MAX] = {
 	{ PALCFG_WINDOWHEIGHT,      PALCFG_UNSIGNED, "WindowHeight",      12, MAKE_VALUE(PAL_DEFAULT_WINDOW_HEIGHT,     0,                     UINT32_MAX) },
 	{ PALCFG_WINDOWWIDTH,       PALCFG_UNSIGNED, "WindowWidth",       11, MAKE_VALUE(PAL_DEFAULT_WINDOW_WIDTH,      0,                     UINT32_MAX) },
 
-	{ PALCFG_CD,                PALCFG_STRING,   "CD",                 2, "OGG",    NULL, NULL },
-	{ PALCFG_GAMEPATH,          PALCFG_STRING,   "GamePath",           8, NULL,     NULL, NULL },
-	{ PALCFG_SAVEPATH,          PALCFG_STRING,   "SavePath",           8, NULL,     NULL, NULL },
-	{ PALCFG_MESSAGEFILE,       PALCFG_STRING,   "MessageFileName",   15, NULL,     NULL, NULL },
-	{ PALCFG_BDFFILE,           PALCFG_STRING,   "BDFFileName",       11, NULL,     NULL, NULL },
-	{ PALCFG_MUSIC,             PALCFG_STRING,   "Music",              5, "RIX",    NULL, NULL },
-	{ PALCFG_OPL,               PALCFG_STRING,   "OPL",                3, "DOSBOX", NULL, NULL },
-	{ PALCFG_RIXEXTRAINIT,      PALCFG_STRING,   "RIXExtraInit",      12, NULL,     NULL, NULL },
+	{ PALCFG_CD,                PALCFG_STRING,   "CD",                 2, MAKE_VALUE("OGG",    NULL, NULL) },
+	{ PALCFG_GAMEPATH,          PALCFG_STRING,   "GamePath",           8, MAKE_VALUE(NULL,     NULL, NULL) },
+	{ PALCFG_SAVEPATH,          PALCFG_STRING,   "SavePath",           8, MAKE_VALUE(NULL,     NULL, NULL) },
+	{ PALCFG_MESSAGEFILE,       PALCFG_STRING,   "MessageFileName",   15, MAKE_VALUE(NULL,     NULL, NULL) },
+	{ PALCFG_BDFFILE,           PALCFG_STRING,   "BDFFileName",       11, MAKE_VALUE(NULL,     NULL, NULL) },
+	{ PALCFG_MUSIC,             PALCFG_STRING,   "Music",              5, MAKE_VALUE("RIX",    NULL, NULL) },
+	{ PALCFG_OPL,               PALCFG_STRING,   "OPL",                3, MAKE_VALUE("DOSBOX", NULL, NULL) },
+	{ PALCFG_RIXEXTRAINIT,      PALCFG_STRING,   "RIXExtraInit",      12, MAKE_VALUE(NULL,     NULL, NULL) },
 };
 
 
@@ -98,7 +98,7 @@ PAL_ParseConfigLine(
 	if (*line && *line != '#')
 	{
 		const char *ptr;
-		if (ptr = strchr(line, '='))
+		if ((ptr = strchr(line, '=')) != NULL)
 		{
 			const char *end = ptr++;
 
@@ -117,37 +117,33 @@ PAL_ParseConfigLine(
 					if (ppItem) *ppItem = &gConfigItems[i];
 					if (pValue)
 					{
-						if (gConfigItems[i].Type != PALCFG_STRING)
-						{
-							switch (gConfigItems[i].Type)
-							{
-							case PALCFG_UNSIGNED:
-								sscanf(ptr, "%u", &pValue->uValue);
-								if (pValue->uValue < gConfigItems[i].MinValue.uValue)
-									pValue->uValue = gConfigItems[i].MinValue.uValue;
-								else if (pValue->uValue > gConfigItems[i].MaxValue.uValue)
-									pValue->uValue = gConfigItems[i].MaxValue.uValue;
-								break;
-							case PALCFG_INTEGER:
-								sscanf(ptr, "%d", &pValue->iValue);
-								if (pValue->iValue < gConfigItems[i].MinValue.iValue)
-									pValue->iValue = gConfigItems[i].MinValue.iValue;
-								else if (pValue->iValue > gConfigItems[i].MaxValue.iValue)
-									pValue->iValue = gConfigItems[i].MaxValue.iValue;
-								break;
-							case PALCFG_BOOLEAN:
-								sscanf(ptr, "%d", &pValue->bValue);
-								pValue->bValue = pValue->bValue ? TRUE : FALSE;
-								break;
-							}
-						}
-						else
+						switch (gConfigItems[i].Type)
 						{
+						case PALCFG_UNSIGNED:
+							sscanf(ptr, "%u", &pValue->uValue);
+							if (pValue->uValue < gConfigItems[i].MinValue.uValue)
+								pValue->uValue = gConfigItems[i].MinValue.uValue;
+							else if (pValue->uValue > gConfigItems[i].MaxValue.uValue)
+								pValue->uValue = gConfigItems[i].MaxValue.uValue;
+							break;
+						case PALCFG_INTEGER:
+							sscanf(ptr, "%d", &pValue->iValue);
+							if (pValue->iValue < gConfigItems[i].MinValue.iValue)
+								pValue->iValue = gConfigItems[i].MinValue.iValue;
+							else if (pValue->iValue > gConfigItems[i].MaxValue.iValue)
+								pValue->iValue = gConfigItems[i].MaxValue.iValue;
+							break;
+						case PALCFG_BOOLEAN:
+							sscanf(ptr, "%d", &pValue->bValue);
+							pValue->bValue = pValue->bValue ? TRUE : FALSE;
+							break;
+						case PALCFG_STRING:
 							//
 							// Skip leading spaces
 							//
 							while (*ptr && isspace(*ptr)) ptr++;
 							pValue->sValue = ptr;
+							break;
 						}
 						return TRUE;
 					}

+ 14 - 17
palcommon.c

@@ -697,10 +697,10 @@ PAL_MKFGetChunkCount(
    }
 
    fseek(fp, 0, SEEK_SET);
-   fread(&iNumChunk, sizeof(INT), 1, fp);
-
-   iNumChunk = (SDL_SwapLE32(iNumChunk) - 4) / 4;
-   return iNumChunk;
+   if (fread(&iNumChunk, sizeof(INT), 1, fp) == 1)
+      return (SDL_SwapLE32(iNumChunk) - 4) >> 2;
+   else
+      return 0;
 }
 
 INT
@@ -743,8 +743,9 @@ PAL_MKFGetChunkSize(
    // Get the offset of the specified chunk and the next chunk.
    //
    fseek(fp, 4 * uiChunkNum, SEEK_SET);
-   fread(&uiOffset, sizeof(UINT), 1, fp);
-   fread(&uiNextOffset, sizeof(UINT), 1, fp);
+   PAL_fread(&uiOffset, sizeof(UINT), 1, fp);
+   PAL_fread(&uiNextOffset, sizeof(UINT), 1, fp);
+
    uiOffset = SDL_SwapLE32(uiOffset);
    uiNextOffset = SDL_SwapLE32(uiNextOffset);
 
@@ -807,8 +808,8 @@ PAL_MKFReadChunk(
    // Get the offset of the chunk.
    //
    fseek(fp, 4 * uiChunkNum, SEEK_SET);
-   fread(&uiOffset, 4, 1, fp);
-   fread(&uiNextOffset, 4, 1, fp);
+   PAL_fread(&uiOffset, 4, 1, fp);
+   PAL_fread(&uiNextOffset, 4, 1, fp);
    uiOffset = SDL_SwapLE32(uiOffset);
    uiNextOffset = SDL_SwapLE32(uiNextOffset);
 
@@ -825,14 +826,10 @@ PAL_MKFReadChunk(
    if (uiChunkLen != 0)
    {
       fseek(fp, uiOffset, SEEK_SET);
-      fread(lpBuffer, uiChunkLen, 1, fp);
-   }
-   else
-   {
-      return -1;
+      return (int)fread(lpBuffer, 1, uiChunkLen, fp);
    }
 
-   return (INT)uiChunkLen;
+   return -1;
 }
 
 INT
@@ -880,7 +877,7 @@ PAL_MKFGetDecompressedSize(
    // Get the offset of the chunk.
    //
    fseek(fp, 4 * uiChunkNum, SEEK_SET);
-   fread(&uiOffset, 4, 1, fp);
+   PAL_fread(&uiOffset, 4, 1, fp);
    uiOffset = SDL_SwapLE32(uiOffset);
 
    //
@@ -889,14 +886,14 @@ PAL_MKFGetDecompressedSize(
    fseek(fp, uiOffset, SEEK_SET);
    if (gConfig.fIsWIN95)
    {
-      fread(buf, sizeof(DWORD), 1, fp);
+      PAL_fread(buf, sizeof(DWORD), 1, fp);
       buf[0] = SDL_SwapLE32(buf[0]);
 
       return (INT)buf[0];
    }
    else
    {
-      fread(buf, sizeof(DWORD), 2, fp);
+      PAL_fread(buf, sizeof(DWORD), 2, fp);
       buf[0] = SDL_SwapLE32(buf[0]);
       buf[1] = SDL_SwapLE32(buf[1]);
 

+ 0 - 6
players.h

@@ -48,25 +48,19 @@ RIX_Init(
 );
 
 /* OGG */
-#if PAL_HAS_OGG
 
 LPAUDIOPLAYER
 OGG_Init(
 	VOID
 );
 
-#endif
-
 /* MP3 */
-#if PAL_HAS_MP3
 
 LPAUDIOPLAYER
 MP3_Init(
 	VOID
 );
 
-#endif
-
 LPAUDIOPLAYER
 SOUND_Init(
 	VOID

+ 7 - 11
rngplay.c

@@ -82,8 +82,8 @@ PAL_RNGReadFrame(
    // Get the offset of the chunk.
    //
    fseek(fpRngMKF, 4 * uiRngNum, SEEK_SET);
-   fread(&uiOffset, sizeof(UINT), 1, fpRngMKF);
-   fread(&uiNextOffset, sizeof(UINT), 1, fpRngMKF);
+   PAL_fread(&uiOffset, sizeof(UINT), 1, fpRngMKF);
+   PAL_fread(&uiNextOffset, sizeof(UINT), 1, fpRngMKF);
    uiOffset = SDL_SwapLE32(uiOffset);
    uiNextOffset = SDL_SwapLE32(uiNextOffset);
 
@@ -103,7 +103,7 @@ PAL_RNGReadFrame(
    //
    // Get the number of sub chunks.
    //
-   fread(&uiChunkCount, sizeof(UINT), 1, fpRngMKF);
+   PAL_fread(&uiChunkCount, sizeof(UINT), 1, fpRngMKF);
    uiChunkCount = (SDL_SwapLE32(uiChunkCount) - 4) / 4;
    if (uiFrameNum >= uiChunkCount)
    {
@@ -114,8 +114,8 @@ PAL_RNGReadFrame(
    // Get the offset of the sub chunk.
    //
    fseek(fpRngMKF, uiOffset + 4 * uiFrameNum, SEEK_SET);
-   fread(&uiSubOffset, sizeof(UINT), 1, fpRngMKF);
-   fread(&uiNextOffset, sizeof(UINT), 1, fpRngMKF);
+   PAL_fread(&uiSubOffset, sizeof(UINT), 1, fpRngMKF);
+   PAL_fread(&uiNextOffset, sizeof(UINT), 1, fpRngMKF);
    uiSubOffset = SDL_SwapLE32(uiSubOffset);
    uiNextOffset = SDL_SwapLE32(uiNextOffset);
 
@@ -131,14 +131,10 @@ PAL_RNGReadFrame(
    if (iChunkLen != 0)
    {
       fseek(fpRngMKF, uiOffset + uiSubOffset, SEEK_SET);
-      fread(lpBuffer, iChunkLen, 1, fpRngMKF);
-   }
-   else
-   {
-      return -1;
+      return (int)fread(lpBuffer, 1, iChunkLen, fpRngMKF);
    }
 
-   return iChunkLen;
+   return -1;
 }
 
 static INT

+ 11 - 11
sound.c

@@ -126,17 +126,17 @@ SOUND_LoadWAVEData(
 --*/
 {
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
-#	define RIFF		'RIFF'
-#	define WAVE		'WAVE'
-#	define FMT		'fmt '
-#	define DATA		'data'
-#	define PCM     0x0100
+#	define RIFF		0x52494646 // 'RIFF'
+#	define WAVE		0x57415645 // 'WAVE'
+#	define FMT		0x666D7420 // 'fmt '
+#	define DATA		0x64617461 // 'data'
+#	define PCM      0x0100
 #else
-#	define RIFF		'FFIR'
-#	define WAVE		'EVAW'
-#	define FMT		' tmf'
-#	define DATA		'atad'
-#	define PCM     0x0001
+#	define RIFF		0x46464952 // 'FFIR'
+#	define WAVE		0x45564157 // 'EVAW'
+#	define FMT		0x20746D66 // ' tmf'
+#	define DATA		0x61746164 // 'atad'
+#	define PCM      0x0001
 #endif
 	LPCRIFFHEADER lpRiff = (LPCRIFFHEADER)lpData;
 	LPCRIFFCHUNK lpChunk;
@@ -916,7 +916,7 @@ SOUND_Shutdown(
 			if (cursnd->resampler[0]) resampler_delete(cursnd->resampler[0]);
 			if (cursnd->resampler[1]) resampler_delete(cursnd->resampler[1]);
 			if (cursnd->base) free((void *)cursnd->base);
-		} while (cursnd = cursnd->next);
+		} while ((cursnd = cursnd->next) != NULL);
 		cursnd = player->soundlist.next;
 		while (cursnd)
 		{

+ 22 - 7
text.c

@@ -25,6 +25,7 @@
 
 #include "main.h"
 #include <errno.h>
+#include <wctype.h>
 
 #define   FONT_COLOR_DEFAULT        0x4F
 #define   FONT_COLOR_YELLOW         0x2D
@@ -208,7 +209,7 @@ PAL_ReadMessageFile(
 	while (!feof(fp))
 	{
 		char *buffer;
-		if (buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp))
+		if ((buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp)) != NULL)
 		{
 			switch(state)
 			{
@@ -571,7 +572,12 @@ PAL_InitText(
 		   return -1;
 	   }
 	   fseek(fpWord, 0, SEEK_SET);
-	   fread(temp, i, 1, fpWord);
+	   if (fread(temp, 1, i, fpWord) < (size_t)i)
+	   {
+		   fclose(fpWord);
+		   fclose(fpMsg);
+		   return -1;
+	   }
 	   memset(temp + i, 0, gConfig.dwWordLength * g_TextLib.nWords - i);
 
 	   //
@@ -591,7 +597,6 @@ PAL_InitText(
 	   if (g_TextLib.lpWordBuf == NULL)
 	   {
 		   free(temp);
-		   fclose(fpWord);
 		   fclose(fpMsg);
 		   return -1;
 	   }
@@ -600,7 +605,6 @@ PAL_InitText(
 	   {
 		   free(g_TextLib.lpWordBuf);
 		   free(temp);
-		   fclose(fpWord);
 		   fclose(fpMsg);
 		   return -1;
 	   }
@@ -625,6 +629,7 @@ PAL_InitText(
 	   offsets = (LPDWORD)malloc(i * sizeof(DWORD));
 	   if (offsets == NULL)
 	   {
+		   free(g_TextLib.lpWordBuf[0]);
 		   free(g_TextLib.lpWordBuf);
 		   fclose(fpMsg);
 		   return -1;
@@ -649,7 +654,14 @@ PAL_InitText(
 	   }
 
 	   fseek(fpMsg, 0, SEEK_SET);
-	   fread(temp, 1, i, fpMsg);
+	   if (fread(temp, 1, i, fpMsg) < (size_t)i)
+	   {
+		   free(offsets);
+		   free(g_TextLib.lpWordBuf[0]);
+		   free(g_TextLib.lpWordBuf);
+		   fclose(fpMsg);
+		   return -1;
+	   }
 
 	   fclose(fpMsg);
 
@@ -661,6 +673,7 @@ PAL_InitText(
 	   g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
 	   if (g_TextLib.lpMsgBuf == NULL)
 	   {
+		   free(g_TextLib.lpWordBuf[0]);
 		   free(g_TextLib.lpWordBuf);
 		   free(offsets);
 		   return -1;
@@ -669,6 +682,7 @@ PAL_InitText(
 	   if (tmp == NULL)
 	   {
 		   free(g_TextLib.lpMsgBuf);
+		   free(g_TextLib.lpWordBuf[0]);
 		   free(g_TextLib.lpWordBuf);
 		   free(offsets);
 		   return -1;
@@ -1828,8 +1842,9 @@ PAL_swprintf(
 	LPCWSTR fmt_start = NULL;
 	LPWSTR cur_fmt = NULL;
 	size_t fmt_len = 0;
-	int precision, width;
-	int state, left_aligned, wide, narrow, width_var, precision_var, precision_defined;
+	int state, precision = 0, width = 0;
+	int left_aligned = 0, wide = 0, narrow = 0;
+	int width_var = 0, precision_var = 0, precision_defined = 0;
 
 	// Buffer & length check
 	if (buffer == NULL || format == NULL)

+ 22 - 29
uigame.c

@@ -22,6 +22,21 @@
 
 #include "main.h"
 
+static WORD GetSavedTimes(int saveslot)
+{
+	FILE *fp = fopen(va("%s%d%s", gConfig.pszSavePath, saveslot, ".rpg"), "rb");
+	WORD wSavedTimes = 0;
+	if (fp != NULL)
+	{
+		if (fread(&wSavedTimes, sizeof(WORD), 1, fp) == 1)
+			wSavedTimes = SDL_SwapLE16(wSavedTimes);
+		else
+			wSavedTimes = 0;
+		fclose(fp);
+	}
+	return wSavedTimes;
+}
+
 VOID
 PAL_DrawOpeningMenuBackground(
    VOID
@@ -166,9 +181,7 @@ PAL_SaveSlotMenu(
    LPBOX           rgpBox[5];
    int             i, w = PAL_WordMaxWidth(LOADMENU_LABEL_SLOT_FIRST, 5);
    int             dx = (w > 4) ? (w - 4) * 16 : 0;
-   FILE           *fp;
    WORD            wItemSelected;
-   WORD            wSavedTimes;
 
    MENUITEM        rgMenuItem[5];
 
@@ -193,22 +206,10 @@ PAL_SaveSlotMenu(
    //
    for (i = 1; i <= 5; i++)
    {
-      fp = fopen(va("%s%d%s", gConfig.pszSavePath, i, ".rpg"), "rb");
-      if (fp == NULL)
-      {
-         wSavedTimes = 0;
-      }
-      else
-      {
-         fread(&wSavedTimes, sizeof(WORD), 1, fp);
-         wSavedTimes = SDL_SwapLE16(wSavedTimes);
-         fclose(fp);
-      }
-
       //
       // Draw the number
       //
-      PAL_DrawNumber((UINT)wSavedTimes, 4, PAL_XY(270, 38 * i - 17),
+      PAL_DrawNumber((UINT)GetSavedTimes(i), 4, PAL_XY(270, 38 * i - 17),
          kNumColorYellow, kNumAlignRight);
    }
 
@@ -531,8 +532,7 @@ PAL_SystemMenu(
 {
    LPBOX               lpMenuBox;
    WORD                wReturnValue;
-   int                 iSlot, i, iSavedTimes;
-   FILE               *fp;
+   int                 iSlot, i;
    const SDL_Rect      rect = {40, 60, 280, 135};
 
    //
@@ -583,25 +583,18 @@ PAL_SystemMenu(
 
       if (iSlot != MENUITEM_VALUE_CANCELLED)
       {
+         WORD wSavedTimes = 0;
          gpGlobals->bCurrentSaveSlot = (BYTE)iSlot;
 
-         iSavedTimes = 0;
          for (i = 1; i <= 5; i++)
          {
-            fp = fopen(va("%s%d%s", gConfig.pszSavePath, i, ".rpg"), "rb");
-            if (fp != NULL)
+            WORD curSavedTimes = GetSavedTimes(i);
+            if (curSavedTimes > wSavedTimes)
             {
-               WORD wSavedTimes;
-               fread(&wSavedTimes, sizeof(WORD), 1, fp);
-               fclose(fp);
-               wSavedTimes = SDL_SwapLE16(wSavedTimes);
-               if ((int)wSavedTimes > iSavedTimes)
-               {
-                  iSavedTimes = wSavedTimes;
-               }
+               wSavedTimes = curSavedTimes;
             }
          }
-         PAL_SaveGame(va("%s%d%s", gConfig.pszSavePath, iSlot, ".rpg"), iSavedTimes + 1);
+         PAL_SaveGame(va("%s%d%s", gConfig.pszSavePath, iSlot, ".rpg"), wSavedTimes + 1);
       }
       break;
 

+ 1 - 1
unix/native_midi.cpp

@@ -74,7 +74,7 @@ extern "C" NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
     {
         char buf[4096];
         size_t bytes;
-        while(bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf)))
+        while((bytes = SDL_RWread(rw, buf, sizeof(char), sizeof(buf))) > 0)
             fwrite(buf, sizeof(char), bytes, fp);
         fclose(fp);
 

+ 2 - 0
unix/pal_config.h

@@ -58,4 +58,6 @@
 # define PAL_HAS_CONFIG_PAGE  1
 # define PAL_HAS_NATIVEMIDI 1
 
+#include <sys/time.h>
+
 #endif

+ 2 - 0
win32/native_midi.cpp

@@ -217,6 +217,8 @@ static void MIDItoStream(NativeMidiSong *song, MIDIEvent *eventlist)
 				if (event->data[0] == 0x51)
 					tempo = (event->extraData[0] << 16) | (event->extraData[1] << 8) | event->extraData[2];
 				break;
+			default:
+				break;
 			}
 			break;
 		}