diff options
| author | Pere001 <[email protected]> | 2022-11-15 12:29:19 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-11-15 12:29:19 +0100 |
| commit | c8fd93d35628545a22189f07c39e4cb821ddc1c2 (patch) | |
| tree | 72772b3b837fca57a7a50166deb2883128bf0f51 /src | |
| parent | 2604b9f72bcc64fe291efd5a2a61595807d39c1c (diff) | |
| download | raylib-c8fd93d35628545a22189f07c39e4cb821ddc1c2.tar.gz raylib-c8fd93d35628545a22189f07c39e4cb821ddc1c2.zip | |
Warning on GetRandomValue range limit (#2800)
Added a comment explaining the range limitations of GetRandomValue.
Added a run-time warning TRACELOG when GetRandomValue is called with an invalid range.
Diffstat (limited to 'src')
| -rw-r--r-- | src/rcore.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/rcore.c b/src/rcore.c index 54feb38b..3fec0d42 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2816,6 +2816,13 @@ int GetRandomValue(int min, int max) max = min; min = tmp; } + + // WARNING: Ranges higher than RAND_MAX will return invalid results. More specifically, if (max - min) > INT_MAX there will + // be an overflow, and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold. + if ((unsigned int)(max - min) > (unsigned int)RAND_MAX) + { + TRACELOG(LOG_WARNING, "Invalid GetRandomValue arguments. Range should not be higher than %i.", RAND_MAX); + } return (rand()%(abs(max - min) + 1) + min); } |
