fix(qdp): guard batch size multiplication against overflow#1324
Merged
Conversation
`Preprocessor::validate_batch` and `AmplitudeEncoder::encode_batch_f32` computed `num_samples * sample_size` without overflow checks, while the analogous angle encoder paths already use `checked_mul`. On 64-bit hosts the product can wrap and let an undersized buffer pass validation, leading to out-of-bounds reads in the encoder. Mirror the angle.rs pattern: use `checked_mul` and surface an `InvalidInput` error on overflow. Add a unit test covering the overflow path through the shared preprocessor.
ryankert01
approved these changes
May 18, 2026
Member
|
Make sense, will let copilot double check for me. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens QDP batch validation by preventing unchecked usize multiplication overflow when deriving expected batch buffer sizes, aligning amplitude/preprocessing behavior with existing checked length validation patterns.
Changes:
- Added
checked_muloverflow handling inPreprocessor::validate_batch. - Added matching overflow handling in
AmplitudeEncoder::encode_batch_f32. - Added a unit test for the preprocessing overflow path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
qdp/qdp-core/src/preprocessing.rs |
Guards batch expected-length calculation and tests overflow rejection. |
qdp/qdp-core/src/gpu/encodings/amplitude.rs |
Guards f32 amplitude batch expected-length calculation before length comparison. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ryankert01
pushed a commit
that referenced
this pull request
May 20, 2026
`Preprocessor::validate_batch` and `AmplitudeEncoder::encode_batch_f32` computed `num_samples * sample_size` without overflow checks, while the analogous angle encoder paths already use `checked_mul`. On 64-bit hosts the product can wrap and let an undersized buffer pass validation, leading to out-of-bounds reads in the encoder. Mirror the angle.rs pattern: use `checked_mul` and surface an `InvalidInput` error on overflow. Add a unit test covering the overflow path through the shared preprocessor.
8 tasks
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.
Summary
Preprocessor::validate_batchandAmplitudeEncoder::encode_batch_f32computednum_samples * sample_sizewithout overflow checks. The angle encoder paths already usechecked_mul(angle.rs:87, :143, :327, :405, :528) — these two sites were missed.checked_mulreturningMahoutError::InvalidInputon overflow.Changes
qdp/qdp-core/src/preprocessing.rs: guardnum_samples * sample_sizeinvalidate_batch.qdp/qdp-core/src/gpu/encodings/amplitude.rs: guard the same multiplication inencode_batch_f32(the f64 path already routes throughPreprocessor::validate_batch).preprocessing.rscovering the overflow path.Test plan
cargo test --manifest-path qdp/qdp-core/Cargo.toml --lib— 77 passed, 0 failedvalidate_batch_rejects_size_overflowexercises thechecked_mulfailure branch