From 5f8c47da210431909ee73f68233956afcdc5cdae Mon Sep 17 00:00:00 2001 From: theRealAph Date: Wed, 28 Oct 2020 15:37:03 +0000 Subject: [PATCH 1/2] JDK-8255544: Create a checked cast --- src/hotspot/share/utilities/globalDefinitions.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/hotspot/share/utilities/globalDefinitions.hpp b/src/hotspot/share/utilities/globalDefinitions.hpp index 831ed3716dfdb..c8f98f4065d16 100644 --- a/src/hotspot/share/utilities/globalDefinitions.hpp +++ b/src/hotspot/share/utilities/globalDefinitions.hpp @@ -446,6 +446,21 @@ inline size_t pointer_delta(const MetaWord* left, const MetaWord* right) { #define CAST_TO_FN_PTR(func_type, value) (reinterpret_cast(value)) #define CAST_FROM_FN_PTR(new_type, func_ptr) ((new_type)((address_word)(func_ptr))) +// In many places we've added C-style casts to silence compiler +// warnings, for example when truncating a size_t to an int when we +// know the size_t is a small struct. Such casts are risky because +// they effectively disable useful compiler warnings. We can make our +// lives safer with this function, which ensures that any cast is +// reversible without loss of information. It doesn't check +// everything: it isn't intended to make sure that pointer types are +// compatible, for example. +template +T2 checked_cast(T1 thing) { + T2 result = static_cast(thing); + assert(static_cast(result) == thing, "must be"); + return result; +} + // Need the correct linkage to call qsort without warnings extern "C" { typedef int (*_sort_Fn)(const void *, const void *); From a3c9516eff3fd1c7f916cb3279e206413f86a880 Mon Sep 17 00:00:00 2001 From: theRealAph Date: Sat, 31 Oct 2020 13:55:38 +0000 Subject: [PATCH 2/2] JDK-8255544: Create a checked cast --- src/hotspot/share/memory/heap.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hotspot/share/memory/heap.cpp b/src/hotspot/share/memory/heap.cpp index 3bcf3043cf36f..d55bc07e2c0be 100644 --- a/src/hotspot/share/memory/heap.cpp +++ b/src/hotspot/share/memory/heap.cpp @@ -770,7 +770,8 @@ int CodeHeap::segmap_hops(size_t beg, size_t end) { if (beg < end) { // setup _segmap pointers for faster indexing address p = (address)_segmap.low() + beg; - int hops_expected = (int)(((end-beg-1)+(free_sentinel-2))/(free_sentinel-1)); + int hops_expected + = checked_cast(((end-beg-1)+(free_sentinel-2))/(free_sentinel-1)); int nhops = 0; size_t ix = end-beg-1; while (p[ix] > 0) {