Skip to content

Commit

Permalink
Noise: Prevent unittest crash caused by division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed Jul 29, 2017
1 parent e9d7005 commit 765fd9a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/noise.cpp
Expand Up @@ -130,7 +130,9 @@ s32 PcgRandom::range(s32 min, s32 max)
if (max < min)
throw PrngException("Invalid range (max < min)");

u32 bound = max - min + 1;
// We have to cast to s64 because otherwise this could overflow,
// and signed overflow is undefined behavior.
u32 bound = (s64)max - (s64)min + 1;
return range(bound) + min;
}

Expand Down

0 comments on commit 765fd9a

Please sign in to comment.