From 3a10fe65db69dc9e900f6cb741692203cdabb02b Mon Sep 17 00:00:00 2001 From: Meep-Eep Date: Sat, 13 Feb 2010 22:15:38 +0000 Subject: [PATCH] Fix bounds checking errors relating to the fuel reserve aboard the flagship. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3530 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/uqm/outfit.c | 19 +++++++++---------- sc2/src/uqm/sis.c | 21 +++++++++++++++++---- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/sc2/src/uqm/outfit.c b/sc2/src/uqm/outfit.c index 280e59196..eaed262f5 100644 --- a/sc2/src/uqm/outfit.c +++ b/sc2/src/uqm/outfit.c @@ -102,7 +102,8 @@ RedistributeFuel (void) DWORD FuelVolume; RECT r; - if ((FuelVolume = GLOBAL_SIS (FuelOnBoard)) <= FUEL_RESERVE) + FuelVolume = GLOBAL_SIS (FuelOnBoard); + if (FuelVolume <= FUEL_RESERVE) return; GLOBAL_SIS (FuelOnBoard) = 0; @@ -559,18 +560,17 @@ ChangeFuelQuantity (void) LockMutex (GraphicsLock); SetContext (SpaceContext); if (GetFTankCapacity (&r.corner) > GLOBAL_SIS (FuelOnBoard) - && GLOBAL_SIS (ResUnits) >= - (DWORD)GLOBAL (FuelCost)) + && GLOBAL_SIS (ResUnits) >= (DWORD)GLOBAL (FuelCost)) { - if (GLOBAL_SIS (FuelOnBoard) >= - FUEL_RESERVE - FUEL_TANK_SCALE) + if (GLOBAL_SIS (FuelOnBoard) >= FUEL_RESERVE) { r.extent.width = 3; DrawPoint (&r.corner); r.corner.x += r.extent.width + 1; DrawPoint (&r.corner); r.corner.x -= r.extent.width; - SetContextForeGroundColor (SetContextBackGroundColor (BLACK_COLOR)); + SetContextForeGroundColor ( + SetContextBackGroundColor (BLACK_COLOR)); DrawFilledRectangle (&r); } DeltaSISGauges (0, FUEL_TANK_SCALE, -GLOBAL (FuelCost)); @@ -590,10 +590,9 @@ ChangeFuelQuantity (void) SetContext (SpaceContext); if (GLOBAL_SIS (FuelOnBoard)) { - DeltaSISGauges (0, -FUEL_TANK_SCALE, - GLOBAL (FuelCost)); - if (GLOBAL_SIS (FuelOnBoard) - % FUEL_VOLUME_PER_ROW == 0) + DeltaSISGauges (0, -FUEL_TANK_SCALE, GLOBAL (FuelCost)); + if (GLOBAL_SIS (FuelOnBoard) % FUEL_VOLUME_PER_ROW == 0 && + GLOBAL_SIS (FuelOnBoard) >= FUEL_RESERVE) { GetFTankCapacity (&r.corner); SetContextForeGroundColor ( diff --git a/sc2/src/uqm/sis.c b/sc2/src/uqm/sis.c index 0f7b6d130..cf35c2fe0 100644 --- a/sc2/src/uqm/sis.c +++ b/sc2/src/uqm/sis.c @@ -1418,12 +1418,15 @@ GetFuelTankCapacity (void) // crew pod, where the Nth unit of fuel would be located. // If the unit does not fit, false is returned, and *slotNr and // *compartmentNr are unchanged. +// Pre: unitNr >= FUEL_RESERER static bool GetFuelTankForFuelUnit (DWORD unitNr, COUNT *slotNr, DWORD *compartmentNr) { COUNT slotI; DWORD capacity = FUEL_RESERVE; + assert (unitNr >= FUEL_RESERVE); + slotI = NUM_MODULE_SLOTS; while (slotI--) { BYTE moduleType = GLOBAL_SIS (ModuleSlots[slotI]); @@ -1447,6 +1450,7 @@ GetFuelTankForFuelUnit (DWORD unitNr, COUNT *slotNr, DWORD *compartmentNr) DWORD GetFTankCapacity (POINT *ppt) { + DWORD capacity; DWORD fuelAmount; COUNT slotNr; DWORD compartmentNr; @@ -1454,14 +1458,22 @@ GetFTankCapacity (POINT *ppt) DWORD volume; COUNT rowNr; - + static const Color fuelColors[] = FUEL_COLOR_TABLE; - + + capacity = GetFuelTankCapacity (); fuelAmount = GetFuelTotal (); + if (fuelAmount < FUEL_RESERVE) + { + // Fuel is in the SIS reserve, not in a fuel tank. + // *ppt is unchanged + return capacity; + } + if (!GetFuelTankForFuelUnit (fuelAmount, &slotNr, &compartmentNr)) { // Fuel does not fit. *ppt is unchanged. - return GetFuelTankCapacity (); + return capacity; } moduleType = GLOBAL_SIS (ModuleSlots[slotNr]); @@ -1475,10 +1487,11 @@ GetFTankCapacity (POINT *ppt) else ppt->y = 30 - rowNr; + assert (rowNr + 1 < (COUNT) (sizeof fuelColors / sizeof fuelColors[0])); SetContextForeGroundColor (fuelColors[rowNr]); SetContextBackGroundColor (fuelColors[rowNr + 1]); - return GetFuelTankCapacity (); + return capacity; }