8341178: TypeRawPtr::add_offset may be "miscompiled" due to UB

Reviewed-by: dlong, kvn
This commit is contained in:
Kim Barrett 2024-10-11 21:11:12 +00:00
parent 1f6bd0c3e5
commit 0a57fe1df6

View File

@ -3111,8 +3111,8 @@ const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
return (TypeRawPtr*)(new TypeRawPtr(ptr,nullptr))->hashcons(); return (TypeRawPtr*)(new TypeRawPtr(ptr,nullptr))->hashcons();
} }
const TypeRawPtr *TypeRawPtr::make( address bits ) { const TypeRawPtr *TypeRawPtr::make(address bits) {
assert( bits, "Use TypePtr for null" ); assert(bits != nullptr, "Use TypePtr for null");
return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons(); return (TypeRawPtr*)(new TypeRawPtr(Constant,bits))->hashcons();
} }
@ -3201,15 +3201,21 @@ const TypePtr* TypeRawPtr::add_offset(intptr_t offset) const {
case TypePtr::BotPTR: case TypePtr::BotPTR:
case TypePtr::NotNull: case TypePtr::NotNull:
return this; return this;
case TypePtr::Null:
case TypePtr::Constant: { case TypePtr::Constant: {
address bits = _bits+offset; uintptr_t bits = (uintptr_t)_bits;
if ( bits == 0 ) return TypePtr::NULL_PTR; uintptr_t sum = bits + offset;
return make( bits ); if (( offset < 0 )
? ( sum > bits ) // Underflow?
: ( sum < bits )) { // Overflow?
return BOTTOM;
} else if ( sum == 0 ) {
return TypePtr::NULL_PTR;
} else {
return make( (address)sum );
}
} }
default: ShouldNotReachHere(); default: ShouldNotReachHere();
} }
return nullptr; // Lint noise
} }
//------------------------------eq--------------------------------------------- //------------------------------eq---------------------------------------------