RFR: 8328934: Assert that ABS input and output are legal [v3]

Aleksey Shipilev shade at openjdk.org
Fri Apr 12 10:10:42 UTC 2024


On Fri, 12 Apr 2024 10:02:06 GMT, Andrew Haley <aph at openjdk.org> wrote:

>> This is why we can't have nice things. So the fix would be to check for overflow first, and the do the abs, like this?
>> 
>> 
>> template<class T> inline T asserted_abs(T x, const char* file, int line) {
>>   if (std::is_integral<T>::value && x == std::numeric_limits<T>::min()) {
>> #ifdef ASSERT    
>>     report_vm_error(file, line, "ABS: argument should not allow overflow");
>> #endif   
>>     // Do not allow UB, return the overflowed value.
>>     return x; 
>>   } else {
>>     T res = (x > 0) ? x : -x;
>> #ifdef ASSERT
>>     if (res < 0) {
>>       report_vm_error(file, line, "ABS: result should be non-negative");
>>     }
>> #endif
>>     return res;
>>   }
>> }
>
> Or not, because GCC knows that assert may be `noreturn`? In that case never mind.

Yeah, this is ugly, especially since we have to decide what do we return on overflow in product bits. I'd prefer not to do this.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/18751#discussion_r1562344322


More information about the hotspot-dev mailing list