Skip to content

Commit 6000fed

Browse files
committed
compressor fuzzing: fixing rare overflow in RDO candidate picker
1 parent d7ef703 commit 6000fed

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

encoder/basisu_astc_hdr_6x6_enc.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,9 @@ struct candidate_encoding
27232723

27242724
int m_reuse_delta_index;
27252725

2726-
float m_t, m_d, m_bits;
2726+
// m_t can get VERY large
2727+
double m_t, m_d;
2728+
float m_bits;
27272729

27282730
candidate_encoding()
27292731
{
@@ -5967,7 +5969,7 @@ static bool compress_strip_task(
59675969
}
59685970

59695971
// Find best overall candidate
5970-
double best_t = BIG_FLOAT_VAL;
5972+
double best_t = DBL_MAX;
59715973
int best_candidate_index = -1;
59725974

59735975
float best_d_ssim = BIG_FLOAT_VAL;
@@ -6114,10 +6116,13 @@ static bool compress_strip_task(
61146116
mode_penalty *= (complex_block ? RUN_PENALTY * 2.0f : RUN_PENALTY);
61156117

61166118
float candidate_bits = (float)candidate.m_coder.get_total_bits();
6117-
float candidate_d = candidate_mse * mode_penalty;
6119+
6120+
double candidate_d = (double)candidate_mse * mode_penalty;
61186121

61196122
const float D_POWER = 2.0f;
6120-
float candidate_t = perceptual_scale * powf(candidate_d, D_POWER) + candidate_bits * (global_cfg.m_lambda * 1000.0f);
6123+
6124+
// this value can get VERY large after squaring on random (fuzzed) HDR inputs
6125+
double candidate_t = perceptual_scale * pow(candidate_d, D_POWER) + candidate_bits * (global_cfg.m_lambda * 1000.0f);
61216126

61226127
candidate.m_t = candidate_t;
61236128
candidate.m_d = candidate_d;
@@ -6131,6 +6136,14 @@ static bool compress_strip_task(
61316136

61326137
} // candidate_iter
61336138

6139+
if (best_candidate_index < 0)
6140+
{
6141+
assert(0);
6142+
6143+
// Should never happen
6144+
best_candidate_index = 0;
6145+
}
6146+
61346147
if (global_cfg.m_gaussian1_fallback && (outer_pass == 0) && (very_complex_block) && (best_d_ssim > SWITCH_TO_GAUSSIAN_FILTERED_THRESH1_D_SSIM))
61356148
{
61366149
debug_state.m_total_gaussian1_blocks.fetch_add(1, std::memory_order_relaxed);
@@ -6152,7 +6165,7 @@ static bool compress_strip_task(
61526165
// candidate diversity boosting - consider candidates along/near the Pareto front
61536166
const candidate_encoding& comp_candidate = candidates[best_candidate_index];
61546167

6155-
float best_d = BIG_FLOAT_VAL;
6168+
double best_d = DBL_MAX;
61566169

61576170
for (uint32_t candidate_iter = 0; candidate_iter < candidates.size_u32(); candidate_iter++)
61586171
{

0 commit comments

Comments
 (0)