Skip to content

Commit 765fd9a

Browse files
committedJul 29, 2017
Noise: Prevent unittest crash caused by division by zero
1 parent e9d7005 commit 765fd9a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed
 

Diff for: ‎src/noise.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ s32 PcgRandom::range(s32 min, s32 max)
130130
if (max < min)
131131
throw PrngException("Invalid range (max < min)");
132132

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

0 commit comments

Comments
 (0)
Please sign in to comment.