DescentDevelopers / Descent3

Descent 3 by Outrage Entertainment
GNU General Public License v3.0
2.74k stars 231 forks source link

Fix RAND_MAX overflow warnings #387

Closed GravisZro closed 1 month ago

GravisZro commented 1 month ago

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

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

Additional Comments