From 973ab86c58215f2d94aaea493304ddd0e199177a Mon Sep 17 00:00:00 2001 From: zhaonan287 Date: Fri, 15 Nov 2024 15:03:17 +0800 Subject: [PATCH] Added security check to prevent underrunning array Signed-off-by: zhaonan287 --- Source/astcenc_block_sizes.cpp | 6 ++++++ Source/astcenc_color_quantize.cpp | 1 + Source/astcenc_percentile_tables.cpp | 4 ++++ Source/astcenc_pick_best_endpoint_format.cpp | 9 +++++++++ 4 files changed, 20 insertions(+) diff --git a/Source/astcenc_block_sizes.cpp b/Source/astcenc_block_sizes.cpp index e7b5348..0093bf5 100644 --- a/Source/astcenc_block_sizes.cpp +++ b/Source/astcenc_block_sizes.cpp @@ -821,6 +821,12 @@ static void construct_block_size_descriptor_2d( // Gather all the decimation grids that can be used with the current block #if !defined(ASTCENC_DECOMPRESS_ONLY) const float *percentiles = get_2d_percentile_table(x_texels, y_texels); + if (percentiles == nullptr) { + delete wb; +#ifdef ASTC_CUSTOMIZED_ENABLE + return false; +#endif + } float always_cutoff = (privateProfile != HIGH_QUALITY_PROFILE) ? 1.0f : 0.0f; #else // Unused in decompress-only builds diff --git a/Source/astcenc_color_quantize.cpp b/Source/astcenc_color_quantize.cpp index 4ac0aff..7b61a7c 100644 --- a/Source/astcenc_color_quantize.cpp +++ b/Source/astcenc_color_quantize.cpp @@ -73,6 +73,7 @@ static inline uint8_t quant_color( quant_method quant_level, int value ) { + value = astc::clamp(value, 0, 255); // 255: maximum value int index = value * 2 + 1; return color_unquant_to_uquant_tables[quant_level - QUANT_6][index]; } diff --git a/Source/astcenc_percentile_tables.cpp b/Source/astcenc_percentile_tables.cpp index 448ddcc..98f5777 100644 --- a/Source/astcenc_percentile_tables.cpp +++ b/Source/astcenc_percentile_tables.cpp @@ -1168,6 +1168,10 @@ const float *get_2d_percentile_table( ) { float* unpacked_table = new float[WEIGHTS_MAX_BLOCK_MODES]; const packed_percentile_table *apt = get_packed_table(xdim, ydim); + if (apt == nullptr) { + delete[] unpacked_table; + return nullptr; + } // Set the default percentile for (unsigned int i = 0; i < WEIGHTS_MAX_BLOCK_MODES; i++) diff --git a/Source/astcenc_pick_best_endpoint_format.cpp b/Source/astcenc_pick_best_endpoint_format.cpp index d3b9f6c..2b97bd9 100644 --- a/Source/astcenc_pick_best_endpoint_format.cpp +++ b/Source/astcenc_pick_best_endpoint_format.cpp @@ -917,6 +917,9 @@ static float two_partitions_find_best_combination_for_bitcount( if (ql >= QUANT_6) { + if (best_integer_count < 2) { // 2: minimum integer_count + return ERROR_CALC_DEFAULT; + } for (int i = 0; i < 2; i++) { best_formats[i] = best_combined_format[ql][best_integer_count - 2][i]; @@ -1042,6 +1045,9 @@ static float three_partitions_find_best_combination_for_bitcount( if (ql >= QUANT_6) { + if (best_integer_count < 3) { // 3: minimum integer_count + return ERROR_CALC_DEFAULT; + } for (int i = 0; i < 3; i++) { best_formats[i] = best_combined_format[ql][best_integer_count - 3][i]; @@ -1178,6 +1184,9 @@ static float four_partitions_find_best_combination_for_bitcount( if (ql >= QUANT_6) { + if (best_integer_count < 4) { // 4: minimum integer_count + return ERROR_CALC_DEFAULT; + } for (int i = 0; i < 4; i++) { best_formats[i] = best_combined_format[ql][best_integer_count - 4][i]; -- Gitee