space-to-depth: guard output_channels size against integer overflow - #10907
Merged
copybara-service[bot] merged 1 commit intoAug 4, 2026
Conversation
reshape_space_to_depth_nhwc() computed output_channels as input_channels * block_size * block_size with no overflow guard. On 32-bit size_t targets (WASM, ARM32, which this library explicitly supports), entirely plausible dimensions -- e.g. input_channels=100000, block_size=300 -- silently wrap: the true value (9,000,000,000) overflows to 410,065,408. Since output_channels is reported back to the caller via output_channels_out for output-buffer sizing, a wrapped value can cause the caller to under-allocate its output buffer. This mirrors the xnn_safe_mul() guard already applied to 9 sibling operators in this codebase for the same size_t-overflow class (batch-matrix-multiply-nc, deconvolution-nhwc, average-pooling-nhwc, dynamic-fully-connected-nc, convolution-nchw/nhwc, resize-bilinear-nchw/ nhwc, and the dwconv/unpooling indirection-buffer fixes).
qukhan
approved these changes
Aug 4, 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.
reshape_space_to_depth_nhwc()insrc/operators/transpose-nd.ccomputedoutput_channelsasinput_channels * block_size * block_sizewith no overflow guard.On 32-bit
size_ttargets (WASM, ARM32, which this library explicitly supports), entirely plausible dimensions can silently wrap -- e.g.input_channels=100000, block_size=300gives a true value of9,000,000,000, which wraps to410,065,408in unguarded 32-bit arithmetic. Sinceoutput_channelsis reported back to the caller viaoutput_channels_outfor output-buffer sizing, a wrapped value can cause the caller to under-allocate its output buffer relative to what the operator's internal shape/stride computation actually produces.This mirrors the
xnn_safe_mul()guard already applied to 9 sibling operators in this codebase for the identicalsize_t-overflow class:batch-matrix-multiply-nc.c,deconvolution-nhwc.c,average-pooling-nhwc.c,dynamic-fully-connected-nc.c,convolution-nchw.c/convolution-nhwc.c,resize-bilinear-nchw.c/resize-bilinear-nhwc.c.Verified standalone (32-bit and 64-bit arithmetic) that the guard correctly rejects the overflowing case while accepting all in-range values unchanged.