From a87bc7e4f0e797a108f447a1c9801abe39b700da Mon Sep 17 00:00:00 2001 From: Kim Barrett Date: Thu, 2 Jan 2025 08:11:55 +0000 Subject: [PATCH] 8345374: Ubsan: runtime error: division by zero Reviewed-by: jwaters, ayang, amitkumar --- src/hotspot/share/gc/g1/g1HeapSizingPolicy.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/hotspot/share/gc/g1/g1HeapSizingPolicy.cpp b/src/hotspot/share/gc/g1/g1HeapSizingPolicy.cpp index 0b7db7836d6a8..e8642e59cb4a4 100644 --- a/src/hotspot/share/gc/g1/g1HeapSizingPolicy.cpp +++ b/src/hotspot/share/gc/g1/g1HeapSizingPolicy.cpp @@ -198,6 +198,14 @@ size_t G1HeapSizingPolicy::young_collection_expansion_amount() { } static size_t target_heap_capacity(size_t used_bytes, uintx free_ratio) { + assert(free_ratio <= 100, "precondition"); + if (free_ratio == 100) { + // If 100 then below calculations will divide by zero and return min of + // resulting infinity and MaxHeapSize. Avoid issues of UB vs is_iec559 + // and ubsan warnings, and just immediately return MaxHeapSize. + return MaxHeapSize; + } + const double desired_free_percentage = (double) free_ratio / 100.0; const double desired_used_percentage = 1.0 - desired_free_percentage;