Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RAND_MAX overflow warnings #387

Merged
merged 1 commit into from
May 24, 2024

Conversation

GravisZro
Copy link
Contributor

In psrand.h it attempts to undefine RAND_MAX and then define it once again. This may work on some compilers but not G++/Clang. To resolve this error RAND_MAX was renamed to D3_RAND_MAX but only in files .cpp that included psrand.h. The code behavior should restored to that of the official release.

NOTE: Not every overflow is fixed because as far as I can tell, that's how it is in the official release as psrand.h is not included everywhere RAND_MAX is used.

Pull Request Type

  • GitHub Workflow changes
  • Documentation or Wiki changes
  • Build and Dependency changes
  • Runtime changes
    • Render changes
    • Audio changes
    • Input changes
    • Network changes
    • Other changes

Description

When compiling D3, there are several overflow warning regarding RAND_MAX + 1 because the value of RAND_MAX contains the same as INT32_MAX (0x7FFFFFFF). However, psrand.h does #undef RAND_MAX and then defines it as 0x7FFF. This does not work with G++ and Clang and the value 0x7FFFFFFFis retained. Since compilers assume a given value is a signed integer (unless specified otherwise) adding one will cause an overflow and thus causing a overflow warning. This is a sign that the value of RAND_MAX was not redefined to 0x7FFF. The solution is to simply rename references to RAND_MAX that would be redefined (on a more forgiving compiler) from RAND_MAX to D3_RAND_MAX.

However, not all parts of the code that use RAND_MAX also include psrand.h which means the ISO RAND_MAX value was likely used in the official release. This is why some overflow warnings will still persist after this patch.

Related Issues

Screenshots (if applicable)

Checklist

  • I have tested my changes locally and verified that they work as intended.
  • I have documented any new or modified functionality.
  • I have reviewed the changes to ensure they do not introduce any unnecessary complexity or duplicate code.
  • I understand that by submitting this pull request, I am agreeing to license my contributions under the project's license.

Additional Comments

@GravisZro GravisZro changed the title Fix RAND_MAX overflow errors Fix RAND_MAX overflow warnings May 23, 2024
@@ -34,9 +34,7 @@
* $NoKeywords: $
*/

#undef RAND_MAX
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems to be missing include guards

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve these changes, the original file didn't have the guard either.

@bryanperris bryanperris added the cleanup Code cleanup label May 23, 2024
In `psrand.h` it attempts to undefine RAND_MAX and then redefine it.
This may work on some compilers but G++/Clang. To resolve this error
RAND_MAX was renamed to D3_RAND_MAX but **only** in files that
included `psrand.h`. The code behavior should restored to that of the
official release.
@bryanperris bryanperris merged commit 9c039e6 into DescentDevelopers:main May 24, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code cleanup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants