1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00

Avoid integer overflow and conversion related UB.

This commit is contained in:
Karlis Senko 2018-04-28 20:51:30 +03:00
parent 40e6292ed2
commit 69330de89c
4 changed files with 18 additions and 18 deletions

View File

@ -25,9 +25,9 @@ namespace BitmapHandler
bool isPCX(const ui8 *header)//check whether file can be PCX according to header
{
int fSize = read_le_u32(header + 0);
int width = read_le_u32(header + 4);
int height = read_le_u32(header + 8);
ui32 fSize = read_le_u32(header + 0);
ui32 width = read_le_u32(header + 4);
ui32 height = read_le_u32(header + 8);
return fSize == width*height || fSize == width*height*3;
}

View File

@ -718,8 +718,10 @@ void CHeroHandler::loadExperience()
expPerLevel.push_back(34140);
while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
{
int i = expPerLevel.size() - 1;
expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
auto i = expPerLevel.size() - 1;
auto diff = expPerLevel[i] - expPerLevel[i-1];
diff += diff / 5;
expPerLevel.push_back (expPerLevel[i] + diff);
}
expPerLevel.pop_back();//last value is broken
}

View File

@ -105,15 +105,15 @@ namespace CGH
//RNG that works like H3 one
struct RandGen
{
int seed;
ui32 seed;
void srand(int s)
void srand(ui32 s)
{
seed = s;
}
void srand(int3 pos)
{
srand(110291 * pos.x + 167801 * pos.y + 81569);
srand(110291 * ui32(pos.x) + 167801 * ui32(pos.y) + 81569);
}
int rand()
{

View File

@ -544,17 +544,15 @@ int CGCreature::getNumberOfStacks(const CGHeroInstance *hero) const
else
split = 2;
int a = 1550811371;
int b = -935900487;
int c = 1943276003;
int d = -1120346418;
ui32 a = 1550811371u;
ui32 b = 3359066809u;
ui32 c = 1943276003u;
ui32 d = 3174620878u;
int R1 = a * pos.x + b * pos.y + c * pos.z + d;
int R2 = R1 / 65536;
int R3 = R2 % 32768;
if (R3 < 0)
R3 += 32767; //is it ever needed if we do modulo calculus?
int R4 = R3 % 100 + 1;
ui32 R1 = a * ui32(pos.x) + b * ui32(pos.y) + c * ui32(pos.z) + d;
ui32 R2 = (R1 >> 16) & 0x7fff;
int R4 = R2 % 100 + 1;
if (R4 <= 20)
split -= 1;