From f55859c1dcebf02bd1d3bb9b1c70822591a0615b Mon Sep 17 00:00:00 2001 From: sahvx655-wq Date: Wed, 17 Jun 2026 19:55:18 +0530 Subject: [PATCH] Clamp HDR endpoint spread before narrowing it to int --- Source/astcenc_pick_best_endpoint_format.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Source/astcenc_pick_best_endpoint_format.cpp b/Source/astcenc_pick_best_endpoint_format.cpp index a899a7ff..caff41b7 100644 --- a/Source/astcenc_pick_best_endpoint_format.cpp +++ b/Source/astcenc_pick_best_endpoint_format.cpp @@ -404,9 +404,15 @@ static void compute_color_error_for_every_integer_count_and_quant_level( // Estimate of color-component spread in low endpoint color float df = hmax_s(abs(pdif)); - int b = static_cast(bf); - int c = static_cast(cf); - int d = static_cast(df); + // Endpoint colors can fall well outside the input range, and an HDR + // input may contain very large or non-finite values, so these spreads + // are not bounded by the int range. Clamp before the narrowing cast to + // avoid undefined behavior; the values are only used in the threshold + // comparisons below and the largest threshold is 32768, so saturating + // at 65536 leaves every comparison result unchanged. + int b = static_cast(astc::clamp(bf, 0.0f, 65536.0f)); + int c = static_cast(astc::clamp(cf, 0.0f, 65536.0f)); + int d = static_cast(astc::clamp(df, 0.0f, 65536.0f)); // Determine which one of the 6 submodes is likely to be used in case of an RGBO-mode int rgbo_mode = 5; // 7 bits per component