Repository navigation
Normalize zero block_z in validate_config to avoid divide by zero - #678
Merged
Merged
Conversation
astcenc_context_alloc() accepts a hand-built astcenc_config, and validate_block_size() treats a block_z of zero as meaning a 2D block size, the same as a value of one. astcenc_config_init() normalizes its own block_z input to at least one, but validate_config() did not apply the same normalization to the copy stored in the context. The stored block dimensions are later used as divisors by astcenc_compress_image() and astcenc_decompress_image() when computing block counts, so a config with block_z set to zero causes a division by zero (SIGFPE) in get_block_count_safe() on the first encode or decode call, instead of returning a normal error code. Normalize the stored value to at least one in validate_config(), which runs on every context allocation, so the context can never be created with a zero block divisor.
solidpixel
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
astcenc_context_alloc()accepts a hand-builtastcenc_configin addition to one populated byastcenc_config_init().validate_block_size()treats ablock_zof zero as meaning "2D", the same as a value of one, andastcenc_config_init()normalizes its ownblock_zinput to at least one before storing it (astcenc_entry.cpp line 527). Howevervalidate_config()did not apply the same normalization to the copy of the config stored in the context, so a context can be created holdingblock_z == 0.The stored block dimensions are later used as divisors when computing block counts:
A config with
block_zset to zero therefore causes a division by zero (SIGFPE) inget_block_count_safe()on the first encode or decode call, instead of behaving like the equivalent 2D config withblock_z == 1.Reproducer
Changes
block_zto at least one invalidate_config(), which runs on everyastcenc_context_alloc()call, matching the normalizationastcenc_config_init()already applies to its own inputs.block_z == 0(fails with a divide-by-zero or a wrong context config on the old code, passes after the fix).Behavior is otherwise unchanged for any config built through
astcenc_config_init(), which already normalizesblock_zto at least one.