Fix memory corruption bugs in structure limits code. Reported by several.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@3865 4a71c877-e1ca-e34f-864e-861f7616d084
master
Per Inge Mathisen 2008-02-24 15:31:50 +00:00
parent f92b8fc5b4
commit d0a0b225ef
1 changed files with 9 additions and 31 deletions

View File

@ -51,11 +51,6 @@
#include "lib/ivis_common/piemode.h" #include "lib/ivis_common/piemode.h"
#include "lib/script/script.h" #include "lib/script/script.h"
// ////////////////////////////////////////////////////////////////////////////
// externs
extern void intDisplayPlainForm (WIDGET *psWidget, UDWORD xOffset,
UDWORD yOffset, PIELIGHT *pColours);
// //////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////
// defines // defines
#define IDLIMITS 22000 #define IDLIMITS 22000
@ -76,6 +71,7 @@ extern void intDisplayPlainForm (WIDGET *psWidget, UDWORD xOffset,
#define BARWIDTH 480 #define BARWIDTH 480
#define BARHEIGHT 40 #define BARHEIGHT 40
#define BUTPERFORM 8 #define BUTPERFORM 8
// //////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////
// protos. // protos.
@ -85,7 +81,7 @@ static void displayStructureBar(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset
static BOOL useStruct(UDWORD count,UDWORD i) static BOOL useStruct(UDWORD count,UDWORD i)
{ {
// STRUCTURE_STATS *pStat = asStructureStats+i; ASSERT(i < numStructureStats, "Bad structure for structure limit: %d", (int)i);
if(count >= (4*BUTPERFORM)) if(count >= (4*BUTPERFORM))
{ {
@ -144,20 +140,6 @@ BOOL startLimitScreen(void)
widgAddForm(psWScreen, &sFormInit); widgAddForm(psWScreen, &sFormInit);
// return button. // return button.
// addMultiBut(psWScreen,IDLIMITS,IDLIMITS_RETURN,
// 8,5,
// iV_GetImageWidth(FrontImages,IMAGE_RETURN),
// iV_GetImageHeight(FrontImages,IMAGE_RETURN),
// _("Return To Previous Screen"),IMAGE_RETURN,IMAGE_RETURN_HI,TRUE);
// ok button
// addMultiBut(psWScreen,IDLIMITS,IDLIMITS_OK,
// LIMITS_OKX,LIMITS_OKY,
// iV_GetImageWidth(FrontImages,IMAGE_BIGOK),
// iV_GetImageHeight(FrontImages,IMAGE_BIGOK),
// _("Accept Settings"),IMAGE_BIGOK,IMAGE_BIGOK,TRUE);
addMultiBut(psWScreen,IDLIMITS,IDLIMITS_RETURN, addMultiBut(psWScreen,IDLIMITS,IDLIMITS_RETURN,
LIMITS_OKX-40,LIMITS_OKY, LIMITS_OKX-40,LIMITS_OKY,
iV_GetImageWidth(FrontImages,IMAGE_RETURN), iV_GetImageWidth(FrontImages,IMAGE_RETURN),
@ -172,8 +154,6 @@ BOOL startLimitScreen(void)
iV_GetImageHeight(FrontImages,IMAGE_BIGOK), iV_GetImageHeight(FrontImages,IMAGE_BIGOK),
_("Accept Settings"),IMAGE_OK,IMAGE_OK,TRUE); _("Accept Settings"),IMAGE_OK,IMAGE_OK,TRUE);
// Count the number of minor tabs needed // Count the number of minor tabs needed
numButtons = 0; numButtons = 0;
@ -240,7 +220,6 @@ BOOL startLimitScreen(void)
asStructLimits[0][i].limit, 0); asStructLimits[0][i].limit, 0);
sButInit.id ++; sButInit.id ++;
if (sButInit.y + BARHEIGHT + 2 > (BUTPERFORM*(BARHEIGHT+2) - 4) ) if (sButInit.y + BARHEIGHT + 2 > (BUTPERFORM*(BARHEIGHT+2) - 4) )
{ {
sButInit.y = 5; sButInit.y = 5;
@ -283,14 +262,11 @@ void runLimitScreen(void)
case IDLIMITS_RETURN: case IDLIMITS_RETURN:
// reset the sliders.. // reset the sliders..
eventReset(); eventReset();
// resReleaseBlockData(500);
resReleaseBlockData(501); resReleaseBlockData(501);
resReleaseBlockData(502); resReleaseBlockData(502);
// eventReset();
bForceEditorLoaded = FALSE; bForceEditorLoaded = FALSE;
changeTitleMode(MULTIOPTION); changeTitleMode(MULTIOPTION);
// make some noize. // make some noize.
if(!ingame.localOptionsReceived) if(!ingame.localOptionsReceived)
{ {
@ -301,7 +277,6 @@ void runLimitScreen(void)
sendTextMessage("Limits Reset To Default Values",TRUE); sendTextMessage("Limits Reset To Default Values",TRUE);
} }
break; break;
case IDLIMITS_OK: case IDLIMITS_OK:
createLimitSet(); createLimitSet();
@ -318,7 +293,7 @@ void runLimitScreen(void)
// //////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////
void createLimitSet(void) void createLimitSet(void)
{ {
UDWORD i, numchanges = 0; UDWORD i, numchanges = 0, bufSize;
MULTISTRUCTLIMITS *pEntry; MULTISTRUCTLIMITS *pEntry;
// Free the old set if required // Free the old set if required
@ -340,9 +315,12 @@ void createLimitSet(void)
} }
// Allocate some memory for the changes // Allocate some memory for the changes
pEntry = malloc(numchanges * sizeof(MULTISTRUCTLIMITS)); bufSize = numStructureStats * sizeof(MULTISTRUCTLIMITS);
pEntry = malloc(bufSize);
memset(pEntry, 255, bufSize);
// Prepare chunk // Prepare chunk
ASSERT(numStructureStats < UBYTE_MAX, "Too many structure stats");
for (i = 0; i < numStructureStats; i++) for (i = 0; i < numStructureStats; i++)
{ {
if (asStructLimits[0][i].limit != LOTS_OF) if (asStructLimits[0][i].limit != LOTS_OF)
@ -355,7 +333,7 @@ void createLimitSet(void)
ingame.numStructureLimits = numchanges; ingame.numStructureLimits = numchanges;
ingame.pStructureLimits = pEntry; ingame.pStructureLimits = pEntry;
sendOptions(0,0); sendOptions(0, 0);
} }
// //////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////
@ -372,7 +350,7 @@ void applyLimitSet(void)
// Get the limits and decode // Get the limits and decode
for (i = 0; i < ingame.numStructureLimits; i++) for (i = 0; i < ingame.numStructureLimits; i++)
{ {
unsigned int id = pEntry[i].id; UBYTE id = pEntry[i].id;
// So long as the ID is valid // So long as the ID is valid
if (id < numStructureStats) if (id < numStructureStats)