JDK-8244496 : Add utilities to check for arithmetic overflow and underflow
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 15
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2020-05-06
  • Updated: 2021-01-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
Checking for overflow or underflow of some arithmetic operation is a problem that arises in various contexts. It can be somewhat tricky to write such tests though. Depending on the operation and the types involved, naive approaches may involve undefined behavior which the compiler may recognize and "optimize" in a way that defeats the purpose of the calculation.

Some carefully developed utilities for such operations would be helpful, both by having a single place for the possibly convoluted code, and providing meaningful names to hide that code behind.

Comments
As an example of the problems that can arise: {code} size_t bytes = .....; char* ptr = .....; if (uintx(ptr + bytes) < uintx(ptr)) { // wraps around 64-bit {code} clang concludes that the "ptr + bytes" expression cannot overflow (else it would invoke undefined behavior), so the condition is never true.
06-05-2020