Add fbounds-safety annotations for bit_depths.

Reasoning:

Analysis showed `bit_depths` is passed from `VP8LCreateHuffmanTree` (as
`huff_code->code_lengths`) to `GenerateOptimalTree` (as `bit_depths`
with size `histogram_size` = `huff_code->num_symbols`) and then to
`SetBitDepths`. The `HuffmanTreeCode` struct stores `code_lengths` and
`codes` pointers, both sized by `num_symbols`. These arrays are
allocated in `GetHuffBitLengthsAndCodes` (called by
`EncodeImageInternal`) based on `num_symbols`.

The fix involves:
- Annotating `HuffmanTreeCode::code_lengths` and
  `HuffmanTreeCode::codes` with `__counted_by(num_symbols)` in
  `src/utils/huffman_encode_utils.h`.
- Annotating the `bit_depths` parameter in `GenerateOptimalTree` with
  `__counted_by(histogram_size)` in `src/utils/huffman_encode_utils.c`.
- Annotating the `bit_depths` parameter in `SetBitDepths` with
  `__indexable` in `src/utils/huffman_encode_utils.c`, as the size
  parameter (`histogram_size`) is not directly available but indexing is
  known to be safe based on caller logic (indices `tree->value` are
  within `[0, histogram_size - 1]`).

Bug: 432511821
Change-Id: Icfd32f15d0744983b5912d527e5bc59ac58343a5
This commit is contained in:
Arman Hasanzadeh
2025-08-12 19:01:15 -07:00
committed by James Zern
parent 2246828be3
commit 4cc27eb808
2 changed files with 8 additions and 5 deletions

View File

@@ -139,7 +139,7 @@ static int CompareHuffmanTrees(const void* ptr1, const void* ptr2) {
static void SetBitDepths(const HuffmanTree* const tree,
const HuffmanTree* const pool,
uint8_t* const bit_depths, int level) {
uint8_t* WEBP_INDEXABLE const bit_depths, int level) {
if (tree->pool_index_left >= 0) {
SetBitDepths(&pool[tree->pool_index_left], pool, bit_depths, level + 1);
SetBitDepths(&pool[tree->pool_index_right], pool, bit_depths, level + 1);
@@ -170,7 +170,8 @@ static void SetBitDepths(const HuffmanTree* const tree,
static void GenerateOptimalTree(const uint32_t* const histogram,
int histogram_size, HuffmanTree* tree,
int tree_depth_limit,
uint8_t* const bit_depths) {
uint8_t* WEBP_COUNTED_BY(histogram_size)
const bit_depths) {
uint32_t count_min;
HuffmanTree* tree_pool;
int tree_size_orig = 0;

View File

@@ -31,9 +31,11 @@ typedef struct {
// Struct to represent the tree codes (depth and bits array).
typedef struct {
int num_symbols; // Number of symbols.
uint8_t* code_lengths; // Code lengths of the symbols.
uint16_t* codes; // Symbol Codes.
int num_symbols; // Number of symbols.
// Code lengths of the symbols.
uint8_t* WEBP_COUNTED_BY(num_symbols) code_lengths;
// Symbol Codes.
uint16_t* WEBP_COUNTED_BY(num_symbols) codes;
} HuffmanTreeCode;
// Struct to represent the Huffman tree.