Skip to content

space-to-depth: guard output_channels size against integer overflow - #10907

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
prasanna8585:fix-space-to-depth-output-channels-overflow
Aug 4, 2026
Merged

space-to-depth: guard output_channels size against integer overflow#10907
copybara-service[bot] merged 1 commit into
google:masterfrom
prasanna8585:fix-space-to-depth-output-channels-overflow

Conversation

@prasanna8585

@prasanna8585 prasanna8585 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

reshape_space_to_depth_nhwc() in src/operators/transpose-nd.c 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 can silently wrap -- e.g. input_channels=100000, block_size=300 gives a true value of 9,000,000,000, which wraps to 410,065,408 in unguarded 32-bit arithmetic. 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 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 identical size_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.

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).
@copybara-service
copybara-service Bot merged commit f72e9f3 into google:master Aug 4, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants